Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(912)

Side by Side Diff: chrome/browser/resources/md_bookmarks/store_client.js

Issue 2704983002: MD Bookmarks: Proof-of-concept reimplementation of data storage/binding layer (Closed)
Patch Set: Add doc comments Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 Defines StoreClient, a Polymer behavior to tie a front-end
7 * element to back-end data from the store.
8 */
9
10 cr.define('bookmarks', function() {
11 /**
12 * @polymerBehavior
13 * @implements {StoreObserver}
14 */
15 var StoreClient = {
16 attached: function() {
17 /**
18 * @type {!Array<{
19 * localProperty: string,
20 * storeGetter: function(BookmarksPageState)
21 * }>}
22 */
23 this.observers_ = [];
24 bookmarks.Store.getInstance().addObserver(this);
25 },
26
27 detached: function() {
28 bookmarks.Store.getInstance().removeObserver(this);
29 },
30
31 /**
32 * Observe a particular part of the state tree, updating |localProperty|
33 * whenever the state changes. |storeGetter| should be a function which,
34 * given the whole state tree, returns the particular Object that this
35 * Client is interested in. Eg:
36 * observe('item', (state) => state.nodes[this.itemId]);
37 *
38 * @param {string} localProperty
39 * @param {function(BookmarksPageState)} storeGetter
40 */
41 observe: function(localProperty, storeGetter) {
42 this.observers_.push({
43 localProperty: localProperty,
44 storeGetter: storeGetter,
45 });
46 },
47
48 /**
49 * Helper to dispatch an action to the store, which will update the store
50 * data and then (possibly) flow through to the UI.
51 * @param {Action} action
52 */
53 dispatch: function(action) {
54 bookmarks.Store.getInstance().handleAction(action);
55 },
56
57 /** @param {string} newState */
58 onStateChanged: function(newState) {
59 if (!this.observers_)
60 return;
61 this.observers_.forEach(function(observer) {
62
63 var oldValue = this[observer.localProperty];
64 var newValue = observer.storeGetter(newState);
65
66 // Avoid poking Polymer unless something has actually changed. Reducers
67 // must return new objects rather than mutating existing objects, so
68 // any real changes will pass through correctly.
69 if (oldValue == newValue)
70 return;
71
72 this[observer.localProperty] = newValue;
73 }.bind(this));
74 },
75
76 updateFromStore: function() {
77 if (bookmarks.Store.getInstance().isInitialized())
78 this.onStateChanged(bookmarks.Store.getInstance().data);
79 },
80
81 /** @return {BookmarksPageState} */
82 getState: function() {
83 return bookmarks.Store.getInstance().data;
84 },
85 };
86
87 return {StoreClient: StoreClient};
88 });
OLDNEW
« no previous file with comments | « chrome/browser/resources/md_bookmarks/store_client.html ('k') | chrome/browser/resources/md_bookmarks/toolbar.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698