-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from krawaller/refactorall
refactor Reflux.all
- Loading branch information
Showing
3 changed files
with
91 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,20 @@ | ||
var createAction = require('./createAction'); | ||
|
||
var slice = Array.prototype.slice; | ||
var createStore = require('./createStore'); | ||
|
||
/** | ||
* Track a set of Actions and Stores. Use Reflux.all if you need to handle | ||
* data coming in parallel. | ||
* | ||
* @param {...Action|Store} listenables Actions and Stores that should be | ||
* @param {...Publishers} publishers Publishers that should be | ||
* tracked. | ||
* @returns {Action} An action which tracks the provided Actions and Stores. | ||
* The action will emit once all of the provided listenables have emitted at | ||
* @returns {Store} A store which listens to the provided Publishers. | ||
* The store will emit once all of the provided publishers have emitted at | ||
* least once. | ||
*/ | ||
module.exports = function(/* listenables... */) { | ||
var numberOfListenables = arguments.length, | ||
// create a new array of the expected size. The initial | ||
// values will be falsy, which is fine for us. | ||
// Once each item in the array is truthy, the callback can be called | ||
listenablesEmitted, | ||
// these arguments will be used to *apply* the action. | ||
args, | ||
// this action combines all the listenables | ||
action = createAction(), | ||
// the original listenables | ||
listenables = slice.call(arguments); | ||
|
||
action._isAction = false; | ||
|
||
action.hasListener = function(listenable) { | ||
var i = 0, listener; | ||
|
||
for (; i < listenables.length; ++i) { | ||
listener = listenables[i]; | ||
if ((listener === listenable && !listener._isAction) || listener.hasListener && listener.hasListener(listenable)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
reset(); | ||
|
||
for (var i = 0; i < numberOfListenables; i++) { | ||
arguments[i].listen(newListener(i), null); | ||
} | ||
|
||
return action; | ||
|
||
function reset() { | ||
listenablesEmitted = new Array(numberOfListenables); | ||
args = new Array(numberOfListenables); | ||
} | ||
|
||
function newListener(i) { | ||
return function() { | ||
listenablesEmitted[i] = true; | ||
// Reflux users should not need to care about Array and arguments | ||
// differences. This makes sure that they get the expected Array | ||
// interface | ||
args[i] = slice.call(arguments); | ||
emitWhenAllListenablesEmitted(); | ||
}; | ||
} | ||
|
||
function emitWhenAllListenablesEmitted() { | ||
if (didAllListenablesEmit()) { | ||
action.apply(action, args); | ||
reset(); | ||
} | ||
} | ||
|
||
function didAllListenablesEmit() { | ||
// reduce cannot be used because it only iterates over *present* | ||
// elements in the array. Initially the Array doesn't contain | ||
// elements. For this reason the usage of reduce would always indicate | ||
// that all listenables emitted. | ||
for (var i = 0; i < numberOfListenables; i++) { | ||
if (!listenablesEmitted[i]) { | ||
return false; | ||
} | ||
var listenables = Array.prototype.slice.call(arguments); | ||
return createStore({ | ||
init: function(){ | ||
this.listenToAggregate.apply(this,listenables.concat("trigger")); | ||
} | ||
return true; | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters