| 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_ = {}; |
| 16 /** @type {boolean} */ |
| 17 this.initialized_ = false; |
| 18 /** @type {!Array<!StoreObserver>} */ |
| 19 this.observers_ = []; |
| 20 } |
| 21 |
| 22 Store.prototype = { |
| 23 /** |
| 24 * @param {!BookmarksPageState} initialState |
| 25 */ |
| 26 init: function(initialState) { |
| 27 this.data_ = initialState; |
| 28 this.initialized_ = true; |
| 29 this.notifyObservers_(this.data_); |
| 30 }, |
| 31 |
| 32 /** @type {!BookmarksPageState} */ |
| 33 get data() { |
| 34 return this.data_; |
| 35 }, |
| 36 |
| 37 /** @return {boolean} */ |
| 38 isInitialized: function() { |
| 39 return this.initialized_; |
| 40 }, |
| 41 |
| 42 /** @param {!StoreObserver} observer */ |
| 43 addObserver: function(observer) { |
| 44 this.observers_.push(observer); |
| 45 }, |
| 46 |
| 47 /** @param {!StoreObserver} observer */ |
| 48 removeObserver: function(observer) { |
| 49 var index = this.observers_.indexOf(observer); |
| 50 this.observers_.splice(index, 1); |
| 51 }, |
| 52 |
| 53 /** |
| 54 * Transition to a new UI state based on the supplied |action|, and notify |
| 55 * observers of the change. |
| 56 * @param {Action} action |
| 57 */ |
| 58 handleAction: function(action) { |
| 59 if (!this.initialized_) |
| 60 return; |
| 61 |
| 62 this.data_ = bookmarks.reduceAction(this.data_, action); |
| 63 this.notifyObservers_(this.data_); |
| 64 }, |
| 65 |
| 66 /** |
| 67 * @param {!BookmarksPageState} state |
| 68 * @private |
| 69 */ |
| 70 notifyObservers_: function(state) { |
| 71 this.observers_.forEach(function(o) { |
| 72 o.onStateChanged(state); |
| 73 }); |
| 74 }, |
| 75 }; |
| 76 |
| 77 cr.addSingletonGetter(Store); |
| 78 |
| 79 return { |
| 80 Store: Store, |
| 81 }; |
| 82 }); |
| OLD | NEW |