shablon 0.0.1-rc.3 → 0.0.1-rc.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -3
- package/package.json +1 -1
- package/src/state.js +20 -7
package/README.md
CHANGED
|
@@ -142,7 +142,7 @@ importing it from [npm](https://www.npmjs.com/package/shablon).
|
|
|
142
142
|
|
|
143
143
|
The keys of an `obj` must be "stringifiable" because they are used internally to construct a path to the reactive value.
|
|
144
144
|
|
|
145
|
-
The values can be any valid JS value, including nested arrays and objects (aka. it is recursively reactive).
|
|
145
|
+
The values can be any valid JS primitive value, including nested plain arrays and objects (aka. it is recursively reactive).
|
|
146
146
|
|
|
147
147
|
Getters are also supported and can be used as reactive computed properties.
|
|
148
148
|
The value of a reactive getter is "cached", meaning that even if one of the getter dependency changes, as long as the resulting value is the same there will be no unnecessary watch events fired.
|
|
@@ -162,6 +162,9 @@ data.age++
|
|
|
162
162
|
data.activity = "rest"
|
|
163
163
|
```
|
|
164
164
|
|
|
165
|
+
> Note that Object values like `Date`, `Set`, `Map`, `WeakRef`, `WeakSet` and `WeakMap` values are not wrapped in a nested `Proxy` and they will be resolved as they are to avoid access errors.
|
|
166
|
+
> For other custom object types thay you may want to access without a `Proxy` you can use the special `__raw` key, e.g. `data.myCustomType.__raw.someKey`.
|
|
167
|
+
|
|
165
168
|
</details>
|
|
166
169
|
|
|
167
170
|
|
|
@@ -171,6 +174,8 @@ data.activity = "rest"
|
|
|
171
174
|
Watch registers a callback function that fires on initialization and
|
|
172
175
|
every time any of its evaluated `store` reactive properties change.
|
|
173
176
|
|
|
177
|
+
Note that for reactive getters, initially the watch `trackedFunc` will be invoked twice because we register a second internal watcher to cache the getter value.
|
|
178
|
+
|
|
174
179
|
It returns a "watcher" object that could be used to `unwatch()` the registered listener.
|
|
175
180
|
|
|
176
181
|
_Optionally also accepts a second callback function that is excluded from the evaluated
|
|
@@ -215,8 +220,6 @@ data.b++ // trigger watch update
|
|
|
215
220
|
data.c++ // doesn't trigger watch update
|
|
216
221
|
```
|
|
217
222
|
|
|
218
|
-
Note that for reactive getters, initially the watch trackCallback will be invoked twice because we register a second internal watcher to cache the getter value.
|
|
219
|
-
|
|
220
223
|
</details>
|
|
221
224
|
|
|
222
225
|
|
package/package.json
CHANGED
package/src/state.js
CHANGED
|
@@ -207,19 +207,21 @@ function createProxy(obj, pathWatcherIds) {
|
|
|
207
207
|
prop = "@" + prop;
|
|
208
208
|
}
|
|
209
209
|
|
|
210
|
+
const propVal = obj[prop]
|
|
211
|
+
|
|
210
212
|
// directly return symbols and functions (pop, push, etc.)
|
|
211
|
-
if (typeof prop == "symbol" || typeof
|
|
212
|
-
return
|
|
213
|
+
if (typeof prop == "symbol" || typeof propVal == "function") {
|
|
214
|
+
return propVal;
|
|
213
215
|
}
|
|
214
216
|
|
|
215
217
|
// wrap child object or array as sub store
|
|
216
218
|
if (
|
|
217
|
-
typeof
|
|
218
|
-
|
|
219
|
-
!
|
|
219
|
+
propVal !== null && typeof propVal == "object" &&
|
|
220
|
+
!propVal[parentSym] &&
|
|
221
|
+
!isExcludedInstance(propVal)
|
|
220
222
|
) {
|
|
221
|
-
|
|
222
|
-
obj[prop] = createProxy(
|
|
223
|
+
propVal[parentSym] = [obj, prop];
|
|
224
|
+
obj[prop] = createProxy(propVal, pathWatcherIds);
|
|
223
225
|
}
|
|
224
226
|
|
|
225
227
|
// register watch subscriber (if any)
|
|
@@ -342,6 +344,17 @@ function getPath(obj, prop) {
|
|
|
342
344
|
return currentPath;
|
|
343
345
|
}
|
|
344
346
|
|
|
347
|
+
function isExcludedInstance(val) {
|
|
348
|
+
return (
|
|
349
|
+
(val instanceof Date) ||
|
|
350
|
+
(val instanceof Set) ||
|
|
351
|
+
(val instanceof Map) ||
|
|
352
|
+
(val instanceof WeakRef) ||
|
|
353
|
+
(val instanceof WeakMap) ||
|
|
354
|
+
(val instanceof WeakSet)
|
|
355
|
+
)
|
|
356
|
+
}
|
|
357
|
+
|
|
345
358
|
function callWatchers(obj, prop, pathWatcherIds) {
|
|
346
359
|
let currentPath = getPath(obj, prop);
|
|
347
360
|
|