Skip to content

Commit f12a376

Browse files
committed
Update Flux Dispatcher.dispatch and waitFor examples
The previous examples didn't properly work when 1) a Store callback does waitFor on Stores that haven't been reached yet and 2) a Store callback waits on another Store that is already waiting. The updated example uses constructs Promises up front and then asynchronously resolves them.
1 parent 78958fe commit f12a376

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

docs/docs/flux-todo-list.md

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,6 @@ var _addPromise = function(callback, payload) {
7474
}));
7575
};
7676

77-
/**
78-
* Empty the queue of callback invocation promises.
79-
*/
80-
var _clearPromises = function() {
81-
_promises = [];
82-
};
83-
8477
var Dispatcher = function() {};
8578
Dispatcher.prototype = merge(Dispatcher.prototype, {
8679

@@ -99,12 +92,27 @@ Dispatcher.prototype = merge(Dispatcher.prototype, {
9992
* @param {object} payload The data from the action.
10093
*/
10194
dispatch: function(payload) {
102-
_callbacks.forEach(function(callback) {
103-
_addPromise(callback, payload);
95+
// First create array of promises for callbacks to reference.
96+
var _resolves = [];
97+
var _rejects = [];
98+
_promises = _callbacks.map(function(_, i) {
99+
return new Promise(function(resolve, reject) {
100+
_resolves[i] = resolve;
101+
_rejects[i] = reject;
102+
});
103+
});
104+
// Dispatch to callbacks and resolve/reject promises.
105+
_callbacks.forEach(function(callback, i) {
106+
// Callback can return an obj, to resolve, or a promise, to chain.
107+
// See waitFor() for why this might be useful.
108+
Promise.resolve(callback(payload)).then(function() {
109+
_resolves[i](payload);
110+
}, function() {
111+
_rejects[i](new Error('Dispatcher callback unsuccessful'));
112+
});
104113
});
105-
Promise.all(_promises).then(_clearPromises);
114+
_promises = [];
106115
}
107-
108116
});
109117

110118
module.exports = Dispatcher;
@@ -583,18 +591,18 @@ Adding Dependency Management to the Dispatcher
583591

584592
As I said previously, our Dispatcher implementation is a bit naive. It's pretty good, but it will not suffice for most applications. We need a way to be able to manage dependencies between Stores. Let's add that functionality with a waitFor() method within the main body of the Dispatcher class.
585593

586-
We'll need another public method, waitFor().
594+
We'll need another public method, waitFor(). Note that it return a Promise that can in turn be returned from the Store callback.
587595

588596
```javascript
589597
/**
590598
* @param {array} promisesIndexes
591599
* @param {function} callback
592600
*/
593601
waitFor: function(promiseIndexes, callback) {
594-
var selectedPromises = _promises.filter(function(/*object*/ _, /*number*/ j) {
595-
return promiseIndexes.indexOf(j) !== -1;
602+
var selectedPromises = promiseIndexes.map(function(index) {
603+
return _promises[index];
596604
});
597-
Promise.all(selectedPromises).then(callback);
605+
return Promise.all(selectedPromises).then(callback);
598606
}
599607
```
600608

0 commit comments

Comments
 (0)