@samitouri / QOS-React-2 / commits / f12a376f34

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.

Joshua Ma committed May 17, 2014 at 21:17 UTC f12a376f346ac8d61adf89ffcc1871cd0d52df45
1 file changed +23 -15
docs/docs/flux-todo-list.md
+23 -15
@@ -74,13 +74,6 @@ var _addPromise = function(callback, payload) {
74 }));
75 };
76
77 -/**
78 - * Empty the queue of callback invocation promises.
79 - */
80 -var _clearPromises = function() {
81 - _promises = [];
82 -};
83 -
77 var Dispatcher = function() {};
78 Dispatcher.prototype = merge(Dispatcher.prototype, {
79
@@ -99,12 +92,27 @@ Dispatcher.prototype = merge(Dispatcher.prototype, {
92 * @param {object} payload The data from the action.
93 */
94 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 + });
113 });
105 - Promise.all(_promises).then(_clearPromises);
114 + _promises = [];
115 }
107 -
116 });
117
118 module.exports = Dispatcher;
@@ -583,7 +591,7 @@ Adding Dependency Management to the Dispatcher
591
592 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.
593
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.
595
596 ```javascript
597 /**
@@ -591,10 +599,10 @@ We'll need another public method, waitFor().
599 * @param {function} callback
600 */
601 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];
604 });
597 - Promise.all(selectedPromises).then(callback);
605 + return Promise.all(selectedPromises).then(callback);
606 }
607 ```
608