watch-state 3.5.1 → 3.5.2
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 +25 -5
- package/State/State.d.ts +8 -0
- package/State/State.es6.js +10 -0
- package/State/State.js +10 -0
- package/index.min.js +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -103,7 +103,7 @@ Was born during working on [@innet/dom](https://www.npmjs.com/package/@innet/dom
|
|
|
103
103
|
<sup>**[ [Install](#install) ]**</sup>
|
|
104
104
|
<sup>**[ [Usage](#usage) ]** [Simple example](#simple-example) • [Example Vanilla JS](#example-vanilla-js) • [Example React](#example-react) • [Example @innet/dom](#example-innetdom)</sup>
|
|
105
105
|
<sup>**[ [Watch](#watch) ]** [Update argument](#update-argument) • [Force update of Watch](#force-update-of-watch) • [Destroy Watch](#destroy-watch) • [Deep/Nested watchers](#deepnested-watchers)</sup>
|
|
106
|
-
<sup>**[ [State](#state) ]** [Get or Set value](#get-or-set-value) • [Force update of State](#force-update-of-state) • [Raw value](#raw-value)</sup>
|
|
106
|
+
<sup>**[ [State](#state) ]** [Get or Set value](#get-or-set-value) • [Force update of State](#force-update-of-state) • [Raw value](#raw-value) • [State.set (experimental)](#stateset-experimental)</sup>
|
|
107
107
|
<sup>**[ [Compute](#compute) ]** [Lazy computation](#lazy-computation) • [Force update of Compute](#force-update-of-compute) • [Destroy Compute](#destroy-compute)</sup>
|
|
108
108
|
<sup>**[ [Utils](#utils) ]** [onDestroy](#ondestroy) • [callEvent](#callevent) • [createEvent](#createevent) • [unwatch](#unwatch)</sup>
|
|
109
109
|
<sup>**[ [Typescript](#typescript) ]**</sup>
|
|
@@ -396,14 +396,16 @@ state.value++
|
|
|
396
396
|
## State
|
|
397
397
|
###### [🏠︎](#index) / State [↑](#watch) [↓](#compute)
|
|
398
398
|
|
|
399
|
-
<sup>[Get or Set value](#get-or-set-value) • [Force update of State](#force-update-of-state) • [Raw value](#raw-value)</sup>
|
|
399
|
+
<sup>[Get or Set value](#get-or-set-value) • [Force update of State](#force-update-of-state) • [Raw value](#raw-value) • [State.set (experimental)](#stateset-experimental)</sup>
|
|
400
400
|
|
|
401
401
|
**Reactive primitive** that automatically notifies all subscribed watchers when `.value` changes.
|
|
402
402
|
|
|
403
403
|
### Get or Set value
|
|
404
404
|
###### [🏠︎](#index) / [State](#state) / Get or Set value [↓](#force-update-of-state)
|
|
405
405
|
|
|
406
|
-
**Access or mutate the state value.**
|
|
406
|
+
**Access or mutate the state value.**
|
|
407
|
+
|
|
408
|
+
Reading `.value` inside `Observer` **auto-subscribes** to changes. Writing `.value` **triggers all watchers**.
|
|
407
409
|
|
|
408
410
|
```ts
|
|
409
411
|
const count = new State(0)
|
|
@@ -433,9 +435,9 @@ log.update() // logs: [1]
|
|
|
433
435
|
```
|
|
434
436
|
|
|
435
437
|
### Raw value
|
|
436
|
-
###### [🏠︎](#index) / [State](#state) / Raw value [↑](#force-update-of-state)
|
|
438
|
+
###### [🏠︎](#index) / [State](#state) / Raw value [↑](#force-update-of-state) [↓](#stateset-experimental)
|
|
437
439
|
|
|
438
|
-
`rawValue` returns the current value but **doesn't subscribe** to changes — unlike `value
|
|
440
|
+
`rawValue` returns the current value but **doesn't subscribe** to changes — unlike `value`.
|
|
439
441
|
|
|
440
442
|
```ts
|
|
441
443
|
const foo = new State(0)
|
|
@@ -449,6 +451,24 @@ bar.value++ // no logs
|
|
|
449
451
|
foo.value++ // logs: 2, 1
|
|
450
452
|
```
|
|
451
453
|
|
|
454
|
+
### State.set (experimental)
|
|
455
|
+
###### [🏠︎](#index) / [State](#state) / State.set [↑](#raw-value)
|
|
456
|
+
|
|
457
|
+
`State.set` mirrors the behavior of the value setter but returns `void`.
|
|
458
|
+
It is useful as a shorthand in arrow functions: `() => state.set(nextValue)` instead of `() => { state.value = nextValue }`.
|
|
459
|
+
|
|
460
|
+
Note: `state.set` cannot be used as a standalone function; `const set = state.set` is not supported.
|
|
461
|
+
|
|
462
|
+
```ts
|
|
463
|
+
const count = new State(0)
|
|
464
|
+
|
|
465
|
+
// Subscribing
|
|
466
|
+
new Watch(() => console.log(count.value))
|
|
467
|
+
// log: 0
|
|
468
|
+
|
|
469
|
+
count.set(1) // triggers: log: 1
|
|
470
|
+
```
|
|
471
|
+
|
|
452
472
|
## Compute
|
|
453
473
|
###### [🏠︎](#index) / Compute [↑](#state) [↓](#utils)
|
|
454
474
|
|
package/State/State.d.ts
CHANGED
|
@@ -32,6 +32,14 @@ export declare class State<V = unknown> extends Observable<V> {
|
|
|
32
32
|
*/
|
|
33
33
|
get value(): V;
|
|
34
34
|
set value(value: V);
|
|
35
|
+
/**
|
|
36
|
+
* @experimental
|
|
37
|
+
* Sets the state value. Identical to the `value` setter but returns `void`.
|
|
38
|
+
* Useful as a shorthand in arrow functions: `() => state.set(value)` instead of `() => { state.value = value }`
|
|
39
|
+
*
|
|
40
|
+
* `state.set` cannot be used as a standalone function: `const set = state.set`
|
|
41
|
+
*/
|
|
42
|
+
set(value: V): void;
|
|
35
43
|
/**
|
|
36
44
|
* Force triggers all watchers even if value didn't change.
|
|
37
45
|
*
|
package/State/State.es6.js
CHANGED
|
@@ -46,6 +46,16 @@ class State extends Observable {
|
|
|
46
46
|
this.update();
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* @experimental
|
|
51
|
+
* Sets the state value. Identical to the `value` setter but returns `void`.
|
|
52
|
+
* Useful as a shorthand in arrow functions: `() => state.set(value)` instead of `() => { state.value = value }`
|
|
53
|
+
*
|
|
54
|
+
* `state.set` cannot be used as a standalone function: `const set = state.set`
|
|
55
|
+
*/
|
|
56
|
+
set(value) {
|
|
57
|
+
this.value = value;
|
|
58
|
+
}
|
|
49
59
|
/**
|
|
50
60
|
* Force triggers all watchers even if value didn't change.
|
|
51
61
|
*
|
package/State/State.js
CHANGED
|
@@ -50,6 +50,16 @@ class State extends Observable.Observable {
|
|
|
50
50
|
this.update();
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* @experimental
|
|
55
|
+
* Sets the state value. Identical to the `value` setter but returns `void`.
|
|
56
|
+
* Useful as a shorthand in arrow functions: `() => state.set(value)` instead of `() => { state.value = value }`
|
|
57
|
+
*
|
|
58
|
+
* `state.set` cannot be used as a standalone function: `const set = state.set`
|
|
59
|
+
*/
|
|
60
|
+
set(value) {
|
|
61
|
+
this.value = value;
|
|
62
|
+
}
|
|
53
63
|
/**
|
|
54
64
|
* Force triggers all watchers even if value didn't change.
|
|
55
65
|
*
|
package/index.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var WatchState=function(e){"use strict";const t={activeWatcher:void 0,eventDeep:0};function s(e){const{activeWatcher:s}=t;s&&(s.childrenObservers.add(e),s.destructors.add((()=>{s.childrenObservers.delete(e)})))}const r=[];let c;function n(e){const t=Boolean(r.length);if(r.push(e),!t)for(;c=r.shift();){c.childrenObservers.forEach((e=>{r.push(e)}));for(const e of c.destructors)c.destructors.delete(e),e();c.destroyed=!0}}const i=[];let a,o,h,u;function d(e){const t=Boolean(i.length);if(i.push(e),!t)for(;a=i.shift();){a.childrenObservers.forEach(n);for(const e of a.destructors)a.destructors.delete(e),e()}}function l(e,s){const r=t.activeWatcher;t.activeWatcher=e,s(),t.activeWatcher=r}class v{constructor(){this.observers=new Set}get value(){const{activeWatcher:e}=t;return e&&(this.observers.add(e),e.destructors.add((()=>{this.observers.delete(e)}))),this.rawValue}}function f(e){if(!e.size)return;const t=e.values().next().value;return e.delete(t),t}const p=new Set,b=new Set;function w(){if(!u){for(u=!0;(o=f(p))||(h=f(b));)o?o.invalid=!0:(d(h),h.update());u=!1}}function W(e){const s=!t.eventDeep&&!b.size&&!p.size,r=[...b];b.clear(),e.forEach((e=>{b.add(e),e instanceof D&&p.add(e)})),r.forEach((e=>b.add(e))),s&&w()}const y=[];let O;function S(e){const t=y.length;if(y.push(e),!t)for(;O=y.shift();)O instanceof D&&(y.push(...O.observers),O.invalid=!0)}class D extends v{get childWatchers(){return this.childrenObservers}constructor(e,t,r){super(),this.invalid=!0,this.updated=!1,this.destroyed=!1,this.isCache=!0,this.destructors=new Set,this.childrenObservers=new Set,this.watcher=e,t||s(this),r&&this.forceUpdate()}update(){S(this);const e=[...this.observers];let t;for(;t=e.pop();){if(!(t instanceof D))return this.forceUpdate();e.push(...t.observers)}}forceUpdate(){this.destroyed||(this.invalid=!1,l(this,(()=>{const e=this.watcher(!!this.updated&&(this.updated=!0));e!==this.rawValue&&(this.rawValue=e,W(this.observers))})))}get value(){return this.invalid&&this.forceUpdate(),this.destroyed?this.rawValue:super.value}destroy(){n(this)}}function g(e){const{activeWatcher:s}=t;t.activeWatcher=void 0;const r=e();return t.activeWatcher=s,r}return e.Cache=class extends D{},e.Compute=D,e.Observable=v,e.State=class extends v{constructor(e){super(),this.rawValue=e}get value(){return super.value}set value(e){this.rawValue!==e&&(this.rawValue=e,this.update())}update(){W(this.observers)}},e.Watch=class{get childWatchers(){return this.childrenObservers}constructor(e,t,r){this.watcher=e,this.destroyed=!1,this.destructors=new Set,this.childrenObservers=new Set,t||s(this),r||l(this,(()=>{e(!1)}))}destroy(){n(this)}update(){this.destroyed||l(this,(()=>{this.watcher(!0)}))}},e.bindObserver=s,e.callEvent=function(e){const s=g((()=>{t.eventDeep++;const s=e();return t.eventDeep--,s}));return t.eventDeep||w(),s},e.clearWatcher=d,e.createEvent=function(e){return function(...s){const r=g((()=>{t.eventDeep++;const r=e.apply(this,s);return t.eventDeep--,r}));return t.eventDeep||w(),r}},e.destroyWatchers=n,e.forceQueueWatchers=w,e.invalidateCache=function(e){S(e)},e.invalidateCompute=S,e.onDestroy=function(e){t.activeWatcher&&t.activeWatcher.destructors.add(e)},e.queueWatchers=W,e.scope=t,e.shiftSet=f,e.unwatch=g,e.watchWithScope=l,Object.defineProperty(e,"__esModule",{value:!0}),e}({});
|
|
1
|
+
var WatchState=function(e){"use strict";const t={activeWatcher:void 0,eventDeep:0};function s(e){const{activeWatcher:s}=t;s&&(s.childrenObservers.add(e),s.destructors.add((()=>{s.childrenObservers.delete(e)})))}const r=[];let c;function n(e){const t=Boolean(r.length);if(r.push(e),!t)for(;c=r.shift();){c.childrenObservers.forEach((e=>{r.push(e)}));for(const e of c.destructors)c.destructors.delete(e),e();c.destroyed=!0}}const i=[];let a,o,h,u;function d(e){const t=Boolean(i.length);if(i.push(e),!t)for(;a=i.shift();){a.childrenObservers.forEach(n);for(const e of a.destructors)a.destructors.delete(e),e()}}function l(e,s){const r=t.activeWatcher;t.activeWatcher=e,s(),t.activeWatcher=r}class v{constructor(){this.observers=new Set}get value(){const{activeWatcher:e}=t;return e&&(this.observers.add(e),e.destructors.add((()=>{this.observers.delete(e)}))),this.rawValue}}function f(e){if(!e.size)return;const t=e.values().next().value;return e.delete(t),t}const p=new Set,b=new Set;function w(){if(!u){for(u=!0;(o=f(p))||(h=f(b));)o?o.invalid=!0:(d(h),h.update());u=!1}}function W(e){const s=!t.eventDeep&&!b.size&&!p.size,r=[...b];b.clear(),e.forEach((e=>{b.add(e),e instanceof D&&p.add(e)})),r.forEach((e=>b.add(e))),s&&w()}const y=[];let O;function S(e){const t=y.length;if(y.push(e),!t)for(;O=y.shift();)O instanceof D&&(y.push(...O.observers),O.invalid=!0)}class D extends v{get childWatchers(){return this.childrenObservers}constructor(e,t,r){super(),this.invalid=!0,this.updated=!1,this.destroyed=!1,this.isCache=!0,this.destructors=new Set,this.childrenObservers=new Set,this.watcher=e,t||s(this),r&&this.forceUpdate()}update(){S(this);const e=[...this.observers];let t;for(;t=e.pop();){if(!(t instanceof D))return this.forceUpdate();e.push(...t.observers)}}forceUpdate(){this.destroyed||(this.invalid=!1,l(this,(()=>{const e=this.watcher(!!this.updated&&(this.updated=!0));e!==this.rawValue&&(this.rawValue=e,W(this.observers))})))}get value(){return this.invalid&&this.forceUpdate(),this.destroyed?this.rawValue:super.value}destroy(){n(this)}}function g(e){const{activeWatcher:s}=t;t.activeWatcher=void 0;const r=e();return t.activeWatcher=s,r}return e.Cache=class extends D{},e.Compute=D,e.Observable=v,e.State=class extends v{constructor(e){super(),this.rawValue=e}get value(){return super.value}set value(e){this.rawValue!==e&&(this.rawValue=e,this.update())}set(e){this.value=e}update(){W(this.observers)}},e.Watch=class{get childWatchers(){return this.childrenObservers}constructor(e,t,r){this.watcher=e,this.destroyed=!1,this.destructors=new Set,this.childrenObservers=new Set,t||s(this),r||l(this,(()=>{e(!1)}))}destroy(){n(this)}update(){this.destroyed||l(this,(()=>{this.watcher(!0)}))}},e.bindObserver=s,e.callEvent=function(e){const s=g((()=>{t.eventDeep++;const s=e();return t.eventDeep--,s}));return t.eventDeep||w(),s},e.clearWatcher=d,e.createEvent=function(e){return function(...s){const r=g((()=>{t.eventDeep++;const r=e.apply(this,s);return t.eventDeep--,r}));return t.eventDeep||w(),r}},e.destroyWatchers=n,e.forceQueueWatchers=w,e.invalidateCache=function(e){S(e)},e.invalidateCompute=S,e.onDestroy=function(e){t.activeWatcher&&t.activeWatcher.destructors.add(e)},e.queueWatchers=W,e.scope=t,e.shiftSet=f,e.unwatch=g,e.watchWithScope=l,Object.defineProperty(e,"__esModule",{value:!0}),e}({});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "watch-state",
|
|
3
|
-
"version": "3.5.
|
|
3
|
+
"version": "3.5.2",
|
|
4
4
|
"description": "CANT inc. state management system.",
|
|
5
5
|
"author": "Mikhail Lysikov <d8corp@mail.ru>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"import": {
|
|
52
52
|
"./release/index.es6.js": "*"
|
|
53
53
|
},
|
|
54
|
-
"limit": "1.
|
|
54
|
+
"limit": "1.07 kB"
|
|
55
55
|
},
|
|
56
56
|
{
|
|
57
57
|
"name": "Core",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"import": {
|
|
60
60
|
"./release/index.es6.js": "{ Watch, State, Compute, createEvent }"
|
|
61
61
|
},
|
|
62
|
-
"limit": "
|
|
62
|
+
"limit": "850 B"
|
|
63
63
|
}
|
|
64
64
|
]
|
|
65
65
|
}
|