Chromium Code Reviews| 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; | |
|
calamity
2017/03/03 02:29:03
nit: newline.
Also, when does this happen?
tsergeant
2017/03/03 05:08:00
Mainly in the router, which likes to fire off acti
| |
| 61 this.data_ = bookmarks.reduceAction(this.data_, action); | |
| 62 this.notifyObservers_(this.data_); | |
| 63 }, | |
| 64 | |
| 65 /** | |
| 66 * @param {!BookmarksPageState} state | |
| 67 * @private | |
| 68 */ | |
| 69 notifyObservers_: function(state) { | |
| 70 this.observers_.forEach(function(o) { | |
| 71 o.onStateChanged(state); | |
| 72 }); | |
| 73 }, | |
| 74 }; | |
| 75 | |
| 76 cr.addSingletonGetter(Store); | |
| 77 | |
| 78 return { | |
| 79 Store: Store, | |
| 80 }; | |
| 81 }); | |
| OLD | NEW |