-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
264 lines (245 loc) · 6.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*! (c) Andrea Giammarchi */
const {is} = Object;
let batches;
/**
* Execute a callback that will not side-effect until its top-most batch is
* completed.
* @param {() => void} callback a function that batches changes to be notified
* through signals.
*/
export const batch = callback => {
const prev = batches;
batches = prev || [];
try {
callback();
if (!prev)
for (const {value} of batches);
}
finally { batches = prev }
};
/**
* A signal with a value property also exposed via toJSON, toString and valueOf.
* When created via computed, the `value` property is **readonly**.
* @template T
*/
export class Signal {
constructor(value) {
this._ = value;
}
/** @returns {T} */
toJSON() { return this.value }
/** @returns {string} */
toString() { return String(this.value) }
/** @returns {T} */
valueOf() { return this.value }
}
let computedSignal;
/**
* @template T
* @extends {Signal<T>}
*/
class Computed extends Signal {
/**
* @private
* @type{Reactive<T>}
*/
s
/**
* @param {(v: T) => T} _
* @param {T} v
* @param {{ equals?: Equals<T> }} o
* @param {boolean} f
*/
constructor(_, v, o, f) {
super(_);
this.f = f; // is effect?
this.$ = true; // should update ("value for money")
this.r = new Set; // related signals
this.s = new Reactive(v, o); // signal
}
peek() { return this.s.peek() }
get value() {
if (this.$) {
const prev = computedSignal;
computedSignal = this;
try { this.s.value = this._(this.s._) }
finally {
this.$ = false;
computedSignal = prev;
}
}
return this.s.value;
}
}
const defaults = {async: false, equals: true};
/**
* Returns a read-only Signal that is invoked only when any of the internally
* used signals, as in within the callback, is unknown or updated.
* @type {<R, V, T = unknown extends V ? R : R|V>(fn: (v: T) => R, value?: V, options?: { equals?: Equals<T> }) => ComputedSignal<T>}
*/
export const computed = (fn, value, options = defaults) =>
new Computed(fn, value, options, false);
let outerEffect;
const empty = [];
const noop = () => {};
const dispose = ({s}) => {
if (typeof s._ === 'function')
s._ = s._();
};
export class FX extends Computed {
constructor(_, v, o) {
super(_, v, o, true);
this.e = empty;
}
run() {
this.$ = true;
this.value;
return this;
}
stop() {
this._ = noop;
this.r.clear();
this.s.c.clear();
}
}
export class Effect extends FX {
constructor(_, v, o) {
super(_, v, o);
this.i = 0; // index
this.a = !!o.async; // async
this.m = true; // microtask
this.e = []; // effects
// "I am effects" ^_^;;
}
get value() {
this.a ? this.async() : this.sync();
}
async() {
if (this.m) {
this.m = false;
queueMicrotask(() => {
this.m = true;
this.sync();
});
}
}
sync() {
const prev = outerEffect;
(outerEffect = this).i = 0;
dispose(this);
super.value;
outerEffect = prev;
}
stop() {
super.stop();
dispose(this);
for (const effect of this.e.splice(0))
effect.stop();
}
}
/**
* Invokes a function when any of its internal signals or computed values change.
*
* Returns a dispose callback.
* @template T
* @type {<T>(fn: (v: T) => T, value?: T, options?: { async?: boolean }) => () => void}
*/
export const effect = (callback, value, options = defaults) => {
let unique;
if (outerEffect) {
const {i, e} = outerEffect;
const isNew = i === e.length;
// bottleneck:
// there's literally no way to optimize this path *unless* the callback is
// already a known one. however, latter case is not really common code so
// the question is: should I optimize this more than this? 'cause I don't
// think the amount of code needed to understand if a callback is *likely*
// the same as before makes any sense + correctness would be trashed.
if (isNew || e[i]._ !== callback) {
if (!isNew) e[i].stop();
e[i] = new Effect(callback, value, options).run();
}
unique = e[i];
outerEffect.i++;
}
else
unique = new Effect(callback, value, options).run();
return () => { unique.stop() };
};
const skip = () => false;
/**
* @template T
* @extends {Signal<T>}
*/
class Reactive extends Signal {
constructor(_, {equals}) {
super(_)
this.c = new Set; // computeds
this.s = equals === true ? is : (equals || skip); // (don't) skip updates
}
/**
* Allows to get signal.value without subscribing to updates in an effect
* @returns {T}
*/
peek() { return this._ }
/** @returns {T} */
get value() {
if (computedSignal) {
this.c.add(computedSignal);
computedSignal.r.add(this);
}
return this._;
}
set value(_) {
const prev = this._;
if (!this.s((this._ = _), prev)) {
if (this.c.size) {
const effects = [];
const stack = [this];
for (const signal of stack) {
for (const computed of signal.c) {
if (!computed.$ && computed.r.has(signal)) {
computed.r.clear();
computed.$ = true;
if (computed.f) {
effects.push(computed);
const stack = [computed];
for (const c of stack) {
for (const effect of c.e) {
effect.r.clear();
effect.$ = true;
stack.push(effect);
}
}
}
else
stack.push(computed.s);
}
}
}
for (const effect of effects)
batches ? batches.push(effect) : effect.value;
}
}
}
}
/**
* Returns a writable Signal that side-effects whenever its value gets updated.
* @template T
* @type {<T>(initialValue: T, options?: { equals?: Equals<T> }) => ReactiveSignal<T>}
*/
export const signal = (value, options = defaults) => new Reactive(value, options);
/**
* @template [T=any]
* @typedef {boolean | ((prev: T, next: T) => boolean)} Equals
*/
/**
* @public
* @template T
* @typedef {Omit<Reactive<T>, '_'|'s'|'c'>} ReactiveSignal<T>
*/
/**
* @public
* @template T
* @typedef {Omit<Computed<T>, '$'|'s'|'f'|'r'|'_'>} ComputedSignal<T>
*/