From 2430f52bb97fa9d682e5f028c977c5bf94c5ec38 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Fri, 23 Mar 2012 14:03:24 -0700 Subject: chore(module): move files around in preparation for more modules --- src/ngScenario/Future.js | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/ngScenario/Future.js (limited to 'src/ngScenario/Future.js') 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); +}; -- cgit v1.2.3