@esportsplus/reactivity 0.0.22 → 0.0.24
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/build/api/effect.d.ts +2 -0
- package/build/api/effect.js +3 -0
- package/build/api/index.d.ts +4 -0
- package/build/api/index.js +4 -0
- package/build/api/macro.d.ts +4 -0
- package/build/api/macro.js +8 -0
- package/build/api/promise.d.ts +3 -0
- package/build/api/promise.js +38 -0
- package/build/api/reactive.d.ts +16 -0
- package/build/api/reactive.js +27 -0
- package/build/context/index.d.ts +5 -0
- package/build/context/index.js +3 -0
- package/build/context/node.d.ts +7 -0
- package/build/context/node.js +21 -0
- package/build/context/nodes.d.ts +7 -0
- package/build/context/nodes.js +33 -0
- package/build/index.d.ts +3 -12
- package/build/index.js +3 -4
- package/build/signal.d.ts +31 -0
- package/build/signal.js +256 -0
- package/build/symbols.d.ts +10 -1
- package/build/symbols.js +10 -1
- package/build/types.d.ts +33 -13
- package/build/types.js +2 -1
- package/package.json +2 -2
- package/readme.md +2 -0
- package/src/api/effect.ts +5 -0
- package/src/api/index.ts +4 -0
- package/src/api/macro.ts +15 -0
- package/src/api/promise.ts +45 -0
- package/src/api/reactive.ts +42 -0
- package/src/context/index.ts +5 -0
- package/src/context/node.ts +36 -0
- package/src/context/nodes.ts +52 -0
- package/src/index.ts +3 -6
- package/src/signal.ts +344 -0
- package/src/symbols.ts +22 -1
- package/src/types.ts +40 -13
- package/tsconfig.json +1 -1
- package/build/index.browser.js +0 -277
- package/build/methods/effect.d.ts +0 -2
- package/build/methods/effect.js +0 -4
- package/build/methods/index.d.ts +0 -3
- package/build/methods/index.js +0 -3
- package/build/methods/reactive.d.ts +0 -3
- package/build/methods/reactive.js +0 -52
- package/build/reactive.d.ts +0 -22
- package/build/reactive.js +0 -190
- package/src/methods/effect.ts +0 -6
- package/src/methods/index.ts +0 -5
- package/src/methods/reactive.ts +0 -71
- package/src/reactive.ts +0 -266
package/src/reactive.ts
DELETED
|
@@ -1,266 +0,0 @@
|
|
|
1
|
-
import { CLEAN, CHECK, DIRTY } from './symbols';
|
|
2
|
-
import { ReactiveFn, Scheduler, State } from './types';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
let index = 0,
|
|
6
|
-
reaction: Reactive<any> | null = null,
|
|
7
|
-
queue: Reactive<any>[] = [],
|
|
8
|
-
scheduled = false,
|
|
9
|
-
schedulers = new Set<Scheduler>,
|
|
10
|
-
stack: Reactive<any>[] | null = null;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
function mark(instances: Reactive<any>[] | null, state: typeof CHECK | typeof DIRTY) {
|
|
14
|
-
if (!instances) {
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
for (let i = 0, n = instances.length; i < n; i++) {
|
|
19
|
-
let instance = instances[i];
|
|
20
|
-
|
|
21
|
-
if (instance.state < state) {
|
|
22
|
-
// If previous state was clean we need to update effects
|
|
23
|
-
if (instance.effect && instance.state === CLEAN) {
|
|
24
|
-
queue.push(instance);
|
|
25
|
-
|
|
26
|
-
if (!scheduled) {
|
|
27
|
-
schedule();
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
instance.state = state;
|
|
32
|
-
|
|
33
|
-
if (instance.observers) {
|
|
34
|
-
mark(instance.observers, CHECK);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function schedule() {
|
|
41
|
-
if (scheduled) {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
for (let scheduler of schedulers) {
|
|
46
|
-
scheduler.schedule();
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
scheduled = true;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async function task() {
|
|
53
|
-
let n = queue.length;
|
|
54
|
-
|
|
55
|
-
for (let i = 0; i < n; i++) {
|
|
56
|
-
await queue[i].get();
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
queue.splice(0, n);
|
|
60
|
-
scheduled = false;
|
|
61
|
-
|
|
62
|
-
if (queue.length) {
|
|
63
|
-
schedule();
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
class Reactive<T> {
|
|
69
|
-
private fn?: ReactiveFn<T>;
|
|
70
|
-
private value: T;
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
effect: boolean;
|
|
74
|
-
cleanup: ((old: T) => void)[] | null = null;
|
|
75
|
-
observers: Reactive<any>[] | null = null;
|
|
76
|
-
sources: Reactive<any>[] | null = null;
|
|
77
|
-
state: State;
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
constructor(data: ReactiveFn<T> | T, effect: boolean = false) {
|
|
81
|
-
this.effect = effect;
|
|
82
|
-
|
|
83
|
-
if (typeof data === 'function') {
|
|
84
|
-
this.fn = data as ReactiveFn<T>;
|
|
85
|
-
this.state = DIRTY;
|
|
86
|
-
this.value = undefined as any;
|
|
87
|
-
|
|
88
|
-
if (effect) {
|
|
89
|
-
this.update();
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
else {
|
|
93
|
-
this.state = CLEAN;
|
|
94
|
-
this.value = data;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
get() {
|
|
100
|
-
if (reaction) {
|
|
101
|
-
if (!stack && reaction.sources && reaction.sources[index] == this) {
|
|
102
|
-
index++;
|
|
103
|
-
}
|
|
104
|
-
else {
|
|
105
|
-
if (!stack) {
|
|
106
|
-
stack = [this];
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
stack.push(this);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if (this.fn) {
|
|
115
|
-
this.sync();
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return this.value as T extends (...args: any[]) => any ? ReturnType<T> : T;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
set(value: T) {
|
|
122
|
-
if (this.value !== value) {
|
|
123
|
-
mark(this.observers, DIRTY);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
this.value = value;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// We don't actually delete sources here because we're replacing the entire array soon
|
|
130
|
-
private removeParentObservers() {
|
|
131
|
-
if (!this.sources) {
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
for (let i = index; i < this.sources.length; i++) {
|
|
136
|
-
let source = this.sources[i];
|
|
137
|
-
|
|
138
|
-
source.observers![ source.observers!.findIndex((v) => v === this) ] = source.observers![source.observers!.length - 1];
|
|
139
|
-
source.observers!.pop();
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// Update if dirty or if a parent is dirty
|
|
144
|
-
private sync() {
|
|
145
|
-
// If we are potentially dirty, see if we have a parent who has actually changed value
|
|
146
|
-
if (this.state === CHECK && this.sources) {
|
|
147
|
-
for (let i = 0, n = this.sources.length; i < n; i++) {
|
|
148
|
-
this.sources[i].sync();
|
|
149
|
-
|
|
150
|
-
// Stop the loop here so we won't trigger updates on other parents unnecessarily
|
|
151
|
-
// If our computation changes to no longer use some sources, we don't
|
|
152
|
-
// want to update() a source we used last time, but now don't use.
|
|
153
|
-
if ((this.state as State) === DIRTY) {
|
|
154
|
-
break;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// If we were already dirty or marked dirty by the step above, update.
|
|
160
|
-
if (this.state === DIRTY) {
|
|
161
|
-
this.update();
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// By now, we're clean
|
|
165
|
-
this.state = CLEAN;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
private update() {
|
|
169
|
-
let previous = {
|
|
170
|
-
index: index,
|
|
171
|
-
reaction: reaction,
|
|
172
|
-
stack: stack,
|
|
173
|
-
value: this.value
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
index = 0;
|
|
177
|
-
reaction = this;
|
|
178
|
-
stack = [];
|
|
179
|
-
|
|
180
|
-
try {
|
|
181
|
-
if (this.cleanup) {
|
|
182
|
-
for (let i = 0, n = this.cleanup.length; i < n; i++) {
|
|
183
|
-
this.cleanup[i]( this.value );
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
this.cleanup.length = 0;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
this.value = this.fn!(
|
|
190
|
-
(fn: VoidFunction) => {
|
|
191
|
-
if (!this.cleanup) {
|
|
192
|
-
this.cleanup = [fn];
|
|
193
|
-
}
|
|
194
|
-
else {
|
|
195
|
-
this.cleanup.push(fn);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
);
|
|
199
|
-
|
|
200
|
-
// If sources have changed, update source & observer links
|
|
201
|
-
if (stack.length) {
|
|
202
|
-
// Remove all old sources' observers links to us
|
|
203
|
-
this.removeParentObservers();
|
|
204
|
-
|
|
205
|
-
// Update source up links
|
|
206
|
-
if (this.sources && index > 0) {
|
|
207
|
-
this.sources.length = index + stack.length;
|
|
208
|
-
|
|
209
|
-
for (let i = 0; i < stack.length; i++) {
|
|
210
|
-
this.sources[index + i] = stack[i];
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
else {
|
|
214
|
-
this.sources = stack;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// Add ourselves to the end of the parent observers array
|
|
218
|
-
for (let i = index; i < this.sources.length; i++) {
|
|
219
|
-
let source = this.sources[i];
|
|
220
|
-
|
|
221
|
-
if (!source.observers) {
|
|
222
|
-
source.observers = [this];
|
|
223
|
-
}
|
|
224
|
-
else {
|
|
225
|
-
source.observers.push( this );
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
// Remove all old sources' observers links to us
|
|
230
|
-
else if (this.sources && index < this.sources.length) {
|
|
231
|
-
this.removeParentObservers();
|
|
232
|
-
this.sources.length = index;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
finally {
|
|
236
|
-
index = previous.index;
|
|
237
|
-
reaction = previous.reaction;
|
|
238
|
-
stack = previous.stack;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
// Handle diamond depenendencies if we're the parent of a diamond.
|
|
242
|
-
if (this.observers && previous.value !== this.value) {
|
|
243
|
-
for (let i = 0; i < this.observers.length; i++) {
|
|
244
|
-
this.observers[i].state = DIRTY;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
this.state = CLEAN;
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
const scheduler = {
|
|
254
|
-
add: (scheduler: Scheduler) => {
|
|
255
|
-
scheduler.tasks.add(task);
|
|
256
|
-
schedulers.add(scheduler);
|
|
257
|
-
},
|
|
258
|
-
delete: (scheduler: Scheduler) => {
|
|
259
|
-
scheduler.tasks.delete(task);
|
|
260
|
-
schedulers.delete(scheduler);
|
|
261
|
-
}
|
|
262
|
-
};
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
export default Reactive;
|
|
266
|
-
export { scheduler };
|