Commits

Joris van der Burgh committed 6d6781e

Replaced "futures" meta package with "future"
Wrapped future objects with a jquery Deferred like api

Comments (0)

Files changed (3)

 
 
 module.exports = {
-	futures: require('futures')
+	//futures: require('futures'),
+	future: require('future'),
+	//defered, a wrapper around the futures library
+	deferred: require('./lib/promises.js').deferred
 };
 
+
+var Future = require('future');
+
+/** Wraps the future api in a jquery deferred like interface */
+module.exports = {
+
+	deferred: function() {
+		//var p = Futures.promise(guaranteed);
+		var p = Future.future(context);
+		return wrapPromise(p);		
+	}	
+};
+
+function is_array(input){
+    return typeof(input)=='object'&&(input instanceof Array);
+}
+function is_function(input) {
+	return typeof(input) === 'function';
+}
+  
+function wrapPassable(promise, passable) {	
+	//(then, done, fail, always, pipe. isResolved, and isRejected) 
+	var res = {
+		then:function() { promise.then.apply(promise, arguments); return this; },
+		done:function() { promise.done.apply(promise, arguments); return this; },
+		fail:function() { promise.fail.apply(promise, arguments); return this; },
+		always:function() { promise.always.apply(promise, arguments); return this; },
+		isResolved:function() { promise.isResolved.apply(promise, arguments); return this; },
+		isRejected:function() { promise.isRejected.apply(promise, arguments); return this; }
+	};
+	return res;
+}
+function wrapPromise(p) {
+	var passable = false,
+		state = 'pending';
+	
+	p.whenever(function(err, data) {
+		if(err) {
+			state = "rejected";
+			return;		
+		} 
+		state = "resolved";		
+	});
+	// p.when(function() {
+		// state = "resolved";
+	// }).fail(function() {
+		// state = "rejected";
+	// });
+	 
+	
+	var res = {
+		state:function() { return state; },
+		wrapCallback:function(callback) {
+			//p.whenever(function (error, data) {
+			p.whenever(callback);
+			
+			return this;
+		},
+		then:function(doneCallbacks, failCallbacks) {
+			p.whenever(function(err, data) {
+				if(err) {
+					if(!is_function(failCallbacks)) {
+						for(var i=0;i<failCallbacks.length;i++) {
+							failCallbacks[i].call(this, err, data); 
+						}
+					} else {
+						failCallbacks.call(this, err, data);
+					}
+					return;
+				} else {
+				
+					if(!is_function(doneCallbacks)) {
+						for(var i=0;i<doneCallbacks.length;i++) {
+							doneCallbacks[i].call(this, data); 
+						}
+					} else {
+						doneCallbacks.call(this, data);
+					}
+				}					
+			}); 
+			return this;
+		},
+		always:function(callback) {			
+			//p.when(callback).fail(callback);
+			p.whenever(callback);
+			return this;
+		},
+		
+		isResolved:function() { return state == "resolved"; },
+		isRejected:function() { return state == "rejected"; },
+		
+		// done: function() { p.when.apply(p, arguments); return this;},
+		// fail: function() { p.when.apply(p, arguments); return this; },
+		done: function(callback) { 
+			p.whenever(function(err, data) {
+				if(!err) {
+					var args = [];
+					for(var i=1;i<arguments.length;i++) {
+						args.push(arguments[i]);
+					}
+					callback.apply(this, args);
+				}
+			});
+			//p.when.apply(p, arguments); 
+			return this;
+		},
+		fail: function(callback) { 
+			p.whenever(function(err, data) {
+				if(err) {
+					callback.apply(this, arguments);
+				}
+			});
+		},
+		
+		resolve: function(args) { 
+			var err = undefined;
+			p.fulfill(err, args);
+			//p.fulfill(args);
+			return this;
+		},
+		reject: function(err, data) { 
+			p.fulfill(err, data);
+			//p.smash(args);			
+			return this;
+		},
+	
+		promise:function() {	
+			if(!passable) passable = wrapPassable(this /*, p.passable()*/);
+			return passable;
+		}
+	};
+	return res;
+}
+ 
 {
 
   "name": "nxt-core",
-  "version": "0.0.1",
+  "version": "0.0.2",
   "description": "Core library",
 
   "homepage": "https://bitbucket.org/nxt/node-nxt-core",
     "url": "https://bitbucket.org/nxt/node-nxt-core"
   },
   
-  "dependencies": {
-	"futures": ">=2.2.0"  
+  "dependencies": {	
+	"future": ">=2.2.x"  
   },  
   
   "main": "index.js",