Chromium Code Reviews| Index: chrome/browser/resources/md_bookmarks/store.js |
| diff --git a/chrome/browser/resources/md_bookmarks/store.js b/chrome/browser/resources/md_bookmarks/store.js |
| index 077c6db535a2412f8e49c40e9861f914579a0300..aa8f4b9d9de77887eb55a7bcb0ac1023eed0f7dc 100644 |
| --- a/chrome/browser/resources/md_bookmarks/store.js |
| +++ b/chrome/browser/resources/md_bookmarks/store.js |
| @@ -15,7 +15,7 @@ cr.define('bookmarks', function() { |
| this.data_ = bookmarks.util.createEmptyState(); |
| /** @type {boolean} */ |
| this.initialized_ = false; |
| - /** @type {!Array<!Action>} */ |
| + /** @type {!Array<DeferredAction>} */ |
| this.queuedActions_ = []; |
| /** @type {!Array<!StoreObserver>} */ |
| this.observers_ = []; |
| @@ -29,7 +29,7 @@ cr.define('bookmarks', function() { |
| this.data_ = initialState; |
| this.queuedActions_.forEach(function(action) { |
| - this.reduce_(action); |
| + this.handleActionInternal_(action); |
| }.bind(this)); |
| this.initialized_ = true; |
| @@ -58,27 +58,54 @@ cr.define('bookmarks', function() { |
| }, |
| /** |
| - * Transition to a new UI state based on the supplied |action|, and notify |
| - * observers of the change. If the Store has not yet been initialized, the |
| - * action will be queued and performed upon initialization. |
| - * @param {Action} action |
| + * Handles a 'deferred' action, which can asynchronously dispatch actions |
| + * to the Store in order to reach a new UI state. DeferredActions have the |
| + * form `handleDeferredAction(function(dispatch) { ... })`). Inside that |
|
calamity
2017/05/03 07:49:46
Should this really be called dispatch? It's a bit
tsergeant
2017/05/04 01:08:49
If anything, I think it's confusing that the funct
calamity
2017/05/04 07:14:26
Hmm. I guess now that all DeferredActions go throu
tsergeant
2017/05/04 07:25:37
That's a fair point. Injecting the dispatch method
|
| + * function, the |dispatch| callback can be called asynchronously to |
| + * dispatch Actions directly to the Store. |
|
calamity
2017/05/03 07:49:46
So clients can totally mess this up and send bogus
tsergeant
2017/05/04 01:08:50
Yeah. This isn't a problem for any actions at the
calamity
2017/05/04 07:14:26
Acknowledged.
|
| + * @param {DeferredAction} action |
| */ |
| - handleAction: function(action) { |
| + handleDeferredAction: function(action) { |
| if (!this.initialized_) { |
| this.queuedActions_.push(action); |
| return; |
| } |
| - this.reduce_(action); |
| - this.notifyObservers_(this.data_); |
| + this.handleActionInternal_(action); |
| + }, |
| + |
| + /** |
| + * Transition to a new UI state based on the supplied |action|, and notify |
| + * observers of the change. If the Store has not yet been initialized, the |
| + * action will be queued and performed upon initialization. |
| + * @param {?Action} action |
| + */ |
| + handleAction: function(action) { |
| + this.handleDeferredAction(function(dispatch) { |
| + dispatch(action); |
| + }); |
| + }, |
| + |
| + /** |
| + * @param {DeferredAction} action |
| + */ |
| + handleActionInternal_: function(action) { |
| + action(this.reduce_.bind(this)); |
|
calamity
2017/05/03 07:49:46
nit: May as well inline this now.
tsergeant
2017/05/04 01:08:49
It's still called from multiple places, so I'd pre
|
| }, |
| /** |
| - * @param {Action} action |
| + * @param {?Action} action |
| * @private |
| */ |
| reduce_: function(action) { |
| + if (!action) |
| + return; |
| + |
| this.data_ = bookmarks.reduceAction(this.data_, action); |
| + // Batch notifications until after all initialization queuedActions are |
| + // resolved. |
| + if (this.isInitialized()) |
| + this.notifyObservers_(this.data_); |
| }, |
| /** |