Permalink
Browse files

travis-841337: Add Output/promise, fix toString

Output (i.e. what you get from an exec) needs events because the result
of an exec could evolve (e.g. "tail -f") but we also want a simple
'are we done' thing. This adds that so Output now has a then function
which acts like a promise.

Also we fix toString which should have used this.data in place of output
which must have been a cut and paste error. And remove the console.error from
update() to allow errors to propagate.

Signed-off-by: Joe Walker <jwalker@mozilla.com>
  • Loading branch information...
1 parent 423435b commit 79a1a96f4a28ea8c56a4719b028cb60dc1dbb3ac @joewalker joewalker committed Feb 16, 2013
Showing with 23 additions and 26 deletions.
  1. +23 −25 lib/gcli/cli.js
  2. +0 −1 lib/gcli/ui/inputter.js
View
@@ -1163,15 +1163,8 @@ Requisition.prototype.exec = function(options) {
this.commandOutputManager.onOutput({ output: output });
- var onDone = function(data) {
- output.complete(data);
- };
-
- var onError = function(error) {
- console.error(error);
- output.error = true;
- output.complete(error);
- };
+ var onDone = function(data) { output.complete(data, false); };
+ var onError = function(error) { output.complete(error, true); };
try {
var context = exports.createExecutionContext(this);
@@ -1228,12 +1221,12 @@ Requisition.prototype.clear = function() {
Requisition.prototype._then = function(thing, onDone, onError) {
var then = null;
if (thing != null && typeof thing.then === 'function') {
- // Old GCLI style / simple promises with a then function
+ // Simple promises with a then function
then = thing.then;
}
else if (thing != null && thing.promise != null &&
typeof thing.promise.then === 'function') {
- // Q / Mozilla add-ons style
+ // Deprecated: When we're passed a deferred rather than a promise
then = thing.promise.then;
}
@@ -1259,8 +1252,8 @@ Requisition.prototype.update = function(typed) {
return this._assign(args).then(function() {
this._structuralChangeInProgress = false;
this.onTextChange();
- }.bind(this), console.error);
- }.bind(this), console.error);
+ }.bind(this));
+ }.bind(this));
};
/**
@@ -1750,6 +1743,9 @@ function Output(options) {
this.error = false;
this.start = new Date();
+ this.deferred = Promise.defer();
+ this.then = this.deferred.promise.then;
+
this.onClose = util.createEvent('Output.onClose');
this.onChange = util.createEvent('Output.onChange');
}
@@ -1774,12 +1770,20 @@ Output.prototype.changed = function(data, ev) {
* Called when there is data to display, and the command has finished executing
* See changed() for details on parameters.
*/
-Output.prototype.complete = function(data, ev) {
+Output.prototype.complete = function(data, error, ev) {
this.end = new Date();
this.duration = this.end.getTime() - this.start.getTime();
this.completed = true;
+ this.error = error;
this.changed(data, ev);
+
+ if (error) {
+ this.deferred.reject(data);
+ }
+ else {
+ this.deferred.resolve(data);
+ }
};
/**
@@ -1847,20 +1851,14 @@ Output.prototype.toDom = function(element) {
* based terminals.
*/
Output.prototype.toString = function(document) {
- var output = this.data;
- if (output == null) {
- return '';
+ if (this.data.isView) {
+ return this.data.toDom(document).textContent;
}
- if (typeof HTMLElement !== 'undefined' && output instanceof HTMLElement) {
- return output.textContent;
- }
-
- if (output.isView) {
- return output.toDom(document).textContent;
+ if (typeof HTMLElement !== 'undefined' && this.data instanceof HTMLElement) {
+ return this.data.textContent;
}
-
- return output.toString();
+ return this.data == null ? '' : this.data.toString();
};
exports.Output = Output;
View
@@ -595,7 +595,6 @@ Inputter.prototype.getInputState = function() {
// values for its content
if (input.typed == null) {
input = { typed: '', cursor: { start: 0, end: 0 } };
- console.log('fixing input.typed=""', input);
}
// Workaround for a Bug 717268 (which is really a jsdom bug)

0 comments on commit 79a1a96

Please sign in to comment.