diff options
| author | Misko Hevery | 2012-03-23 14:03:24 -0700 |
|---|---|---|
| committer | Misko Hevery | 2012-03-28 11:16:35 -0700 |
| commit | 2430f52bb97fa9d682e5f028c977c5bf94c5ec38 (patch) | |
| tree | e7529b741d70199f36d52090b430510bad07f233 /src/ngScenario/Future.js | |
| parent | 944098a4e0f753f06b40c73ca3e79991cec6c2e2 (diff) | |
| download | angular.js-2430f52bb97fa9d682e5f028c977c5bf94c5ec38.tar.bz2 | |
chore(module): move files around in preparation for more modules
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); +}; |
