Permalink
Browse files

d

  • Loading branch information...
youngjay committed Jun 6, 2013
1 parent d30b55e commit e4746ded51dd05a3f5551b665c16e41a2670f4eb
Showing with 41 additions and 6 deletions.
  1. +22 −5 lib/promise.js
  2. +19 −1 test/promise.js
View
@@ -5,6 +5,10 @@ var Subscribable = require('./subscribable');
var noop = function() {};
+var returnValueNeedToBeEvaluate = function(o) {
+ return o !== undefined && !_.isFunction(o);
+};
+
var PromiseInterFace = Subscribable.extend({
subscribe: function(subscriber) {
throw new Error('to be implemented')
@@ -25,7 +29,13 @@ var PromiseInterFace = Subscribable.extend({
var disposeCurrentSubscriber = self.subscribe(function() {
disposePreviousSubscriber();
- subscriberDisposeHandler = transformer(slice.call(arguments), subscriber);
+ subscriberDisposeHandler = null;
+ var ret = transformer(slice.call(arguments), subscriber);
+ if (returnValueNeedToBeEvaluate(ret)) {
+ subscriber(ret);
+ } else {
+ subscriberDisposeHandler = ret;
+ }
});
return function() {
@@ -74,14 +84,21 @@ var Promise = PromiseInterFace.extend(
}
};
- var dispose = handle(function() {
+ var evaluateArgs = function() {
if (!isDisposed) {
disposePrevArgs();
prevArgsDisposeHandler = whenAllSubscribablesAreNotified(arguments).subscribe(function(args) {
subscriber.apply(null, args);
- })
- }
- });
+ });
+ }
+ };
+
+ var dispose = handle(evaluateArgs);
+ // if return something that was not a dispose
+
+ if (returnValueNeedToBeEvaluate(dispose)) {
+ evaluateArgs(dispose);
+ }
return function() {
if (!isDisposed) {
View
@@ -49,6 +49,17 @@ describe('promise', function() {
assert(spy.calledWith(2))
});
+
+
+ it('return promise', function() {
+ var a = new Promise(function(callback) {
+ return 2;
+ });
+
+ a.subscribe(spy);
+
+ assert(spy.calledWith(2))
+ })
});
describe('#then', function() {
@@ -70,7 +81,15 @@ describe('promise', function() {
assert(spy.calledWith(2))
});
+ it('should evaluate return promise data', function() {
+ r1.then(function(a, next) {
+ return new Promise(function(callback) {
+ return a + 1
+ });
+ }).subscribe(spy);
+ assert(spy.calledWith(2))
+ });
it('should call previous return function when called', function() {
@@ -322,5 +341,4 @@ describe('promise', function() {
})
})
-
})

0 comments on commit e4746de

Please sign in to comment.