diff options
Diffstat (limited to 'src/ngScenario/Future.js')
| -rw-r--r-- | src/ngScenario/Future.js | 64 | 
1 files changed, 64 insertions, 0 deletions
diff --git a/src/ngScenario/Future.js b/src/ngScenario/Future.js new file mode 100644 index 00000000..335dd2bb --- /dev/null +++ b/src/ngScenario/Future.js @@ -0,0 +1,64 @@ +'use strict'; + +/** + * A future action in a spec. + * + * @param {string} name of the future action + * @param {function()} future callback(error, result) + * @param {function()} Optional. function that returns the file/line number. + */ +angular.scenario.Future = function(name, behavior, line) { +  this.name = name; +  this.behavior = behavior; +  this.fulfilled = false; +  this.value = undefined; +  this.parser = angular.identity; +  this.line = line || function() { return ''; }; +}; + +/** + * Executes the behavior of the closure. + * + * @param {function()} doneFn Callback function(error, result) + */ +angular.scenario.Future.prototype.execute = function(doneFn) { +  var self = this; +  this.behavior(function(error, result) { +    self.fulfilled = true; +    if (result) { +      try { +        result = self.parser(result); +      } catch(e) { +        error = e; +      } +    } +    self.value = error || result; +    doneFn(error, result); +  }); +}; + +/** + * Configures the future to convert it's final with a function fn(value) + * + * @param {function()} fn function(value) that returns the parsed value + */ +angular.scenario.Future.prototype.parsedWith = function(fn) { +  this.parser = fn; +  return this; +}; + +/** + * Configures the future to parse it's final value from JSON + * into objects. + */ +angular.scenario.Future.prototype.fromJson = function() { +  return this.parsedWith(angular.fromJson); +}; + +/** + * Configures the future to convert it's final value from objects + * into JSON. + */ +angular.scenario.Future.prototype.toJson = function() { +  return this.parsedWith(angular.toJson); +};  | 
