-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreactivity.js
623 lines (577 loc) · 17.6 KB
/
reactivity.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//
// Heavily inspired by signal.ts from [https://github.com/ryansolid/solid]
// and by extension S.js [https://github.com/adamhaile/S]
//
// See:
// https://indepth.dev/posts/1269/finding-fine-grained-reactive-programming#how-it-works
// https://levelup.gitconnected.com/finding-fine-grained-reactive-programming-89741994ddee?source=friends_link&sk=31c66a70c1dce7dd5f3f4229423ad127#4543
//
/** @typedef { import('./internal').SubjectR } SubjectR */
/** @typedef { import('./internal').ObserverR } ObserverR */
/** @typedef { import('./internal').MemoR } MemoR */
/**
@template T
@typedef { import('./internal').Observer<T> } Observer<T>
*/
/**
@template T
@typedef { import('./internal').Subject<T> } Subject<T>
*/
/**
@template T
@typedef { import('./internal').Memo<T> } Memo<T>
*/
/** @type { import('./internal').StatusEnum } */
const Status = Object.freeze({
OK: 0,
Pending: 1,
Stale: 2,
});
/**
@param { ObserverR } observer
@return { observer is MemoR }
*/
function isObserverRMemoR(observer) {
// prettier-ignore
const memo = /** @type MemoR */ (observer);
return memo.observers !== undefined;
}
/**
@template T
@param { Subject<T> } subject
@return { subject is Memo<T> }
*/
function isSubjectMemo(subject) {
// prettier-ignore
const memo = /** @type Memo<T> */ (/** @type unknown */ (subject));
return memo.subjects !== undefined;
}
/**
@template T
@param { Observer<T> } observer
@return { observer is Memo<T> }
*/
function isObserverMemo(observer) {
// prettier-ignore
const memo = /** @type Memo<T> */ (/** @type unknown */ (observer));
return memo.observers !== undefined;
}
/**
@template T
@type { <T>(lhs: T, rhs: T) => boolean }
*/
const defaultEqual = (lhs, rhs) => lhs === rhs;
/**
@template T
@param { boolean | EqualFn<T> | undefined } equal
@return { EqualFn<T> | undefined }
*/
function selectEqualFn(equal) {
if (typeof equal === 'function') return equal;
if (equal === true) return defaultEqual;
return undefined;
}
// module context values
/** @type ObserverR */
let activeObserver;
/** @type { ObserverR[] | undefined } */
let updateQueue;
/** @type { ObserverR[] | undefined } */
let effectsQueue;
/**
@param { SubjectR } subject
@param { ObserverR } observer
@return void
*/
function link(subject, observer) {
observer.subjects.add(subject);
subject.observers.add(observer);
}
/**
@param { ObserverR } observer
@return void
*/
function prepareForUpdate(observer) {
if (isObserverRMemoR(observer) && observer.status !== Status.Pending) {
markDeepObservers(observer);
}
observer.status = Status.Stale;
// prettier-ignore
if (observer.pure) (/** @type { ObserverR[] } */(updateQueue)).push(observer);
else (/** @type { ObserverR[] } */(effectsQueue)).push(observer);
}
/**
@param { ObserverR } observer
@return void
*/
function markDeep(observer) {
if (observer.status === Status.OK) {
observer.status = Status.Pending;
if (isObserverRMemoR(observer)) markDeepObservers(observer);
}
}
/**
@param { SubjectR } subject
@return void
*/
function markDeepObservers(subject) {
subject.observers.forEach(markDeep);
}
/**
@param { () => void } prepareUpdates
@return void
*/
function runUpdates(prepareUpdates) {
if (updateQueue) return prepareUpdates();
/** @type ObserverR[] */
const updates = [];
updateQueue = updates;
const [effects, delayEffects] = effectsQueue
? [effectsQueue, true]
: [[], false];
effectsQueue = effects;
prepareUpdates();
updateQueued(updates);
updateQueue = undefined;
if (delayEffects) return;
updateQueued(effects);
effectsQueue = undefined;
}
/**
@param { ObserverR[] } queued
@return void
*/
function updateQueued(queued) {
for (let i = 0; i < queued.length; i++)
// prettier-ignore
updateViaStatus(/** @type Observer<unknown> */(queued[i]), true);
}
/**
@template T
@param { Observer<T> } observer
@param { boolean } saveQueue
@return void
*/
function updateViaStatus(observer, saveQueue = false) {
switch (observer.status) {
case Status.Pending: {
if (saveQueue !== true) {
updateDeepStaleSubjects(observer);
} else {
const prevUpdate = updateQueue;
updateQueue = undefined;
updateDeepStaleSubjects(observer);
updateQueue = prevUpdate;
}
break;
}
case Status.Stale:
updateObserver(observer);
break;
}
}
/**
@template T
@param { Subject<T> } subject
@return void
*/
function updateDeep(subject) {
if (isSubjectMemo(subject)) updateViaStatus(subject);
}
/**
@template T
@param { Observer<T> } observer
@return void
*/
function updateDeepStaleSubjects(observer) {
observer.subjects.forEach(updateDeep);
}
/**
@template T
@param { Observer<T> } observer
@return void
*/
function updateObserver(observer) {
unsubscribe(observer);
const prevObserver = activeObserver;
activeObserver = observer;
observer.status = Status.OK;
const nextValue = observer.updateFn(observer.value);
if (isObserverMemo(observer)) writeSubject(observer, nextValue);
else observer.value = nextValue;
activeObserver = prevObserver;
}
/**
@template T
@param { Subject<T> } subject
@return void
*/
function readSubject(subject) {
if (isSubjectMemo(subject) && subject.status !== Status.OK) {
const updates = updateQueue;
updateQueue = undefined;
updateViaStatus(subject);
updateQueue = updates;
}
if (activeObserver) link(subject, activeObserver);
// prettier-ignore
return /** @type { !T } */(subject.value);
}
/**
@template T
@param { Subject<T> } subject
@param { T } value
@return T
*/
function writeSubject(subject, value) {
if (subject.equalFn && subject.value && subject.equalFn(subject.value, value))
return value;
subject.value = value;
if (subject.observers.size) {
runUpdates(() => subject.observers.forEach(prepareForUpdate));
}
return subject.value;
}
/** @typedef { import('reactivity').Options } Options */
/**
@template T
@typedef { import('reactivity').EqualFn<T> } EqualFn<T>
*/
/**
@template T
@typedef { import('reactivity').GetterFn<T> } GetterFn<T>
*/
/**
@template T
@typedef { import('reactivity').SignalPair<T> } SignalPair<T>
*/
/**
@template T
@typedef { import('reactivity').UpdateFn<T> } UpdateFn<T>
*/
/**
@template T
@param { T } value
@param { boolean | EqualFn<T> } [equal]
@param { Options } [options]
@returns { SignalPair<T> }
@type createSignal<T>
*/
function createSignal(value, equal, options) {
/** @type Subject<T> */
const subject = {
name: options?.name,
observers: new Set(),
value,
equalFn: selectEqualFn(equal),
};
return [() => readSubject(subject), (value) => writeSubject(subject, value)];
}
/**
@template T
@param { import('reactivity').UpdateFn<T> } updateFn<T>
@param { T } [value]
@param { boolean | EqualFn<T> } [equal]
@param { Options } [options]
@returns { GetterFn<T> }
*/
function createMemo(updateFn, value, equal, options) {
/** @type Memo<T> */
const memo = {
name: options?.name,
observers: new Set(),
value,
equalFn: selectEqualFn(equal),
updateFn,
status: Status.Stale,
pure: true,
subjects: new Set(),
};
updateObserver(memo);
return () => readSubject(memo);
}
/** @typedef { import('reactivity').UnsubscribeFn } UnsubscribeFn */
/**
@param { ObserverR } observer
@return void
*/
function unsubscribe(observer) {
observer.subjects.forEach((subject) => subject.observers.delete(observer));
}
/**
@param { ObserverR | undefined } observer
@return { UnsubscribeFn }
*/
function makeUnsubscribe(observer) {
return () => {
if (!observer) return;
const o = observer;
observer = undefined;
unsubscribe(o);
};
}
/**
@template T
@param { UpdateFn<T> } updateFn<T>
@param { T } [value]
@returns { UnsubscribeFn }
*/
function createEffect(updateFn, value) {
/** @type Observer<T> */
const o = {
value,
updateFn,
status: Status.Stale,
pure: false,
subjects: new Set(),
};
if (effectsQueue) effectsQueue.push(o);
else updateObserver(o);
return makeUnsubscribe(o);
}
export { createSignal, createMemo, createEffect };
// -------------------------------------------------------
//
// A `Subject<T>` provides its output value **to**
// its dependents (`Observer<T>`s).
//
// An `Observer<T>` gets its input values **from**
// its dependencies (`Subject<T>`s).
//
// A `Memo<T>` merges the _aspects_ of
// both `Observer<T>` and `Subject<T>`.
// Of the three types `Memo<T>` is the most _general_.
// So while `Memo<T>` is composed of both
// `Subject<T>` and `Observer<T>`, both
// `Subject<T>`, `Observer<T>` could be viewed as
// _constrained_ versions of `Memo<T>` (as
// opposed to _specialized_ versions).
//
// Each type is further split into its _value_ aspect
// (e.g. `ObserverV<T>`) and _relation_ (or _rest_)
// aspect (e.g. `ObserverR`). This tactic helps to
// avoid using generic type references with an
// explicit _any_ type parameter in the update
// routing logic which has no actual dependency
// on the type `T` being managed by the
// `Subject<T>`/ `Observer<T>`/`Memo<T>`
// instance.
//
// The existence of the value aspect is only
// acknowledged in `updateQueued()` where the
// `ObserverR` type is asserted to be an
// `Observer<unknown>` which from this point on is
// then handled by generic functions.
//
// So `createSignal<T>()` internally creates a
// `Subject<T>`, `createEffect<T>()` an `Observer<T>`, and
// `createMemo<T>()` a `Memo<T>`.
//
// `createSignal<T>()` returns two functions in a
// `SignalPair<T>` tuple, a `GetterFn<T>` accessor
// and a `SetterFn<T>` mutator. The mutator triggers
// the update of all the `Subject<T>`'s dependents
// while the accessor returns the `Subject<T>`'s
// current value.
//
// The accessor also has the hidden responsibility
// of subscribing the `Observer<T>` that is accessing
// the `Subject<T>`. The `Observer<T>` shares its
// reference via the module's `activeObserver` context
// value. The `Subject<T>` stores that dependency in
// its `observers` property.
//
// `createMemo<T>()` only returns a `GetterFn<T>`
// accessor to obtain the internal `Memo<T>`'s
// current value. The primary argument to
// `createMemo<T>()` is `fn: UpdateFn<T>` - the
// function responsible for deriving the
// `Memo<T>`'s value from its dependencies
// (and the `Memo<T>`'s own previous value). This
// function is invoked whenever at least one of the
// `Memo<T>`'s dependencies has an updated value.
// The function is run before `createMemo<T>()`
// exits via `updateObserver<T>()` so that
// * the `Memo<T>` can calculate its current value
// * the `Memo<T>` subscribes to all its
// relevant dependencies.
//
// To that end `updateObserver<T>()` sets the
// module's `activeObserver` context value before
// invoking `fn: UpdateFn<T>` so that all the
// `Subject<T>`'s being accessed can register the
// subscription of the `Observer<T>` (or `Memo<T>`).
//
// (Note that `updateObserver<T>()` unsubscribes the
// `Observer<T>` (or `Memo<T>`) first - this is
// essential later so that the `Observer<T>` isn't
// updated for irrelevant dependencies - e.g. an
// input accessor that is guarded by an `if` condition
// that currently evaluates to `false`.)
//
// `createEffect<T>()` returns an `UnsubscribeFn`
// function that deactivates the callback when invoked.
// There is no `GetterFn<T>` because the callbacks
// act by _side effect_ - i.e. changing values that
// exist in their enclosing scope (closure:
// https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/ch7.md
// ). Internally a callback is based on a `Observer<T>`.
// The primary argument to
// `createEffect<T>()` is `fn: UpdateFn<T>` - the
// function is responsible for accessing the `Subject<T>`
// dependencies (and thereby subscribing to them)
// and implementing the side effect(s). This
// function is called whenever at least one of the
// `Observer<T>`'s dependencies has an updated value.
// `updateObserver<T>()` is only invoked if currently no
// update is in progress - otherwise the callback is
// queued for later invocation.
//
// All three `create*` functions take a `value: T`
// argument - required for `createSignal<T>()`,
// optional for the others. For `createSignal<T>()`
// this is the initial value - for the others it is the
// argument that is passed to the `fn: UpdateFn<T>`
// the first time it executes.
//
// `createSignal<T>()` and `createMemo<T>()` have
// an (optional) ` equal?: boolean | EqualFn<T>`
// argument. Given the generic implementation of the
// capabilities, by default `Subject<T>`s don't
// limit updates to occasions when their `value`
// _changes_ - as there is no standardized way
// to check for equality of `T` in TypeScript
// (compared to for example Rust):
// https://doc.rust-lang.org/std/cmp/trait.PartialEq.html
// https://doc.rust-lang.org/std/cmp/trait.Eq.html
//
// A value of `true` directs the use of the default
// equality function `defaultEqual` which will work
// for primitive types and references. Otherwise a
// custom equality predicate must be provided
// if updates for identical values of `T` are to
// be suppressed.
//
// `createEffect<T>` doesn't have this argument.
// Therefore it is necessary to configure the
// `Subject<T>`s it depends on to suppress updates
// on identical values.
//
// `createSignal<T>()` and `createMemo<T>()` have
// an optional `Options` value that may carry a
// `name` property. This can come in handy for
// debugging.
//
// The core capability is driven by the
// `writeSubject<T>` function which is invoked whenever
// an input is set with its `SetterFn<T>` or a
// `Memo<T>` has its `value` set by a dependency.
// The update of dependents is suppressed for identical
// values provided the `Subject<T>` is configured
// accordingly. Otherwise all the observers are
// prepared for update before the actual updates are
// made.
//
// `prepareForUpdate()` first marks the dependents of a
// `Memo<T>` as `Status.Pending` _in depth_. The
// observer itself is marked as `Status.Stale` before
// being pushed onto `updateQueue` or `effectsQueue`.
//
// * `Status.Pending` - an `Observer<T>` has this
// status when one of its dependencies _descendents_
// is marked as `Status.Stale`. It acts as a
// pre-`Status.Stale` status. The `Observer<T>`
// instance is "out-of-sync" but not yet ready
// for update (so it isn't on `updateQueue`).
// * `Status.Stale` - an `Observer<T>` enters this
// state when at least one of its direct
// dependencies has been updated. The `Observer<T>`
// instance is on `updateQueue`.
//
// `runUpdates` coordinates the current wave of updates.
// The passed `prepareUpdate` function is invoked if an
// update wave is already underway. Otherwise an empty
// `updateQueue` is set up. If effects are already
// being queued this particular `runUpdates` invocation
// won't be processing effects
// (`delayEffects = true`) - otherwise an empty
// `effectsQueue` is set up.
//
// Finally the passed `prepareUpdates` function is
// invoked causing "memos" to be queued up on
// `updateQueue` and "effects" to be queued up on
// `effectsQueue`. The `updateQueue` is processed
// with the `updateQueued()` function.
//
// Effects are only processed if this is the
// top-level invocation of `runUpdates()` - this
// ensures that effects are updated as late as
// possible to avoid unnecessary, multiple
// updates.
//
// Updates routed through `updateViaStatus` by
// `updateQueued` start a nested (deep) update wave
// for an `Observer<T>` in the `Status.Pending`
// status - i.e. there are dependencies in need of
// update before the `Observer<T>` can perform
// a meaningful update of itself. `Observer<T>`'s
// in the `Status.Stale` status can immediately update
// themselves via their `fn: UpdateFn<T>`.
//
// `readSubject<T>()` will issue a nested update wave
// if it discovers that its `Subject<T>` is currently
// **not** in `Status.OK` status as it needs those
// updates to complete before an up-to-date `value`
// can be returned.
//
// References:
//
// Enums
// https://www.typescriptlang.org/docs/handbook/enums.html
//
// Type Aliases
// https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-aliases
//
// Function Types
// https://www.typescriptlang.org/docs/handbook/functions.html#function-types
//
// Generics
// https://www.typescriptlang.org/docs/handbook/generics.html
//
// Optional and Default Types
// https://www.typescriptlang.org/docs/handbook/functions.html#optional-and-default-parameters
//
// Tuple
// https://www.typescriptlang.org/docs/handbook/basic-types.html#tuple
//
// Optional Properties
// https://www.typescriptlang.org/docs/handbook/interfaces.html#optional-properties
//
// Set
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
//
// Intersection Types
// https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#intersection-types
//
// User-Defined Type Guards
// https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
//
// Type Assertions
// https://www.typescriptlang.org/docs/handbook/basic-types.html#type-assertions
//
// Union Types
// https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#union-types
//
// typeof
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
//
// Unknown
// https://www.typescriptlang.org/docs/handbook/basic-types.html#unknown
//
// Non-null assertion operator
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator
//
// Optional chaining (?.)
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining
//
// -------------------------------------------------------