| OLD | NEW |
| (Empty) | |
| 1 <!-- |
| 2 @license |
| 3 Copyright (c) 2017 The Polymer Project Authors. All rights reserved. |
| 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 7 Code distributed by Google as part of the polymer project is also |
| 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 9 --> |
| 10 |
| 11 <link rel="import" href="../utils/mixin.html"> |
| 12 |
| 13 <script> |
| 14 (function() { |
| 15 'use strict'; |
| 16 |
| 17 // Common implementation for mixin & behavior |
| 18 function mutablePropertyChange(inst, property, value, old, mutableData) { |
| 19 let isObject; |
| 20 if (mutableData) { |
| 21 isObject = (typeof value === 'object' && value !== null); |
| 22 // Pull `old` for Objects from temp cache, but treat `null` as a primitive |
| 23 if (isObject) { |
| 24 old = inst.__dataTemp[property]; |
| 25 } |
| 26 } |
| 27 // Strict equality check, but return false for NaN===NaN |
| 28 let shouldChange = (old !== value && (old === old || value === value)); |
| 29 // Objects are stored in temporary cache (cleared at end of |
| 30 // turn), which is used for dirty-checking |
| 31 if (isObject && shouldChange) { |
| 32 inst.__dataTemp[property] = value; |
| 33 } |
| 34 return shouldChange; |
| 35 } |
| 36 |
| 37 /** |
| 38 * Element class mixin to skip strict dirty-checking for objects and arrays |
| 39 * (always consider them to be "dirty"), for use on elements utilizing |
| 40 * `Polymer.PropertyEffects` |
| 41 * |
| 42 * By default, `Polymer.PropertyEffects` performs strict dirty checking on |
| 43 * objects, which means that any deep modifications to an object or array will |
| 44 * not be propagated unless "immutable" data patterns are used (i.e. all objec
t |
| 45 * references from the root to the mutation were changed). |
| 46 * |
| 47 * Polymer also provides a proprietary data mutation and path notification API |
| 48 * (e.g. `notifyPath`, `set`, and array mutation API's) that allow efficient |
| 49 * mutation and notification of deep changes in an object graph to all element
s |
| 50 * bound to the same object graph. |
| 51 * |
| 52 * In cases where neither immutable patterns nor the data mutation API can be |
| 53 * used, applying this mixin will cause Polymer to skip dirty checking for |
| 54 * objects and arrays (always consider them to be "dirty"). This allows a |
| 55 * user to make a deep modification to a bound object graph, and then either |
| 56 * simply re-set the object (e.g. `this.items = this.items`) or call `notifyPa
th` |
| 57 * (e.g. `this.notifyPath('items')`) to update the tree. Note that all |
| 58 * elements that wish to be updated based on deep mutations must apply this |
| 59 * mixin or otherwise skip strict dirty checking for objects/arrays. |
| 60 * |
| 61 * In order to make the dirty check strategy configurable, see |
| 62 * `Polymer.OptionalMutableData`. |
| 63 * |
| 64 * Note, the performance characteristics of propagating large object graphs |
| 65 * will be worse as opposed to using strict dirty checking with immutable |
| 66 * patterns or Polymer's path notification API. |
| 67 * |
| 68 * @polymerMixin |
| 69 * @memberof Polymer |
| 70 * @summary Element class mixin to skip strict dirty-checking for objects |
| 71 * and arrays |
| 72 */ |
| 73 Polymer.MutableData = Polymer.dedupingMixin(superClass => { |
| 74 |
| 75 /** |
| 76 * @polymerMixinClass |
| 77 * @implements {Polymer_MutableData} |
| 78 */ |
| 79 class MutableData extends superClass { |
| 80 /** |
| 81 * Overrides `Polymer.PropertyEffects` to provide option for skipping |
| 82 * strict equality checking for Objects and Arrays. |
| 83 * |
| 84 * This method pulls the value to dirty check against from the `__dataTemp
` |
| 85 * cache (rather than the normal `__data` cache) for Objects. Since the t
emp |
| 86 * cache is cleared at the end of a turn, this implementation allows |
| 87 * side-effects of deep object changes to be processed by re-setting the |
| 88 * same object (using the temp cache as an in-turn backstop to prevent |
| 89 * cycles due to 2-way notification). |
| 90 * |
| 91 * @param {string} property Property name |
| 92 * @param {*} value New property value |
| 93 * @param {*} old Previous property value |
| 94 * @return {boolean} Whether the property should be considered a change |
| 95 * @protected |
| 96 */ |
| 97 _shouldPropertyChange(property, value, old) { |
| 98 return mutablePropertyChange(this, property, value, old, true); |
| 99 } |
| 100 |
| 101 } |
| 102 |
| 103 return MutableData; |
| 104 |
| 105 }); |
| 106 |
| 107 /** |
| 108 * Element class mixin to add the optional ability to skip strict |
| 109 * dirty-checking for objects and arrays (always consider them to be |
| 110 * "dirty") by setting a `mutable-data` attribute on an element instance. |
| 111 * |
| 112 * By default, `Polymer.PropertyEffects` performs strict dirty checking on |
| 113 * objects, which means that any deep modifications to an object or array will |
| 114 * not be propagated unless "immutable" data patterns are used (i.e. all objec
t |
| 115 * references from the root to the mutation were changed). |
| 116 * |
| 117 * Polymer also provides a proprietary data mutation and path notification API |
| 118 * (e.g. `notifyPath`, `set`, and array mutation API's) that allow efficient |
| 119 * mutation and notification of deep changes in an object graph to all element
s |
| 120 * bound to the same object graph. |
| 121 * |
| 122 * In cases where neither immutable patterns nor the data mutation API can be |
| 123 * used, applying this mixin will allow Polymer to skip dirty checking for |
| 124 * objects and arrays (always consider them to be "dirty"). This allows a |
| 125 * user to make a deep modification to a bound object graph, and then either |
| 126 * simply re-set the object (e.g. `this.items = this.items`) or call `notifyPa
th` |
| 127 * (e.g. `this.notifyPath('items')`) to update the tree. Note that all |
| 128 * elements that wish to be updated based on deep mutations must apply this |
| 129 * mixin or otherwise skip strict dirty checking for objects/arrays. |
| 130 * |
| 131 * While this mixin adds the ability to forgo Object/Array dirty checking, |
| 132 * the `mutableData` flag defaults to false and must be set on the instance. |
| 133 * |
| 134 * Note, the performance characteristics of propagating large object graphs |
| 135 * will be worse by relying on `mutableData: true` as opposed to using |
| 136 * strict dirty checking with immutable patterns or Polymer's path notificatio
n |
| 137 * API. |
| 138 * |
| 139 * @polymerMixin |
| 140 * @memberof Polymer |
| 141 * @summary Element class mixin to optionally skip strict dirty-checking |
| 142 * for objects and arrays |
| 143 */ |
| 144 Polymer.OptionalMutableData = Polymer.dedupingMixin(superClass => { |
| 145 |
| 146 /** |
| 147 * @polymerMixinClass |
| 148 * @implements {Polymer_OptionalMutableData} |
| 149 */ |
| 150 class OptionalMutableData extends superClass { |
| 151 |
| 152 static get properties() { |
| 153 return { |
| 154 /** |
| 155 * Instance-level flag for configuring the dirty-checking strategy |
| 156 * for this element. When true, Objects and Arrays will skip dirty |
| 157 * checking, otherwise strict equality checking will be used. |
| 158 */ |
| 159 mutableData: Boolean |
| 160 }; |
| 161 } |
| 162 |
| 163 /** |
| 164 * Overrides `Polymer.PropertyEffects` to provide option for skipping |
| 165 * strict equality checking for Objects and Arrays. |
| 166 * |
| 167 * When `this.mutableData` is true on this instance, this method |
| 168 * pulls the value to dirty check against from the `__dataTemp` cache |
| 169 * (rather than the normal `__data` cache) for Objects. Since the temp |
| 170 * cache is cleared at the end of a turn, this implementation allows |
| 171 * side-effects of deep object changes to be processed by re-setting the |
| 172 * same object (using the temp cache as an in-turn backstop to prevent |
| 173 * cycles due to 2-way notification). |
| 174 * |
| 175 * @param {string} property Property name |
| 176 * @param {*} value New property value |
| 177 * @param {*} old Previous property value |
| 178 * @return {boolean} Whether the property should be considered a change |
| 179 * @protected |
| 180 */ |
| 181 _shouldPropertyChange(property, value, old) { |
| 182 return mutablePropertyChange(this, property, value, old, this.mutableDat
a); |
| 183 } |
| 184 } |
| 185 |
| 186 return OptionalMutableData; |
| 187 |
| 188 }); |
| 189 |
| 190 // Export for use by legacy behavior |
| 191 Polymer.MutableData._mutablePropertyChange = mutablePropertyChange; |
| 192 |
| 193 })(); |
| 194 </script> |
| OLD | NEW |