watch-state 3.5.2 → 3.6.1
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/Compute/Compute.d.ts +11 -4
- package/Compute/Compute.es6.js +19 -8
- package/Compute/Compute.js +19 -8
- package/Observable/Observable.d.ts +5 -1
- package/Observable/Observable.es6.js +9 -1
- package/Observable/Observable.js +9 -1
- package/README.md +218 -189
- package/State/State.d.ts +32 -6
- package/State/State.es6.js +19 -4
- package/State/State.js +19 -4
- package/Watch/Watch.d.ts +12 -4
- package/Watch/Watch.es6.js +20 -9
- package/Watch/Watch.js +20 -9
- package/helpers/bindObserver/bindObserver.es6.js +2 -2
- package/helpers/bindObserver/bindObserver.js +2 -2
- package/helpers/clearWatcher/clearWatcher.es6.js +1 -1
- package/helpers/clearWatcher/clearWatcher.js +1 -1
- package/helpers/destroyWatchers/destroyWatchers.es6.js +1 -1
- package/helpers/destroyWatchers/destroyWatchers.js +1 -1
- package/index.min.js +1 -1
- package/package.json +4 -4
- package/types.d.ts +12 -2
- package/utils/createEvent/createEvent.es6.js +1 -0
- package/utils/createEvent/createEvent.js +1 -0
package/Compute/Compute.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Observable } from '../Observable';
|
|
2
|
-
import type { Destructor, Observer, Watcher } from '../types';
|
|
2
|
+
import type { Destructor, Observer, Reaction, Watcher } from '../types';
|
|
3
3
|
export declare function forceQueueWatchers(): void;
|
|
4
4
|
export declare function queueWatchers(observers: Set<Observer>): void;
|
|
5
5
|
export declare function invalidateCompute(observer: Observer): void;
|
|
@@ -41,10 +41,12 @@ export declare function invalidateCompute(observer: Observer): void;
|
|
|
41
41
|
* // Triggers part of chain: fullName → name
|
|
42
42
|
*/
|
|
43
43
|
export declare class Compute<V = unknown> extends Observable<V> implements Observer {
|
|
44
|
+
readonly reaction: Watcher<V> | Reaction<V>;
|
|
44
45
|
/** Indicates if computed value is stale and needs recalculation. */
|
|
45
46
|
invalid: boolean;
|
|
46
47
|
/** Tracks if the computation has run at least once. */
|
|
47
48
|
updated: boolean;
|
|
49
|
+
raw: V;
|
|
48
50
|
/**
|
|
49
51
|
* Indicates if observer has been destroyed.
|
|
50
52
|
* Prevents accidental use after cleanup.
|
|
@@ -55,11 +57,16 @@ export declare class Compute<V = unknown> extends Observable<V> implements Obser
|
|
|
55
57
|
/** Cleanup functions to run on destroy (e.g., unsubscribes). */
|
|
56
58
|
readonly destructors: Set<Destructor>;
|
|
57
59
|
/** Child watchers created within this watcher's scope */
|
|
58
|
-
readonly
|
|
60
|
+
readonly children: Set<Observer>;
|
|
61
|
+
/** @deprecated Use `children` */
|
|
62
|
+
get childrenObservers(): Set<Observer>;
|
|
59
63
|
/** @deprecated Use `childrenObservers` */
|
|
60
64
|
get childWatchers(): Set<Observer>;
|
|
61
|
-
|
|
62
|
-
|
|
65
|
+
/** @deprecated Use `reaction` */
|
|
66
|
+
get watcher(): Watcher<V> | Reaction<V>;
|
|
67
|
+
constructor(reaction: Reaction<V>, freeParent?: boolean, fireImmediately?: boolean);
|
|
68
|
+
/** @deprecated `update` argument is deprecated, use `Reaction` */
|
|
69
|
+
constructor(reaction: Watcher<V>, freeParent?: boolean, fireImmediately?: boolean);
|
|
63
70
|
/** Mark computation as invalid and trigger propagation to parent observers. */
|
|
64
71
|
update(): void;
|
|
65
72
|
forceUpdate(): void;
|
package/Compute/Compute.es6.js
CHANGED
|
@@ -101,13 +101,24 @@ function invalidateCompute(observer) {
|
|
|
101
101
|
* // Triggers part of chain: fullName → name
|
|
102
102
|
*/
|
|
103
103
|
class Compute extends Observable {
|
|
104
|
+
// TODO: remove in major release
|
|
105
|
+
/** @deprecated Use `children` */
|
|
106
|
+
get childrenObservers() {
|
|
107
|
+
return this.children;
|
|
108
|
+
}
|
|
104
109
|
// TODO: remove in major release
|
|
105
110
|
/** @deprecated Use `childrenObservers` */
|
|
106
111
|
get childWatchers() {
|
|
107
|
-
return this.
|
|
112
|
+
return this.children;
|
|
113
|
+
}
|
|
114
|
+
// TODO: remove in major release
|
|
115
|
+
/** @deprecated Use `reaction` */
|
|
116
|
+
get watcher() {
|
|
117
|
+
return this.reaction;
|
|
108
118
|
}
|
|
109
|
-
constructor(
|
|
119
|
+
constructor(reaction, freeParent, fireImmediately) {
|
|
110
120
|
super();
|
|
121
|
+
this.reaction = reaction;
|
|
111
122
|
/** Indicates if computed value is stale and needs recalculation. */
|
|
112
123
|
this.invalid = true;
|
|
113
124
|
/** Tracks if the computation has run at least once. */
|
|
@@ -123,8 +134,7 @@ class Compute extends Observable {
|
|
|
123
134
|
/** Cleanup functions to run on destroy (e.g., unsubscribes). */
|
|
124
135
|
this.destructors = new Set();
|
|
125
136
|
/** Child watchers created within this watcher's scope */
|
|
126
|
-
this.
|
|
127
|
-
this.watcher = watcher;
|
|
137
|
+
this.children = new Set();
|
|
128
138
|
if (!freeParent) {
|
|
129
139
|
bindObserver(this);
|
|
130
140
|
}
|
|
@@ -148,9 +158,10 @@ class Compute extends Observable {
|
|
|
148
158
|
if (!this.destroyed) {
|
|
149
159
|
this.invalid = false;
|
|
150
160
|
watchWithScope(this, () => {
|
|
151
|
-
const newValue = this.
|
|
152
|
-
|
|
153
|
-
|
|
161
|
+
const newValue = this.reaction(this.updated); // TODO: remove `this.updated` in major release
|
|
162
|
+
this.updated = true;
|
|
163
|
+
if (newValue !== this.raw) {
|
|
164
|
+
this.raw = newValue;
|
|
154
165
|
queueWatchers(this.observers);
|
|
155
166
|
}
|
|
156
167
|
});
|
|
@@ -173,7 +184,7 @@ class Compute extends Observable {
|
|
|
173
184
|
if (this.invalid) {
|
|
174
185
|
this.forceUpdate();
|
|
175
186
|
}
|
|
176
|
-
return this.destroyed ? this.
|
|
187
|
+
return this.destroyed ? this.raw : super.value;
|
|
177
188
|
}
|
|
178
189
|
/** Stop observation and remove all dependencies. */
|
|
179
190
|
destroy() {
|
package/Compute/Compute.js
CHANGED
|
@@ -105,13 +105,24 @@ function invalidateCompute(observer) {
|
|
|
105
105
|
* // Triggers part of chain: fullName → name
|
|
106
106
|
*/
|
|
107
107
|
class Compute extends Observable.Observable {
|
|
108
|
+
// TODO: remove in major release
|
|
109
|
+
/** @deprecated Use `children` */
|
|
110
|
+
get childrenObservers() {
|
|
111
|
+
return this.children;
|
|
112
|
+
}
|
|
108
113
|
// TODO: remove in major release
|
|
109
114
|
/** @deprecated Use `childrenObservers` */
|
|
110
115
|
get childWatchers() {
|
|
111
|
-
return this.
|
|
116
|
+
return this.children;
|
|
117
|
+
}
|
|
118
|
+
// TODO: remove in major release
|
|
119
|
+
/** @deprecated Use `reaction` */
|
|
120
|
+
get watcher() {
|
|
121
|
+
return this.reaction;
|
|
112
122
|
}
|
|
113
|
-
constructor(
|
|
123
|
+
constructor(reaction, freeParent, fireImmediately) {
|
|
114
124
|
super();
|
|
125
|
+
this.reaction = reaction;
|
|
115
126
|
/** Indicates if computed value is stale and needs recalculation. */
|
|
116
127
|
this.invalid = true;
|
|
117
128
|
/** Tracks if the computation has run at least once. */
|
|
@@ -127,8 +138,7 @@ class Compute extends Observable.Observable {
|
|
|
127
138
|
/** Cleanup functions to run on destroy (e.g., unsubscribes). */
|
|
128
139
|
this.destructors = new Set();
|
|
129
140
|
/** Child watchers created within this watcher's scope */
|
|
130
|
-
this.
|
|
131
|
-
this.watcher = watcher;
|
|
141
|
+
this.children = new Set();
|
|
132
142
|
if (!freeParent) {
|
|
133
143
|
bindObserver.bindObserver(this);
|
|
134
144
|
}
|
|
@@ -152,9 +162,10 @@ class Compute extends Observable.Observable {
|
|
|
152
162
|
if (!this.destroyed) {
|
|
153
163
|
this.invalid = false;
|
|
154
164
|
watchWithScope.watchWithScope(this, () => {
|
|
155
|
-
const newValue = this.
|
|
156
|
-
|
|
157
|
-
|
|
165
|
+
const newValue = this.reaction(this.updated); // TODO: remove `this.updated` in major release
|
|
166
|
+
this.updated = true;
|
|
167
|
+
if (newValue !== this.raw) {
|
|
168
|
+
this.raw = newValue;
|
|
158
169
|
queueWatchers(this.observers);
|
|
159
170
|
}
|
|
160
171
|
});
|
|
@@ -177,7 +188,7 @@ class Compute extends Observable.Observable {
|
|
|
177
188
|
if (this.invalid) {
|
|
178
189
|
this.forceUpdate();
|
|
179
190
|
}
|
|
180
|
-
return this.destroyed ? this.
|
|
191
|
+
return this.destroyed ? this.raw : super.value;
|
|
181
192
|
}
|
|
182
193
|
/** Stop observation and remove all dependencies. */
|
|
183
194
|
destroy() {
|
|
@@ -11,7 +11,11 @@ export declare abstract class Observable<V> {
|
|
|
11
11
|
/** Set of registered observers */
|
|
12
12
|
readonly observers: Set<Observer>;
|
|
13
13
|
/** Raw value. No auto-subscription on direct access (unlike `value`). */
|
|
14
|
-
|
|
14
|
+
abstract raw: V;
|
|
15
|
+
/** @deprecated Use raw field */
|
|
16
|
+
get rawValue(): V;
|
|
17
|
+
/** @deprecated Use raw field */
|
|
18
|
+
set rawValue(raw: V);
|
|
15
19
|
/**
|
|
16
20
|
* Current value with automatic subscription.
|
|
17
21
|
*
|
|
@@ -13,6 +13,14 @@ class Observable {
|
|
|
13
13
|
/** Set of registered observers */
|
|
14
14
|
this.observers = new Set();
|
|
15
15
|
}
|
|
16
|
+
/** @deprecated Use raw field */
|
|
17
|
+
get rawValue() {
|
|
18
|
+
return this.raw;
|
|
19
|
+
}
|
|
20
|
+
/** @deprecated Use raw field */
|
|
21
|
+
set rawValue(raw) {
|
|
22
|
+
this.raw = raw;
|
|
23
|
+
}
|
|
16
24
|
/**
|
|
17
25
|
* Current value with automatic subscription.
|
|
18
26
|
*
|
|
@@ -29,7 +37,7 @@ class Observable {
|
|
|
29
37
|
this.observers.delete(activeWatcher);
|
|
30
38
|
});
|
|
31
39
|
}
|
|
32
|
-
return this.
|
|
40
|
+
return this.raw;
|
|
33
41
|
}
|
|
34
42
|
}
|
|
35
43
|
|
package/Observable/Observable.js
CHANGED
|
@@ -17,6 +17,14 @@ class Observable {
|
|
|
17
17
|
/** Set of registered observers */
|
|
18
18
|
this.observers = new Set();
|
|
19
19
|
}
|
|
20
|
+
/** @deprecated Use raw field */
|
|
21
|
+
get rawValue() {
|
|
22
|
+
return this.raw;
|
|
23
|
+
}
|
|
24
|
+
/** @deprecated Use raw field */
|
|
25
|
+
set rawValue(raw) {
|
|
26
|
+
this.raw = raw;
|
|
27
|
+
}
|
|
20
28
|
/**
|
|
21
29
|
* Current value with automatic subscription.
|
|
22
30
|
*
|
|
@@ -33,7 +41,7 @@ class Observable {
|
|
|
33
41
|
this.observers.delete(activeWatcher);
|
|
34
42
|
});
|
|
35
43
|
}
|
|
36
|
-
return this.
|
|
44
|
+
return this.raw;
|
|
37
45
|
}
|
|
38
46
|
}
|
|
39
47
|
|