@webqit/observer 2.1.11 → 2.1.12
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 +15 -72
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ Observer API is an upcoming proposal!
|
|
|
14
14
|
|
|
15
15
|
## Motivation
|
|
16
16
|
|
|
17
|
-
Tracking mutations on JavaScript objects has historically relied on "object wrapping" techniques with [ES6 Proxies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy), and on "property mangling" techniques with [getters and setters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty). Besides how the first poses an *object identity* problem and the second, an *interoperability* problem, there is also much inflexibility in the programming model
|
|
17
|
+
Tracking mutations on JavaScript objects has historically relied on "object wrapping" techniques with [ES6 Proxies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy), and on "property mangling" techniques with [getters and setters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty). Besides how the first poses an *object identity* problem and the second, an *interoperability* problem, there is also much inflexibility in the programming model that the two techniques enable!
|
|
18
18
|
|
|
19
19
|
This is discussed extensively in [the introductory blog post](https://dev.to/oxharris/reinvestigating-reactivity-22e0-temp-slug-5973064?preview=8afd0f8b156bf0b0b1c08058837fe4986054e52a7450f0a28adbaf07dcb7f5659b724166f553fb98ceab3d080748e86b244684f515d579bcd0f48cbb#introducing-the-observer-api)<sup>draft</sup>
|
|
20
20
|
|
|
@@ -22,33 +22,17 @@ We find a design precedent to object observability in the [`Object.observe()`](h
|
|
|
22
22
|
|
|
23
23
|
## An Overview
|
|
24
24
|
|
|
25
|
-
The Observer API is a set of utility functions.
|
|
25
|
+
The Observer API is a set of utility functions for all things object observability - notably, the `Observer.observe()` and `Observer.intercept()` methods.
|
|
26
26
|
|
|
27
|
-
>
|
|
28
|
-
> <br>This is documentation for `Observer@2.x`. (Looking for [`Observer@1.x`](https://github.com/webqit/observer/tree/v1.7.6)?)
|
|
27
|
+
<details><summary>This is documentation for Observer@2.x</summary>
|
|
29
28
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
Observe mutations on any object or array!
|
|
33
|
-
|
|
34
|
-
```js
|
|
35
|
-
// Signature 1
|
|
36
|
-
Observer.observe( obj, callback[, options = {} ]);
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
```js
|
|
40
|
-
// Signature 2
|
|
41
|
-
Observer.observe( obj, [ prop, prop, ... ], callback[, options = {} ]);
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
```js
|
|
45
|
-
// Signature 3
|
|
46
|
-
Observer.observe( obj, prop, callback[, options = {} ]);
|
|
47
|
-
```
|
|
29
|
+
Looking for [`Observer@1.x`](https://github.com/webqit/observer/tree/v1.7.6)?
|
|
48
30
|
|
|
49
|
-
|
|
31
|
+
</details>
|
|
32
|
+
|
|
33
|
+
### Method: `Observer.observe()`
|
|
50
34
|
|
|
51
|
-
Observe arbitrary objects
|
|
35
|
+
Observe mutations on arbitrary objects or arrays!
|
|
52
36
|
|
|
53
37
|
```js
|
|
54
38
|
// An object
|
|
@@ -104,15 +88,12 @@ const abortController = Observer.observe( obj, ( mutations, flags ) => {
|
|
|
104
88
|
// Child
|
|
105
89
|
Observer.observe( obj, inspect, { signal: flags.signal } ); // <<<---- AbortSignal-cascading
|
|
106
90
|
|
|
91
|
+
// Child
|
|
107
92
|
Observer.observe( obj, inspect, { signal: flags.signal } ); // <<<---- AbortSignal-cascading
|
|
108
93
|
|
|
109
94
|
} );
|
|
110
95
|
```
|
|
111
96
|
|
|
112
|
-
> **Note**
|
|
113
|
-
> <br>This is documentation for `Observer@2.x`. (Looking for [`Observer@1.x`](https://github.com/webqit/observer/tree/v1.7.6)?)
|
|
114
|
-
|
|
115
|
-
|
|
116
97
|
└ *"Child" observers get automatically aborted at parent's "next turn", and at parent's own abortion!*
|
|
117
98
|
|
|
118
99
|
#### Concept: *Mutation APIs*
|
|
@@ -132,10 +113,9 @@ Observer.set( arr, 0, 'item0' ); // Array [ 'item0' ]
|
|
|
132
113
|
Observer.deleteProperty( arr, 0 ); // Array [ <1 empty slot> ]
|
|
133
114
|
```
|
|
134
115
|
|
|
135
|
-
<details>
|
|
136
|
-
<summary>Polyfill limitations</summary>
|
|
116
|
+
<details><summary>Polyfill limitations</summary>
|
|
137
117
|
|
|
138
|
-
|
|
118
|
+
In the polyfill, object observability doesn't work with literal operations. **Beware non-reactive operations**:
|
|
139
119
|
|
|
140
120
|
```js
|
|
141
121
|
// Literal object operators
|
|
@@ -181,10 +161,9 @@ arr.unshift( 'new-item0' );
|
|
|
181
161
|
arr.shift();
|
|
182
162
|
```
|
|
183
163
|
|
|
184
|
-
<details>
|
|
185
|
-
<summary>Polyfill limitations</summary>
|
|
164
|
+
<details><summary>Polyfill limitations</summary>
|
|
186
165
|
|
|
187
|
-
|
|
166
|
+
In the polyfill, object observability doesn't work with literal operations. **Beware non-reactive operations**:
|
|
188
167
|
|
|
189
168
|
```js
|
|
190
169
|
// The delete operator and object properties that haven't been accessorized
|
|
@@ -278,8 +257,7 @@ Observer.observe( obj, path, m => {
|
|
|
278
257
|
Observer.set( obj.level1, 'level2', 'level2-new-value' );
|
|
279
258
|
```
|
|
280
259
|
|
|
281
|
-
<details>
|
|
282
|
-
<summary>Console</summary>
|
|
260
|
+
<details><summary>Console</summary>
|
|
283
261
|
|
|
284
262
|
| type | path | value | isUpdate |
|
|
285
263
|
| ---- | ---- | ----- | -------- |
|
|
@@ -394,44 +372,9 @@ Observer.batch( arr, async () => {
|
|
|
394
372
|
|
|
395
373
|
> Method calls on a proxied instance - e.g. `Object.proxy( arr ).splice( 0 )` - also follow this strategy.
|
|
396
374
|
|
|
397
|
-
#### Concept: *Custom Details*
|
|
398
|
-
|
|
399
|
-
Pass some custom detail - an arbitrary value - to observers via a `params.detail` property.
|
|
400
|
-
|
|
401
|
-
```js
|
|
402
|
-
// A set operation with detail
|
|
403
|
-
Observer.set( obj, {
|
|
404
|
-
prop2: 'value2',
|
|
405
|
-
prop3: 'value3',
|
|
406
|
-
}, { detail: 'Certain detail' } );
|
|
407
|
-
```
|
|
408
|
-
|
|
409
|
-
└ *Observers recieve this value on their `mutation.detail` property.*
|
|
410
|
-
|
|
411
|
-
```js
|
|
412
|
-
// An observer with detail
|
|
413
|
-
Observer.observe( obj, 'prop1', mutation => {
|
|
414
|
-
console.log( 'A mutation has been made with detail:' + mutation.detail );
|
|
415
|
-
} );
|
|
416
|
-
```
|
|
417
|
-
|
|
418
375
|
### Method: `Observer.intercept()`
|
|
419
376
|
|
|
420
|
-
Intercept operations on any object or array before they happen!
|
|
421
|
-
|
|
422
|
-
```js
|
|
423
|
-
// Signature 1
|
|
424
|
-
Observer.intercept( obj, type, handler[, options = {} ]);
|
|
425
|
-
```
|
|
426
|
-
|
|
427
|
-
```js
|
|
428
|
-
// Signature 2
|
|
429
|
-
Observer.intercept( obj, traps[, options = {} ]);
|
|
430
|
-
```
|
|
431
|
-
|
|
432
|
-
#### Usage
|
|
433
|
-
|
|
434
|
-
Extend standard operations on an object - `Observer.set()`, `Observer.deleteProperty()`, etc - with custom traps using the [`Observer.intercept()`](https://webqit.io/tooling/observer/docs/api/reactions/intercept) method!
|
|
377
|
+
Intercept operations on any object or array before they happen! This helps you extend standard operations on an object - `Observer.set()`, `Observer.deleteProperty()`, etc - using Proxy-like traps.
|
|
435
378
|
|
|
436
379
|
└ *Below, we intercept all "set" operations for an HTTP URL then transform it to an HTTPS URL.*
|
|
437
380
|
|