| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview Defines StoreClient, a Polymer behavior to tie a front-end | 6 * @fileoverview Defines StoreClient, a Polymer behavior to tie a front-end |
| 7 * element to back-end data from the store. | 7 * element to back-end data from the store. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 cr.define('bookmarks', function() { | 10 cr.define('bookmarks', function() { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 bookmarks.Store.getInstance().removeObserver(this); | 31 bookmarks.Store.getInstance().removeObserver(this); |
| 32 }, | 32 }, |
| 33 | 33 |
| 34 /** | 34 /** |
| 35 * Watches a particular part of the state tree, updating |localProperty| | 35 * Watches a particular part of the state tree, updating |localProperty| |
| 36 * to the return value of |valueGetter| whenever the state changes. Eg, to | 36 * to the return value of |valueGetter| whenever the state changes. Eg, to |
| 37 * keep |this.item| updated with the value of a node: | 37 * keep |this.item| updated with the value of a node: |
| 38 * watch('item', (state) => state.nodes[this.itemId]); | 38 * watch('item', (state) => state.nodes[this.itemId]); |
| 39 * | 39 * |
| 40 * Note that object identity is used to determine if the value has changed | 40 * Note that object identity is used to determine if the value has changed |
| 41 * before updating the UI, rather than Polymer-style deep equality. | 41 * before updating the UI, rather than Polymer-style deep equality. If the |
| 42 * getter function returns |undefined|, no changes will propagate to the UI. |
| 42 * | 43 * |
| 43 * Typechecking is supressed because this conflicts with | 44 * Typechecking is supressed because this conflicts with |
| 44 * Object.prototype.watch, which is a Gecko-only method that is recognized | 45 * Object.prototype.watch, which is a Gecko-only method that is recognized |
| 45 * by Closure. | 46 * by Closure. |
| 46 * @suppress {checkTypes} | 47 * @suppress {checkTypes} |
| 47 * @param {string} localProperty | 48 * @param {string} localProperty |
| 48 * @param {function(!BookmarksPageState)} valueGetter | 49 * @param {function(!BookmarksPageState)} valueGetter |
| 49 */ | 50 */ |
| 50 watch: function(localProperty, valueGetter) { | 51 watch: function(localProperty, valueGetter) { |
| 51 // TODO(tsergeant): Warn if localProperty is not a defined property. | 52 // TODO(tsergeant): Warn if localProperty is not a defined property. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 66 | 67 |
| 67 /** @param {string} newState */ | 68 /** @param {string} newState */ |
| 68 onStateChanged: function(newState) { | 69 onStateChanged: function(newState) { |
| 69 this.watches_.forEach(function(watch) { | 70 this.watches_.forEach(function(watch) { |
| 70 var oldValue = this[watch.localProperty]; | 71 var oldValue = this[watch.localProperty]; |
| 71 var newValue = watch.valueGetter(newState); | 72 var newValue = watch.valueGetter(newState); |
| 72 | 73 |
| 73 // Avoid poking Polymer unless something has actually changed. Reducers | 74 // Avoid poking Polymer unless something has actually changed. Reducers |
| 74 // must return new objects rather than mutating existing objects, so | 75 // must return new objects rather than mutating existing objects, so |
| 75 // any real changes will pass through correctly. | 76 // any real changes will pass through correctly. |
| 76 if (oldValue == newValue) | 77 if (oldValue == newValue || newValue == undefined) |
| 77 return; | 78 return; |
| 78 | 79 |
| 79 this[watch.localProperty] = newValue; | 80 this[watch.localProperty] = newValue; |
| 80 }.bind(this)); | 81 }.bind(this)); |
| 81 }, | 82 }, |
| 82 | 83 |
| 83 updateFromStore: function() { | 84 updateFromStore: function() { |
| 84 if (bookmarks.Store.getInstance().isInitialized()) | 85 if (bookmarks.Store.getInstance().isInitialized()) |
| 85 this.onStateChanged(bookmarks.Store.getInstance().data); | 86 this.onStateChanged(bookmarks.Store.getInstance().data); |
| 86 }, | 87 }, |
| 87 | 88 |
| 88 /** @return {!BookmarksPageState} */ | 89 /** @return {!BookmarksPageState} */ |
| 89 getState: function() { | 90 getState: function() { |
| 90 return bookmarks.Store.getInstance().data; | 91 return bookmarks.Store.getInstance().data; |
| 91 }, | 92 }, |
| 92 }; | 93 }; |
| 93 | 94 |
| 94 return { | 95 return { |
| 95 StoreClient: StoreClient, | 96 StoreClient: StoreClient, |
| 96 }; | 97 }; |
| 97 }); | 98 }); |
| OLD | NEW |