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 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} | |
|
michaelpg
2017/03/02 09:30:36
So, this is a StoreObserver but also has its own [
tsergeant
2017/03/02 23:20:35
Hmm, I'm not sure. StoreObserver was only added fo
calamity
2017/03/03 02:29:03
I actually do like watch. It implies to me that it
tsergeant
2017/03/03 05:08:00
Alright, renamed to |watch| and |watches_|
| |
| 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| | |
|
michaelpg
2017/03/02 09:30:36
nit: Observes
tsergeant
2017/03/02 23:20:35
Done.
| |
| 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 | |
|
michaelpg
2017/03/02 09:30:36
nit: remove blank line
tsergeant
2017/03/02 23:20:35
Done.
| |
| 63 var oldValue = this[observer.localProperty]; | |
| 64 var newValue = observer.storeGetter(newState); | |
|
michaelpg
2017/03/02 09:30:36
I had some trouble understanding what storeGetter
tsergeant
2017/03/02 23:20:35
Hmm, you're right: storeGetter is a weird name (so
| |
| 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) | |
|
michaelpg
2017/03/02 09:30:36
nit: mention in one of the function JSDoc comments
tsergeant
2017/03/02 23:20:35
I've added a note to the comment for `observe`
| |
| 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 }); | |
| OLD | NEW |