Connection.prototype.connect = function() { var deferred = Promise.defer(); this.script = exports.document.createElement('script'); this.script.src = '/socket.io/socket.io.js'; this.script.addEventListener('load', function() { this.socket = io.connect('http://' + this.host + ':' + this.port); // We're being passed execution results this.socket.on('executed', function(data) { var request = this.requests[data.id]; if (request == null) { throw new Error('Unknown request id \'' + data.id + '\'. Ignoring.'); } request.complete(data.error, data.type, data.data); delete this.requests[data.id]; }.bind(this)); // On first connection ask for the remote command-specs this.socket.on('connected', function(data) { deferred.resolve(); }.bind(this)); }.bind(this)); exports.document.head.appendChild(this.script); return deferred.promise; };
exports.promiseEach = function(array, action, scope) { if (array.length === 0) { return Promise.resolve([]); } var deferred = Promise.defer(); var replies = []; var callNext = function(index) { var onSuccess = function(reply) { replies[index] = reply; if (index + 1 >= array.length) { deferred.resolve(replies); } else { callNext(index + 1); } }; var onFailure = function(ex) { deferred.reject(ex); }; var reply = action.call(scope, array[index], index, array); Promise.resolve(reply).then(onSuccess).then(null, onFailure); }; callNext(0); return deferred.promise; };
Connection.prototype.disconnect = function(force) { var deferred = Promise.defer(); this.client.close(function() { deferred.resolve(); }); return request.promise; };
/** * A Request is a command typed at the client which lives until the command * has finished executing on the server */ function Request(typed, args) { this.json = { id: Request._nextRequestId++, typed: typed, args: args }; this._deferred = Promise.defer(); this.promise = this._deferred.promise; }
Connection.prototype.getCommandSpecs = function() { var deferred = Promise.defer(); var request = { to: this.actor, type: 'getCommandSpecs' }; this.client.request(request, function(response) { deferred.resolve(response.commandSpecs); }); return deferred.promise; };
Connection.prototype.getCommandSpecs = function() { var deferred = Promise.defer(); // When we have the remote command specs, add them locally this.socket.once('commandSpecs', function(data) { deferred.resolve(data.commandSpecs); }.bind(this)); this.socket.emit('getCommandSpecs'); return deferred.promise; };
/** * A Request is a command typed at the client which lives until the command * has finished executing on the server */ function Request(actor, typed, args) { this.json = { to: actor, type: 'execute', typed: typed, args: args, requestId: 'id-' + Request._nextRequestId++, }; this._deferred = Promise.defer(); this.promise = this._deferred.promise; }
var getData = function() { var deferred = Promise.defer(); var resolve = function() { deferred.resolve([ 'Shalom', 'Namasté', 'Hallo', 'Dydd-da', 'Chào', 'Hej', 'Saluton', 'Sawubona' ]); }; setTimeout(resolve, 10); return deferred.promise; };
Connection.prototype.connect = function() { var deferred = Promise.defer(); this.transport = debuggerSocketConnect(this.host, this.port); this.client = new DebuggerClient(this.transport); this.client.connect(function() { this.client.listTabs(function(response) { this.actor = response.gcliActor; deferred.resolve(); }.bind(this)); }.bind(this)); return deferred.promise; };
/** * Both 'lookup' and 'data' properties (see docs on SelectionType constructor) * in addition to being real data can be a function or a promise, or even a * function which returns a promise of real data, etc. This takes a thing and * returns a promise of actual values. */ function resolve(thing, context, neverForceAsync) { if (forceAsync && !neverForceAsync) { var deferred = promise.defer(); setTimeout(function() { promise.resolve(thing).then(function(resolved) { if (typeof resolved === 'function') { resolved = resolve(resolved(), neverForceAsync); } deferred.resolve(resolved); }); }, 500); return deferred.promise; } return promise.resolve(thing).then(function(resolved) { if (typeof resolved === 'function') { return resolve(resolved(context), context, neverForceAsync); } return resolved; }); }
Test.prototype.run = function(options) { assert.currentTest = this; this.status = stati.executing; if (assert.testLogging) { console.log('START: ' + assert.currentTest); } var deferred = Promise.defer(); setTimeout(function() { try { var reply = this.func.apply(this.suite, [ options ]); deferred.resolve(reply); } catch (ex) { deferred.reject(ex); } }.bind(this), delay); var onDone = function() { if (this.status === stati.executing) { this.status = this.failures.length === 0 ? stati.pass : stati.fail; } if (assert.testLogging) { console.log('END: ' + assert.currentTest); } assert.currentTest = null; }.bind(this); var onError = function(ex) { assert.ok(false, assert.currentTest + ': ' + ex); util.errorHandler(ex); onDone(); }; return deferred.promise.then(onDone, onError); };
Completer.prototype._getCompleterTemplateData = function() { // Some of the data created by this function can be calculated synchronously // but other parts depend on predictions which are asynchronous. var promisedDirectTabText = Promise.defer(); var promisedArrowTabText = Promise.defer(); var promisedEmptyParameters = Promise.defer(); var input = this.inputter.getInputState(); var current = this.requisition.getAssignmentAt(input.cursor.start); var predictionPromise = undefined; if (input.typed.trim().length !== 0) { predictionPromise = current.getPredictionAt(this.choice); } Promise.resolve(predictionPromise).then(function(prediction) { // directTabText is for when the current input is a prefix of the completion // arrowTabText is for when we need to use an -> to show what will be used var directTabText = ''; var arrowTabText = ''; var emptyParameters = []; if (input.typed.trim().length !== 0) { var cArg = current.arg; if (prediction) { var tabText = prediction.name; var existing = cArg.text; // Normally the cursor being just before whitespace means that you are // 'in' the previous argument, which means that the prediction is based // on that argument, however NamedArguments break this by having 2 parts // so we need to prepend the tabText with a space for NamedArguments, // but only when there isn't already a space at the end of the prefix // (i.e. ' --name' not ' --name ') if (current.isInName()) { tabText = ' ' + tabText; } if (existing !== tabText) { // Decide to use directTabText or arrowTabText // Strip any leading whitespace from the user inputted value because // the tabText will never have leading whitespace. var inputValue = existing.replace(/^\s*/, ''); var isStrictCompletion = tabText.indexOf(inputValue) === 0; if (isStrictCompletion && input.cursor.start === input.typed.length) { // Display the suffix of the prediction as the completion var numLeadingSpaces = existing.match(/^(\s*)/)[0].length; directTabText = tabText.slice(existing.length - numLeadingSpaces); } else { // Display the '-> prediction' at the end of the completer element // \u21E5 is the JS escape right arrow arrowTabText = '\u21E5 ' + tabText; } } } else { // There's no prediction, but if this is a named argument that needs a // value (that is without any) then we need to show that one is needed // For example 'git commit --message ', clearly needs some more text if (cArg.type === 'NamedArgument' && cArg.text === '') { emptyParameters.push('<' + current.param.type.name + '>\u00a0'); } } } // Add a space between the typed text (+ directTabText) and the hints, // making sure we don't add 2 sets of padding if (directTabText !== '') { directTabText += '\u00a0'; } else if (!this.requisition.typedEndsWithSeparator()) { emptyParameters.unshift('\u00a0'); } // Calculate the list of parameters to be filled in // We generate an array of emptyParameter markers for each positional // parameter to the current command. // Generally each emptyParameter marker begins with a space to separate it // from whatever came before, unless what comes before ends in a space. this.requisition.getAssignments().forEach(function(assignment) { // Named arguments are handled with a group [options] marker if (!assignment.param.isPositionalAllowed) { return; } // No hints if we've got content for this parameter if (assignment.arg.toString().trim() !== '') { return; } if (directTabText !== '' && current === assignment) { return; } var text = (assignment.param.isDataRequired) ? '<' + assignment.param.name + '>\u00a0' : '[' + assignment.param.name + ']\u00a0'; emptyParameters.push(text); }.bind(this)); var command = this.requisition.commandAssignment.value; var addOptionsMarker = false; // We add an '[options]' marker when there are named parameters that are // not filled in and not hidden, and we don't have any directTabText if (command && command.hasNamedParameters) { command.params.forEach(function(param) { var arg = this.requisition.getAssignment(param.name).arg; if (!param.isPositionalAllowed && !param.hidden && arg.type === "BlankArgument") { addOptionsMarker = true; } }, this); } if (addOptionsMarker) { // Add an nbsp if we don't have one at the end of the input or if // this isn't the first param we've mentioned emptyParameters.push('[options]\u00a0'); } promisedDirectTabText.resolve(directTabText); promisedArrowTabText.resolve(arrowTabText); promisedEmptyParameters.resolve(emptyParameters); }.bind(this), console.error); return { statusMarkup: this._getStatusMarkup(input), unclosedJs: this._getUnclosedJs(), scratchLink: this._getScratchLink(), directTabText: promisedDirectTabText.promise, arrowTabText: promisedArrowTabText.promise, emptyParameters: promisedEmptyParameters.promise }; };