| OLD | NEW |
| (Empty) |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * @fileoverview A singleton datastore for the Bookmarks page. Page state is | |
| 7 * publicly readable, but can only be modified by dispatching an Action to | |
| 8 * the store. | |
| 9 */ | |
| 10 | |
| 11 cr.define('bookmarks', function() { | |
| 12 /** @constructor */ | |
| 13 function Store() { | |
| 14 /** @type {!BookmarksPageState} */ | |
| 15 this.data_ = bookmarks.util.createEmptyState(); | |
| 16 /** @type {boolean} */ | |
| 17 this.initialized_ = false; | |
| 18 /** @type {!Array<!Action>} */ | |
| 19 this.queuedActions_ = []; | |
| 20 /** @type {!Array<!StoreObserver>} */ | |
| 21 this.observers_ = []; | |
| 22 } | |
| 23 | |
| 24 Store.prototype = { | |
| 25 /** | |
| 26 * @param {!BookmarksPageState} initialState | |
| 27 */ | |
| 28 init: function(initialState) { | |
| 29 this.data_ = initialState; | |
| 30 | |
| 31 this.queuedActions_.forEach(function(action) { | |
| 32 this.reduce_(action); | |
| 33 }.bind(this)); | |
| 34 | |
| 35 this.initialized_ = true; | |
| 36 this.notifyObservers_(this.data_); | |
| 37 }, | |
| 38 | |
| 39 /** @type {!BookmarksPageState} */ | |
| 40 get data() { | |
| 41 return this.data_; | |
| 42 }, | |
| 43 | |
| 44 /** @return {boolean} */ | |
| 45 isInitialized: function() { | |
| 46 return this.initialized_; | |
| 47 }, | |
| 48 | |
| 49 /** @param {!StoreObserver} observer */ | |
| 50 addObserver: function(observer) { | |
| 51 this.observers_.push(observer); | |
| 52 }, | |
| 53 | |
| 54 /** @param {!StoreObserver} observer */ | |
| 55 removeObserver: function(observer) { | |
| 56 var index = this.observers_.indexOf(observer); | |
| 57 this.observers_.splice(index, 1); | |
| 58 }, | |
| 59 | |
| 60 /** | |
| 61 * Transition to a new UI state based on the supplied |action|, and notify | |
| 62 * observers of the change. If the Store has not yet been initialized, the | |
| 63 * action will be queued and performed upon initialization. | |
| 64 * @param {Action} action | |
| 65 */ | |
| 66 handleAction: function(action) { | |
| 67 if (!this.initialized_) { | |
| 68 this.queuedActions_.push(action); | |
| 69 return; | |
| 70 } | |
| 71 | |
| 72 this.reduce_(action); | |
| 73 this.notifyObservers_(this.data_); | |
| 74 }, | |
| 75 | |
| 76 /** | |
| 77 * @param {Action} action | |
| 78 * @private | |
| 79 */ | |
| 80 reduce_: function(action) { | |
| 81 this.data_ = bookmarks.reduceAction(this.data_, action); | |
| 82 }, | |
| 83 | |
| 84 /** | |
| 85 * @param {!BookmarksPageState} state | |
| 86 * @private | |
| 87 */ | |
| 88 notifyObservers_: function(state) { | |
| 89 this.observers_.forEach(function(o) { | |
| 90 o.onStateChanged(state); | |
| 91 }); | |
| 92 }, | |
| 93 }; | |
| 94 | |
| 95 cr.addSingletonGetter(Store); | |
| 96 | |
| 97 return { | |
| 98 Store: Store, | |
| 99 }; | |
| 100 }); | |
| OLD | NEW |