Chromium Code Reviews| Index: chrome/browser/resources/md_bookmarks/bookmarks_store.js |
| diff --git a/chrome/browser/resources/md_bookmarks/bookmarks_store.js b/chrome/browser/resources/md_bookmarks/bookmarks_store.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..27b1cec9484e23b9926f47ef42871d52477b1cef |
| --- /dev/null |
| +++ b/chrome/browser/resources/md_bookmarks/bookmarks_store.js |
| @@ -0,0 +1,81 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
|
michaelpg
2017/03/02 09:30:35
2017 :-)
tsergeant
2017/03/02 23:20:35
Oops, done.
|
| +// Use of this source code is governed by a BSD-style license that can be |
|
michaelpg
2017/03/02 09:30:35
:-\ store.js would be a more consistent name (unli
tsergeant
2017/03/02 23:20:34
I did previously have the new store live in store.
calamity
2017/03/03 02:29:03
I don't think it's worth the churn in other parts
tsergeant
2017/03/03 05:08:00
Yup, store.js is the eventual name. bookmarks_stor
michaelpg
2017/03/03 22:04:57
Also SGTM!
|
| +// found in the LICENSE file. |
| + |
| +/** |
| + * @fileoverview A singleton datastore for the Bookmarks page. Page state is |
| + * publicly readable, but can only be modified by dispatching an Action to |
| + * the store. |
| + */ |
| + |
| +cr.define('bookmarks', function() { |
| + /** @constructor */ |
| + function Store() { |
| + /** @type {BookmarksPageState} */ |
|
michaelpg
2017/03/02 09:30:35
throughout: !BookmarksPageState for non-null?
tsergeant
2017/03/02 23:20:34
This isn't strictly necessary, since BookmarksPage
|
| + this.data_ = {}; |
| + /** @type {boolean} */ |
| + this.initialized_ = false; |
| + /** @type {!Array<!StoreObserver>} */ |
| + this.observers_ = []; |
| + } |
| + |
| + Store.prototype = { |
| + /** |
| + * @param {BookmarksPageState} initialState |
| + */ |
| + init: function(initialState) { |
| + this.data_ = initialState; |
| + this.initialized_ = true; |
| + this.notifyObservers_(this.data_); |
| + }, |
| + |
| + /** @type {BookmarksPageState} */ |
| + get data() { |
| + return this.data_; |
| + }, |
| + |
| + /** @return {boolean} */ |
| + isInitialized: function() { |
| + return this.initialized_; |
| + }, |
| + |
| + /** @param {!StoreObserver} observer */ |
| + addObserver: function(observer) { |
| + this.observers_.push(observer); |
| + }, |
| + |
| + /** @param {!StoreObserver} observer */ |
| + removeObserver: function(observer) { |
| + var index = this.observers_.indexOf(observer); |
| + this.observers_.splice(index, 1); |
| + }, |
| + |
| + /** |
| + * Transition to a new UI state based on the supplied |action|, and notify |
| + * observers of the change. |
| + * @param {Action} action |
| + */ |
| + handleAction: function(action) { |
| + if (!this.initialized_) |
| + return; |
| + this.data_ = bookmarks.reduceAction(this.data_, action); |
| + this.notifyObservers_(this.data_); |
| + }, |
| + |
| + /** |
| + * @param {BookmarksPageState} state |
| + * @private |
| + */ |
| + notifyObservers_: function(state) { |
| + this.observers_.forEach(function(o) { |
| + o.onStateChanged(state); |
| + }); |
| + }, |
| + }; |
| + |
| + cr.addSingletonGetter(Store); |
| + |
| + return { |
| + Store: Store, |
| + }; |
| +}); |