Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

rename

  • Loading branch information...
commit de9da7c79e977974cf47b941f38f2675655fd3db 1 parent b25d3ee
@youngjay authored
Showing with 47 additions and 45 deletions.
  1. +1 −1  lib/event.js
  2. +41 −44 lib/promise.js
  3. +5 −0 lib/subscribable.js
View
2  lib/event.js
@@ -1,6 +1,6 @@
var slice = [].slice;
-module.exports = require('./class')
+module.exports = require('./subscribable')
.extend(
function() {
this.__subscribers = [];
View
85 lib/promise.js
@@ -1,12 +1,14 @@
var PreservedEvent = require('../lib/preserved-event');
var slice = [].slice;
var _ = require('underscore');
+var Class = require('./class');
+var Subscribable = require('./subscribable');
var noop = function() {};
-var PromiseInterFace = require('./class').extend({
- _handle: function(callback) {
- throw new Error('must implement _handle')
+var PromiseInterFace = Subscribable.extend({
+ _handle: function(subscriber) {
+ throw new Error('to be implemented')
},
subscribe: function(subscriber) {
@@ -27,59 +29,53 @@ var PromiseInterFace = require('./class').extend({
})
.mixStatic({
when: function() {
- return mergePromises(arguments).then(function(arr, callback) {
+ return whenAllSubscribablesAreNotified(arguments).then(function(arr, callback) {
callback.apply(null, arr);
});
}
})
+var Promise = PromiseInterFace.extend(
+ function(handle) {
+ this._handle = function(subscriber) {
+ var isDisposed = false;
-var evaluatePromiseInArgs = function(args, subscriber) {
- mergePromises(args).subscribe(function(arr) {
- subscriber.apply(null, arr);
- });
-};
-
-var Promise = PromiseInterFace
- .extend(
- function(handle) {
- this._handle = function(subscriber) {
- var isDisposed = false;
+ var dispose = handle(function() {
+ if (!isDisposed) {
+ whenAllSubscribablesAreNotified(arguments).subscribe(function(arr) {
+ subscriber.apply(null, arr);
+ });
+ }
+ });
- var dispose = handle(function() {
- if (!isDisposed) {
- evaluatePromiseInArgs(arguments, subscriber);
+ return function() {
+ if (!isDisposed) {
+ isDisposed = true;
+ if (_.isFunction(dispose)) {
+ dispose();
}
- });
-
- return function() {
- if (!isDisposed) {
- isDisposed = true;
- if (_.isFunction(dispose)) {
- dispose();
- }
- }
- }
+ }
}
}
- )
+ }
+);
var ReturnObjectPromise = PromiseInterFace.extend(
function() {
var args = arguments;
- this._handle = function(callback) {
- callback.apply(null, args);
+ this._handle = function(subscriber) {
+ subscriber.apply(null, args);
return noop;
}
}
);
-var containsPromise = function(o) {
+var containsSubscribable = function(o) {
if (!o) {
return false;
}
- if (o.subscribe) {
+ if (Subscribable.test(o)) {
return true;
}
@@ -90,16 +86,16 @@ var containsPromise = function(o) {
return false;
};
-var mergePromises = function(promises) {
+var whenAllSubscribablesAreNotified = function(subscribables) {
- promises = slice.call(promises);
+ subscribables = slice.call(subscribables);
- return containsPromise(promises) ? new Promise(function(callback) {
- var results = new Array(promises.length);
+ return containsSubscribable(subscribables) ? new Promise(function(callback) {
+ var results = new Array(subscribables.length);
var check = creatCheck(results, callback);
- var disposes = subscribePromises(promises, check);
+ var disposes = checkEachSubscribables(subscribables, check);
return function() {
disposes.forEach(function(dispose) {
@@ -107,7 +103,7 @@ var mergePromises = function(promises) {
});
}
}) :
- new ReturnObjectPromise(promises);
+ new ReturnObjectPromise(subscribables);
;
};
@@ -134,6 +130,7 @@ var creatCheck = function(results, callback) {
return;
}
}
+
isFull = true;
notify();
} :
@@ -141,15 +138,15 @@ var creatCheck = function(results, callback) {
;
};
-var subscribePromises = function(promises, check) {
+var checkEachSubscribables = function(subscribables, check) {
var disposes = [];
- if (promises.length) {
- _.each(promises, function(dep, i) {
+ if (subscribables.length) {
+ _.each(subscribables, function(dep, i) {
if (_.isArray(dep)) {
- dep = mergePromises(dep);
+ dep = whenAllSubscribablesAreNotified(dep);
}
- if (dep.subscribe) {
+ if (Subscribable.test(dep)) {
disposes.push(dep.subscribe(function() {
check(i, slice.call(arguments));
}));
View
5 lib/subscribable.js
@@ -0,0 +1,5 @@
+module.exports = require('./class').extend({
+ subscribe: function() {
+ throw new Error('to be implemented');
+ }
+});
Please sign in to comment.
Something went wrong with that request. Please try again.