@esportsplus/reactivity 0.0.22 → 0.0.23
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/core.d.ts +30 -0
- package/build/core.js +218 -0
- package/build/index.d.ts +3 -12
- package/build/index.js +3 -4
- package/build/primitives/closure.d.ts +3 -0
- package/build/primitives/closure.js +10 -0
- package/build/primitives/computed.d.ts +3 -0
- package/build/primitives/computed.js +11 -0
- package/build/primitives/dispatch.d.ts +3 -0
- package/build/primitives/dispatch.js +13 -0
- package/build/primitives/dispose.d.ts +3 -0
- package/build/primitives/dispose.js +13 -0
- package/build/primitives/effect.d.ts +7 -0
- package/build/primitives/effect.js +9 -0
- package/build/primitives/index.d.ts +8 -0
- package/build/primitives/index.js +8 -0
- package/build/primitives/reactive.d.ts +3 -0
- package/build/primitives/reactive.js +35 -0
- package/build/primitives/reset.d.ts +3 -0
- package/build/primitives/reset.js +13 -0
- package/build/primitives/signal.d.ts +8 -0
- package/build/primitives/signal.js +14 -0
- package/build/signal.d.ts +16 -0
- package/build/signal.js +22 -0
- package/build/symbols.d.ts +10 -1
- package/build/symbols.js +10 -1
- package/build/types.d.ts +27 -13
- package/build/types.js +1 -1
- package/package.json +1 -1
- package/readme.md +2 -0
- package/src/core.ts +295 -0
- package/src/index.ts +3 -6
- package/src/primitives/closure.ts +15 -0
- package/src/primitives/computed.ts +16 -0
- package/src/primitives/dispatch.ts +17 -0
- package/src/primitives/dispose.ts +17 -0
- package/src/primitives/effect.ts +13 -0
- package/src/primitives/index.ts +8 -0
- package/src/primitives/reactive.ts +43 -0
- package/src/primitives/reset.ts +17 -0
- package/src/primitives/signal.ts +18 -0
- package/src/signal.ts +30 -0
- package/src/symbols.ts +22 -1
- package/src/types.ts +34 -13
- package/build/index.browser.js +0 -277
- 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/build/index.browser.js
DELETED
|
@@ -1,277 +0,0 @@
|
|
|
1
|
-
/******/ (() => { // webpackBootstrap
|
|
2
|
-
/******/ "use strict";
|
|
3
|
-
var __webpack_exports__ = {};
|
|
4
|
-
|
|
5
|
-
// UNUSED EXPORTS: default, effect, reactive, scheduler
|
|
6
|
-
|
|
7
|
-
;// CONCATENATED MODULE: ./src/symbols.ts
|
|
8
|
-
const CLEAN = 0;
|
|
9
|
-
const CHECK = 1;
|
|
10
|
-
const DIRTY = 2;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
;// CONCATENATED MODULE: ./src/reactive.ts
|
|
14
|
-
|
|
15
|
-
let index = 0, reaction = null, queue = [], scheduled = false, schedulers = new Set, stack = null;
|
|
16
|
-
function mark(instances, state) {
|
|
17
|
-
if (!instances) {
|
|
18
|
-
return;
|
|
19
|
-
}
|
|
20
|
-
for (let i = 0, n = instances.length; i < n; i++) {
|
|
21
|
-
let instance = instances[i];
|
|
22
|
-
if (instance.state < state) {
|
|
23
|
-
if (instance.effect && instance.state === CLEAN) {
|
|
24
|
-
queue.push(instance);
|
|
25
|
-
if (!scheduled) {
|
|
26
|
-
schedule();
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
instance.state = state;
|
|
30
|
-
if (instance.observers) {
|
|
31
|
-
mark(instance.observers, CHECK);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
function schedule() {
|
|
37
|
-
if (scheduled) {
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
for (let scheduler of schedulers) {
|
|
41
|
-
scheduler.schedule();
|
|
42
|
-
}
|
|
43
|
-
scheduled = true;
|
|
44
|
-
}
|
|
45
|
-
async function task() {
|
|
46
|
-
let n = queue.length;
|
|
47
|
-
for (let i = 0; i < n; i++) {
|
|
48
|
-
await queue[i].get();
|
|
49
|
-
}
|
|
50
|
-
queue.splice(0, n);
|
|
51
|
-
scheduled = false;
|
|
52
|
-
if (queue.length) {
|
|
53
|
-
schedule();
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
class Reactive {
|
|
57
|
-
fn;
|
|
58
|
-
value;
|
|
59
|
-
effect;
|
|
60
|
-
cleanup = null;
|
|
61
|
-
observers = null;
|
|
62
|
-
sources = null;
|
|
63
|
-
state;
|
|
64
|
-
constructor(data, effect = false) {
|
|
65
|
-
this.effect = effect;
|
|
66
|
-
if (typeof data === 'function') {
|
|
67
|
-
this.fn = data;
|
|
68
|
-
this.state = DIRTY;
|
|
69
|
-
this.value = undefined;
|
|
70
|
-
if (effect) {
|
|
71
|
-
this.update();
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
else {
|
|
75
|
-
this.state = CLEAN;
|
|
76
|
-
this.value = data;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
get() {
|
|
80
|
-
if (reaction) {
|
|
81
|
-
if (!stack && reaction.sources && reaction.sources[index] == this) {
|
|
82
|
-
index++;
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
if (!stack) {
|
|
86
|
-
stack = [this];
|
|
87
|
-
}
|
|
88
|
-
else {
|
|
89
|
-
stack.push(this);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
if (this.fn) {
|
|
94
|
-
this.sync();
|
|
95
|
-
}
|
|
96
|
-
return this.value;
|
|
97
|
-
}
|
|
98
|
-
set(value) {
|
|
99
|
-
if (this.value !== value) {
|
|
100
|
-
mark(this.observers, DIRTY);
|
|
101
|
-
}
|
|
102
|
-
this.value = value;
|
|
103
|
-
}
|
|
104
|
-
removeParentObservers() {
|
|
105
|
-
if (!this.sources) {
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
for (let i = index; i < this.sources.length; i++) {
|
|
109
|
-
let source = this.sources[i];
|
|
110
|
-
source.observers[source.observers.findIndex((v) => v === this)] = source.observers[source.observers.length - 1];
|
|
111
|
-
source.observers.pop();
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
sync() {
|
|
115
|
-
if (this.state === CHECK && this.sources) {
|
|
116
|
-
for (let i = 0, n = this.sources.length; i < n; i++) {
|
|
117
|
-
this.sources[i].sync();
|
|
118
|
-
if (this.state === DIRTY) {
|
|
119
|
-
break;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
if (this.state === DIRTY) {
|
|
124
|
-
this.update();
|
|
125
|
-
}
|
|
126
|
-
this.state = CLEAN;
|
|
127
|
-
}
|
|
128
|
-
update() {
|
|
129
|
-
let previous = {
|
|
130
|
-
index: index,
|
|
131
|
-
reaction: reaction,
|
|
132
|
-
stack: stack,
|
|
133
|
-
value: this.value
|
|
134
|
-
};
|
|
135
|
-
index = 0;
|
|
136
|
-
reaction = this;
|
|
137
|
-
stack = [];
|
|
138
|
-
try {
|
|
139
|
-
if (this.cleanup) {
|
|
140
|
-
for (let i = 0, n = this.cleanup.length; i < n; i++) {
|
|
141
|
-
this.cleanup[i](this.value);
|
|
142
|
-
}
|
|
143
|
-
this.cleanup.length = 0;
|
|
144
|
-
}
|
|
145
|
-
this.value = this.fn((fn) => {
|
|
146
|
-
if (!this.cleanup) {
|
|
147
|
-
this.cleanup = [fn];
|
|
148
|
-
}
|
|
149
|
-
else {
|
|
150
|
-
this.cleanup.push(fn);
|
|
151
|
-
}
|
|
152
|
-
});
|
|
153
|
-
if (stack.length) {
|
|
154
|
-
this.removeParentObservers();
|
|
155
|
-
if (this.sources && index > 0) {
|
|
156
|
-
this.sources.length = index + stack.length;
|
|
157
|
-
for (let i = 0; i < stack.length; i++) {
|
|
158
|
-
this.sources[index + i] = stack[i];
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
else {
|
|
162
|
-
this.sources = stack;
|
|
163
|
-
}
|
|
164
|
-
for (let i = index; i < this.sources.length; i++) {
|
|
165
|
-
let source = this.sources[i];
|
|
166
|
-
if (!source.observers) {
|
|
167
|
-
source.observers = [this];
|
|
168
|
-
}
|
|
169
|
-
else {
|
|
170
|
-
source.observers.push(this);
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
else if (this.sources && index < this.sources.length) {
|
|
175
|
-
this.removeParentObservers();
|
|
176
|
-
this.sources.length = index;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
finally {
|
|
180
|
-
index = previous.index;
|
|
181
|
-
reaction = previous.reaction;
|
|
182
|
-
stack = previous.stack;
|
|
183
|
-
}
|
|
184
|
-
if (this.observers && previous.value !== this.value) {
|
|
185
|
-
for (let i = 0; i < this.observers.length; i++) {
|
|
186
|
-
this.observers[i].state = DIRTY;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
this.state = CLEAN;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
const scheduler = {
|
|
193
|
-
add: (scheduler) => {
|
|
194
|
-
scheduler.tasks.add(task);
|
|
195
|
-
schedulers.add(scheduler);
|
|
196
|
-
},
|
|
197
|
-
delete: (scheduler) => {
|
|
198
|
-
scheduler.tasks.delete(task);
|
|
199
|
-
schedulers.delete(scheduler);
|
|
200
|
-
}
|
|
201
|
-
};
|
|
202
|
-
/* harmony default export */ const reactive = (Reactive);
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
;// CONCATENATED MODULE: ./src/methods/effect.ts
|
|
206
|
-
|
|
207
|
-
/* harmony default export */ const effect = ((fn) => {
|
|
208
|
-
new reactive(fn, true);
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
;// CONCATENATED MODULE: ./src/methods/reactive.ts
|
|
212
|
-
|
|
213
|
-
function factory(value) {
|
|
214
|
-
if (typeof value === 'object' && value !== null && (value.constructor === Object)) {
|
|
215
|
-
return obj(value);
|
|
216
|
-
}
|
|
217
|
-
return new reactive(value);
|
|
218
|
-
}
|
|
219
|
-
function fn(value) {
|
|
220
|
-
let fn = new reactive(value);
|
|
221
|
-
return (...args) => {
|
|
222
|
-
let value = fn.get();
|
|
223
|
-
if (args.length && typeof value === 'function') {
|
|
224
|
-
value = value(...args);
|
|
225
|
-
}
|
|
226
|
-
return value;
|
|
227
|
-
};
|
|
228
|
-
}
|
|
229
|
-
function obj(values) {
|
|
230
|
-
let lazy = {}, properties = {};
|
|
231
|
-
for (let key in values) {
|
|
232
|
-
properties[key] = {
|
|
233
|
-
get() {
|
|
234
|
-
if (!lazy[key]) {
|
|
235
|
-
lazy[key] = factory(values[key]);
|
|
236
|
-
}
|
|
237
|
-
if (lazy[key] instanceof reactive) {
|
|
238
|
-
return lazy[key].get();
|
|
239
|
-
}
|
|
240
|
-
return lazy[key];
|
|
241
|
-
},
|
|
242
|
-
set(value) {
|
|
243
|
-
if (!lazy[key]) {
|
|
244
|
-
lazy[key] = factory(values[key]);
|
|
245
|
-
}
|
|
246
|
-
if (lazy[key] instanceof reactive) {
|
|
247
|
-
lazy[key].set(value);
|
|
248
|
-
}
|
|
249
|
-
else {
|
|
250
|
-
lazy[key] = factory(value);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
};
|
|
254
|
-
}
|
|
255
|
-
return Object.defineProperties({}, properties);
|
|
256
|
-
}
|
|
257
|
-
;
|
|
258
|
-
/* harmony default export */ const methods_reactive = ((value) => {
|
|
259
|
-
if (typeof value === 'function') {
|
|
260
|
-
return fn(value);
|
|
261
|
-
}
|
|
262
|
-
return factory(value);
|
|
263
|
-
});
|
|
264
|
-
|
|
265
|
-
;// CONCATENATED MODULE: ./src/methods/index.ts
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
;// CONCATENATED MODULE: ./src/index.ts
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
/* harmony default export */ const src = ({ effect: effect, reactive: methods_reactive, scheduler: scheduler });
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
/******/ })()
|
|
277
|
-
;
|
package/src/methods/effect.ts
DELETED
package/src/methods/index.ts
DELETED
package/src/methods/reactive.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { Infer } from '~/types';
|
|
2
|
-
import Reactive from '~/reactive';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
function factory<T>(value: T) {
|
|
6
|
-
if (typeof value === 'object' && value !== null && (value.constructor === Object)) {
|
|
7
|
-
return obj(value);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
return new Reactive(value);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// Functions are wrapped to remove '.get' usage
|
|
14
|
-
function fn<T>(value: T) {
|
|
15
|
-
let fn = new Reactive(value);
|
|
16
|
-
|
|
17
|
-
return (...args: any[]) => {
|
|
18
|
-
let value = fn.get();
|
|
19
|
-
|
|
20
|
-
if (args.length && typeof value === 'function') {
|
|
21
|
-
value = value(...args);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return value;
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// TODO: Typecheck on `values` to get rid of lazy var?
|
|
29
|
-
function obj<T>(values: T) {
|
|
30
|
-
let lazy: Record<string, any> = {},
|
|
31
|
-
properties: PropertyDescriptorMap = {};
|
|
32
|
-
|
|
33
|
-
for (let key in values) {
|
|
34
|
-
properties[key] = {
|
|
35
|
-
get() {
|
|
36
|
-
if (!lazy[key]) {
|
|
37
|
-
lazy[key] = factory(values[key]);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (lazy[key] instanceof Reactive) {
|
|
41
|
-
return lazy[key].get();
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return lazy[key];
|
|
45
|
-
},
|
|
46
|
-
set(value: unknown) {
|
|
47
|
-
if (!lazy[key]) {
|
|
48
|
-
lazy[key] = factory(values[key]);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (lazy[key] instanceof Reactive) {
|
|
52
|
-
lazy[key].set(value);
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
55
|
-
lazy[key] = factory(value);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return Object.defineProperties({}, properties);
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
export default <T>(value: T) => {
|
|
66
|
-
if (typeof value === 'function') {
|
|
67
|
-
return fn(value) as Infer<T>;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return factory(value) as Infer<T>;
|
|
71
|
-
};
|
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 };
|