@ersbeth/picoflow 0.2.3 → 1.0.0
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/.cursor/plans/update-js-e795d61b.plan.md +567 -0
- package/.gitlab-ci.yml +24 -0
- package/.vscode/settings.json +3 -3
- package/CHANGELOG.md +51 -0
- package/IMPLEMENTATION_GUIDE.md +1578 -0
- package/README.md +62 -25
- package/biome.json +32 -32
- package/dist/picoflow.js +557 -1099
- package/dist/types/advanced/array.d.ts +0 -6
- package/dist/types/advanced/array.d.ts.map +1 -1
- package/dist/types/advanced/index.d.ts +5 -5
- package/dist/types/advanced/index.d.ts.map +1 -1
- package/dist/types/advanced/map.d.ts +114 -23
- package/dist/types/advanced/map.d.ts.map +1 -1
- package/dist/types/advanced/resource.d.ts +51 -12
- package/dist/types/advanced/resource.d.ts.map +1 -1
- package/dist/types/advanced/resourceAsync.d.ts +28 -13
- package/dist/types/advanced/resourceAsync.d.ts.map +1 -1
- package/dist/types/advanced/stream.d.ts +74 -16
- package/dist/types/advanced/stream.d.ts.map +1 -1
- package/dist/types/advanced/streamAsync.d.ts +69 -15
- package/dist/types/advanced/streamAsync.d.ts.map +1 -1
- package/dist/types/basic/constant.d.ts +44 -16
- package/dist/types/basic/constant.d.ts.map +1 -1
- package/dist/types/basic/derivation.d.ts +73 -24
- package/dist/types/basic/derivation.d.ts.map +1 -1
- package/dist/types/basic/disposable.d.ts +65 -6
- package/dist/types/basic/disposable.d.ts.map +1 -1
- package/dist/types/basic/effect.d.ts +27 -16
- package/dist/types/basic/effect.d.ts.map +1 -1
- package/dist/types/basic/index.d.ts +7 -8
- package/dist/types/basic/index.d.ts.map +1 -1
- package/dist/types/basic/observable.d.ts +62 -13
- package/dist/types/basic/observable.d.ts.map +1 -1
- package/dist/types/basic/signal.d.ts +35 -6
- package/dist/types/basic/signal.d.ts.map +1 -1
- package/dist/types/basic/state.d.ts +25 -4
- package/dist/types/basic/state.d.ts.map +1 -1
- package/dist/types/basic/trackingContext.d.ts +33 -0
- package/dist/types/basic/trackingContext.d.ts.map +1 -0
- package/dist/types/creators.d.ts +271 -26
- package/dist/types/creators.d.ts.map +1 -1
- package/dist/types/index.d.ts +60 -7
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/solid/converters.d.ts +5 -5
- package/dist/types/solid/converters.d.ts.map +1 -1
- package/dist/types/solid/index.d.ts +2 -2
- package/dist/types/solid/index.d.ts.map +1 -1
- package/dist/types/solid/primitives.d.ts +96 -4
- package/dist/types/solid/primitives.d.ts.map +1 -1
- package/docs/.vitepress/config.mts +110 -0
- package/docs/api/classes/FlowArray.md +489 -0
- package/docs/api/classes/FlowConstant.md +350 -0
- package/docs/api/classes/FlowDerivation.md +334 -0
- package/docs/api/classes/FlowEffect.md +100 -0
- package/docs/api/classes/FlowMap.md +512 -0
- package/docs/api/classes/FlowObservable.md +306 -0
- package/docs/api/classes/FlowResource.md +380 -0
- package/docs/api/classes/FlowResourceAsync.md +362 -0
- package/docs/api/classes/FlowSignal.md +160 -0
- package/docs/api/classes/FlowState.md +368 -0
- package/docs/api/classes/FlowStream.md +367 -0
- package/docs/api/classes/FlowStreamAsync.md +364 -0
- package/docs/api/classes/SolidDerivation.md +75 -0
- package/docs/api/classes/SolidResource.md +91 -0
- package/docs/api/classes/SolidState.md +71 -0
- package/docs/api/classes/TrackingContext.md +33 -0
- package/docs/api/functions/array.md +58 -0
- package/docs/api/functions/constant.md +45 -0
- package/docs/api/functions/derivation.md +53 -0
- package/docs/api/functions/effect.md +49 -0
- package/docs/api/functions/from.md +220 -0
- package/docs/api/functions/isDisposable.md +49 -0
- package/docs/api/functions/map.md +57 -0
- package/docs/api/functions/resource.md +52 -0
- package/docs/api/functions/resourceAsync.md +50 -0
- package/docs/api/functions/signal.md +36 -0
- package/docs/api/functions/state.md +47 -0
- package/docs/api/functions/stream.md +53 -0
- package/docs/api/functions/streamAsync.md +50 -0
- package/docs/api/index.md +118 -0
- package/docs/api/interfaces/FlowDisposable.md +65 -0
- package/docs/api/interfaces/SolidObservable.md +19 -0
- package/docs/api/type-aliases/FlowArrayAction.md +49 -0
- package/docs/api/type-aliases/FlowStreamDisposer.md +15 -0
- package/docs/api/type-aliases/FlowStreamSetter.md +27 -0
- package/docs/api/type-aliases/FlowStreamUpdater.md +32 -0
- package/docs/api/type-aliases/NotPromise.md +18 -0
- package/docs/api/type-aliases/SolidGetter.md +17 -0
- package/docs/api/typedoc-sidebar.json +1 -0
- package/docs/examples/examples.md +2313 -0
- package/docs/examples/patterns.md +649 -0
- package/docs/guide/advanced/disposal.md +426 -0
- package/docs/guide/advanced/solidjs.md +221 -0
- package/docs/guide/advanced/upgrading.md +464 -0
- package/docs/guide/introduction/concepts.md +56 -0
- package/docs/guide/introduction/conventions.md +61 -0
- package/docs/guide/introduction/getting-started.md +134 -0
- package/docs/guide/introduction/lifecycle.md +371 -0
- package/docs/guide/primitives/array.md +400 -0
- package/docs/guide/primitives/constant.md +380 -0
- package/docs/guide/primitives/derivations.md +348 -0
- package/docs/guide/primitives/effects.md +458 -0
- package/docs/guide/primitives/map.md +387 -0
- package/docs/guide/primitives/overview.md +175 -0
- package/docs/guide/primitives/resources.md +858 -0
- package/docs/guide/primitives/signal.md +259 -0
- package/docs/guide/primitives/state.md +368 -0
- package/docs/guide/primitives/streams.md +931 -0
- package/docs/index.md +47 -0
- package/docs/public/logo.svg +1 -0
- package/package.json +57 -41
- package/src/advanced/array.ts +208 -210
- package/src/advanced/index.ts +7 -7
- package/src/advanced/map.ts +178 -68
- package/src/advanced/resource.ts +87 -43
- package/src/advanced/resourceAsync.ts +62 -42
- package/src/advanced/stream.ts +113 -50
- package/src/advanced/streamAsync.ts +120 -61
- package/src/basic/constant.ts +82 -49
- package/src/basic/derivation.ts +128 -84
- package/src/basic/disposable.ts +74 -15
- package/src/basic/effect.ts +85 -77
- package/src/basic/index.ts +7 -8
- package/src/basic/observable.ts +94 -36
- package/src/basic/signal.ts +133 -105
- package/src/basic/state.ts +46 -25
- package/src/basic/trackingContext.ts +45 -0
- package/src/creators.ts +297 -54
- package/src/index.ts +96 -43
- package/src/solid/converters.ts +186 -67
- package/src/solid/index.ts +8 -2
- package/src/solid/primitives.ts +167 -65
- package/test/array.test.ts +592 -612
- package/test/constant.test.ts +31 -33
- package/test/derivation.test.ts +531 -536
- package/test/effect.test.ts +21 -21
- package/test/map.test.ts +233 -137
- package/test/resource.test.ts +119 -121
- package/test/resourceAsync.test.ts +98 -100
- package/test/signal.test.ts +51 -55
- package/test/state.test.ts +186 -168
- package/test/stream.test.ts +189 -189
- package/test/streamAsync.test.ts +186 -186
- package/tsconfig.json +19 -18
- package/typedoc.json +37 -0
- package/vite.config.ts +23 -20
- package/vitest.config.ts +7 -7
- package/api/doc/index.md +0 -31
- package/api/doc/picoflow.array.md +0 -55
- package/api/doc/picoflow.constant.md +0 -55
- package/api/doc/picoflow.derivation.md +0 -55
- package/api/doc/picoflow.effect.md +0 -55
- package/api/doc/picoflow.flowarray._constructor_.md +0 -49
- package/api/doc/picoflow.flowarray._lastaction.md +0 -13
- package/api/doc/picoflow.flowarray.clear.md +0 -17
- package/api/doc/picoflow.flowarray.dispose.md +0 -55
- package/api/doc/picoflow.flowarray.get.md +0 -19
- package/api/doc/picoflow.flowarray.length.md +0 -13
- package/api/doc/picoflow.flowarray.md +0 -273
- package/api/doc/picoflow.flowarray.pop.md +0 -17
- package/api/doc/picoflow.flowarray.push.md +0 -53
- package/api/doc/picoflow.flowarray.set.md +0 -53
- package/api/doc/picoflow.flowarray.setitem.md +0 -69
- package/api/doc/picoflow.flowarray.shift.md +0 -17
- package/api/doc/picoflow.flowarray.splice.md +0 -85
- package/api/doc/picoflow.flowarray.unshift.md +0 -53
- package/api/doc/picoflow.flowarrayaction.md +0 -37
- package/api/doc/picoflow.flowconstant._constructor_.md +0 -49
- package/api/doc/picoflow.flowconstant.get.md +0 -25
- package/api/doc/picoflow.flowconstant.md +0 -88
- package/api/doc/picoflow.flowderivation._constructor_.md +0 -49
- package/api/doc/picoflow.flowderivation.get.md +0 -23
- package/api/doc/picoflow.flowderivation.md +0 -86
- package/api/doc/picoflow.flowdisposable.dispose.md +0 -55
- package/api/doc/picoflow.flowdisposable.md +0 -43
- package/api/doc/picoflow.floweffect._constructor_.md +0 -54
- package/api/doc/picoflow.floweffect.dispose.md +0 -21
- package/api/doc/picoflow.floweffect.disposed.md +0 -13
- package/api/doc/picoflow.floweffect.md +0 -131
- package/api/doc/picoflow.flowgetter.md +0 -15
- package/api/doc/picoflow.flowmap._lastdeleted.md +0 -21
- package/api/doc/picoflow.flowmap._lastset.md +0 -21
- package/api/doc/picoflow.flowmap.delete.md +0 -61
- package/api/doc/picoflow.flowmap.md +0 -133
- package/api/doc/picoflow.flowmap.setat.md +0 -77
- package/api/doc/picoflow.flowobservable.get.md +0 -19
- package/api/doc/picoflow.flowobservable.md +0 -68
- package/api/doc/picoflow.flowobservable.subscribe.md +0 -55
- package/api/doc/picoflow.flowresource._constructor_.md +0 -49
- package/api/doc/picoflow.flowresource.fetch.md +0 -27
- package/api/doc/picoflow.flowresource.get.md +0 -23
- package/api/doc/picoflow.flowresource.md +0 -100
- package/api/doc/picoflow.flowresourceasync._constructor_.md +0 -49
- package/api/doc/picoflow.flowresourceasync.fetch.md +0 -27
- package/api/doc/picoflow.flowresourceasync.get.md +0 -23
- package/api/doc/picoflow.flowresourceasync.md +0 -100
- package/api/doc/picoflow.flowsignal.dispose.md +0 -59
- package/api/doc/picoflow.flowsignal.disposed.md +0 -18
- package/api/doc/picoflow.flowsignal.md +0 -112
- package/api/doc/picoflow.flowsignal.trigger.md +0 -21
- package/api/doc/picoflow.flowstate.md +0 -52
- package/api/doc/picoflow.flowstate.set.md +0 -61
- package/api/doc/picoflow.flowstream._constructor_.md +0 -49
- package/api/doc/picoflow.flowstream.dispose.md +0 -21
- package/api/doc/picoflow.flowstream.get.md +0 -23
- package/api/doc/picoflow.flowstream.md +0 -100
- package/api/doc/picoflow.flowstreamasync._constructor_.md +0 -54
- package/api/doc/picoflow.flowstreamasync.dispose.md +0 -21
- package/api/doc/picoflow.flowstreamasync.get.md +0 -23
- package/api/doc/picoflow.flowstreamasync.md +0 -100
- package/api/doc/picoflow.flowstreamdisposer.md +0 -13
- package/api/doc/picoflow.flowstreamsetter.md +0 -13
- package/api/doc/picoflow.flowstreamupdater.md +0 -19
- package/api/doc/picoflow.flowwatcher.md +0 -15
- package/api/doc/picoflow.from.md +0 -55
- package/api/doc/picoflow.from_1.md +0 -55
- package/api/doc/picoflow.from_2.md +0 -55
- package/api/doc/picoflow.from_3.md +0 -55
- package/api/doc/picoflow.from_4.md +0 -55
- package/api/doc/picoflow.from_5.md +0 -55
- package/api/doc/picoflow.isdisposable.md +0 -55
- package/api/doc/picoflow.map.md +0 -59
- package/api/doc/picoflow.md +0 -544
- package/api/doc/picoflow.resource.md +0 -55
- package/api/doc/picoflow.resourceasync.md +0 -55
- package/api/doc/picoflow.signal.md +0 -19
- package/api/doc/picoflow.solidderivation._constructor_.md +0 -49
- package/api/doc/picoflow.solidderivation.get.md +0 -13
- package/api/doc/picoflow.solidderivation.md +0 -94
- package/api/doc/picoflow.solidgetter.md +0 -13
- package/api/doc/picoflow.solidobservable.get.md +0 -13
- package/api/doc/picoflow.solidobservable.md +0 -57
- package/api/doc/picoflow.solidresource._constructor_.md +0 -49
- package/api/doc/picoflow.solidresource.get.md +0 -13
- package/api/doc/picoflow.solidresource.latest.md +0 -13
- package/api/doc/picoflow.solidresource.md +0 -157
- package/api/doc/picoflow.solidresource.refetch.md +0 -13
- package/api/doc/picoflow.solidresource.state.md +0 -13
- package/api/doc/picoflow.solidstate._constructor_.md +0 -49
- package/api/doc/picoflow.solidstate.get.md +0 -13
- package/api/doc/picoflow.solidstate.md +0 -115
- package/api/doc/picoflow.solidstate.set.md +0 -13
- package/api/doc/picoflow.state.md +0 -55
- package/api/doc/picoflow.stream.md +0 -55
- package/api/doc/picoflow.streamasync.md +0 -55
- package/api/picoflow.public.api.md +0 -244
- package/api-extractor.json +0 -61
package/test/derivation.test.ts
CHANGED
|
@@ -2,543 +2,538 @@ import { describe, expect, test, vi } from "vitest";
|
|
|
2
2
|
import { derivation, effect, resource, signal, state } from "#package";
|
|
3
3
|
|
|
4
4
|
describe("derivation", () => {
|
|
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
|
-
});
|
|
5
|
+
describe("from state", () => {
|
|
6
|
+
test("is updated", () => {
|
|
7
|
+
const $state = state(1);
|
|
8
|
+
const $derivation = derivation((t) => $state.get(t) * 2);
|
|
9
|
+
expect($derivation.pick()).toBe(2);
|
|
10
|
+
|
|
11
|
+
$state.set(2);
|
|
12
|
+
expect($derivation.pick()).toBe(4);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("get throws when disposed", () => {
|
|
16
|
+
const $state = state(1);
|
|
17
|
+
const $derivation = derivation((t) => $state.get(t) * 2);
|
|
18
|
+
expect($derivation.pick()).toBe(2);
|
|
19
|
+
|
|
20
|
+
$state.set(2);
|
|
21
|
+
expect($derivation.pick()).toBe(4);
|
|
22
|
+
|
|
23
|
+
$derivation.dispose();
|
|
24
|
+
expect(() => $derivation.pick()).toThrow(
|
|
25
|
+
"[PicoFlow] Primitive is disposed",
|
|
26
|
+
);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("get throws when state is disposed", () => {
|
|
30
|
+
const $state = state(1);
|
|
31
|
+
const $derivation = derivation((t) => $state.get(t) * 2);
|
|
32
|
+
expect($derivation.pick()).toBe(2);
|
|
33
|
+
|
|
34
|
+
$state.set(2);
|
|
35
|
+
expect($derivation.pick()).toBe(4);
|
|
36
|
+
|
|
37
|
+
$state.dispose();
|
|
38
|
+
expect(() => $derivation.pick()).toThrow(
|
|
39
|
+
"[PicoFlow] Primitive is disposed",
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("is updated (chained dependencies)", () => {
|
|
44
|
+
const $state = state(1);
|
|
45
|
+
const $derivation1 = derivation((t) => $state.get(t) * 2);
|
|
46
|
+
const $derivation2 = derivation((t) => $derivation1.get(t) * 2);
|
|
47
|
+
|
|
48
|
+
expect($derivation2.pick()).toBe(4);
|
|
49
|
+
|
|
50
|
+
$state.set(2);
|
|
51
|
+
expect($derivation2.pick()).toBe(8);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("is updated (multiple dependencies)", () => {
|
|
55
|
+
const $state1 = state(1);
|
|
56
|
+
const $state2 = state(2);
|
|
57
|
+
const $derivation = derivation((t) => $state1.get(t) + $state2.get(t));
|
|
58
|
+
|
|
59
|
+
expect($derivation.pick()).toBe(3);
|
|
60
|
+
|
|
61
|
+
$state1.set(2);
|
|
62
|
+
expect($derivation.pick()).toBe(4);
|
|
63
|
+
|
|
64
|
+
$state2.set(3);
|
|
65
|
+
expect($derivation.pick()).toBe(5);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("is updated (multiple dependants)", () => {
|
|
69
|
+
const $state = state(1);
|
|
70
|
+
const $derivation1 = derivation((t) => $state.get(t) * 2);
|
|
71
|
+
const $derivation2 = derivation((t) => $state.get(t) * 3);
|
|
72
|
+
|
|
73
|
+
expect($derivation1.pick()).toBe(2);
|
|
74
|
+
expect($derivation2.pick()).toBe(3);
|
|
75
|
+
|
|
76
|
+
$state.set(2);
|
|
77
|
+
expect($derivation1.pick()).toBe(4);
|
|
78
|
+
expect($derivation2.pick()).toBe(6);
|
|
79
|
+
|
|
80
|
+
$state.set(3);
|
|
81
|
+
expect($derivation1.pick()).toBe(6);
|
|
82
|
+
expect($derivation2.pick()).toBe(9);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("is updated (async)", async () => {
|
|
86
|
+
const $state = state(1);
|
|
87
|
+
const $derivation = derivation(async (t) => $state.get(t) * 2);
|
|
88
|
+
|
|
89
|
+
expect(await $derivation.pick()).toBe(2);
|
|
90
|
+
|
|
91
|
+
$state.set(2);
|
|
92
|
+
expect(await $derivation.pick()).toBe(4);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("is updated (async chain)", async () => {
|
|
96
|
+
const $state = state(1);
|
|
97
|
+
const $derivation1 = derivation(async (t) => $state.get(t) * 2);
|
|
98
|
+
const $derivation2 = derivation(
|
|
99
|
+
async (t) => (await $derivation1.get(t)) * 2,
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
expect(await $derivation2.pick()).toBe(4);
|
|
103
|
+
|
|
104
|
+
$state.set(2);
|
|
105
|
+
expect(await $derivation2.pick()).toBe(8);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe("from resource", () => {
|
|
110
|
+
test("from resource is updated", async () => {
|
|
111
|
+
let resourceCounter = 0;
|
|
112
|
+
const fetchResource = async () => {
|
|
113
|
+
resourceCounter++;
|
|
114
|
+
return resourceCounter;
|
|
115
|
+
};
|
|
116
|
+
const $resource = resource(fetchResource);
|
|
117
|
+
const $derivation = derivation((t) => ($resource.get(t) ?? 0) * 2);
|
|
118
|
+
|
|
119
|
+
expect($derivation.pick()).toBe(0);
|
|
120
|
+
|
|
121
|
+
await $resource.fetch();
|
|
122
|
+
expect($derivation.pick()).toBe(2);
|
|
123
|
+
|
|
124
|
+
await $resource.fetch();
|
|
125
|
+
expect($derivation.pick()).toBe(4);
|
|
126
|
+
|
|
127
|
+
await $resource.fetch();
|
|
128
|
+
expect($derivation.pick()).toBe(6);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
133
131
|
});
|
|
134
132
|
|
|
135
133
|
describe("effect", () => {
|
|
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
|
-
expect(effectFn).toHaveBeenLastCalledWith(6);
|
|
542
|
-
});
|
|
543
|
-
});
|
|
134
|
+
describe("from signal", () => {
|
|
135
|
+
test("called when triggered", () => {
|
|
136
|
+
const $signal = signal();
|
|
137
|
+
const $derivation = derivation((t) => {
|
|
138
|
+
$signal.watch(t);
|
|
139
|
+
});
|
|
140
|
+
const effectFn = vi.fn();
|
|
141
|
+
effect((t) => {
|
|
142
|
+
$derivation.watch(t);
|
|
143
|
+
effectFn();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
147
|
+
|
|
148
|
+
$signal.trigger();
|
|
149
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
150
|
+
|
|
151
|
+
$signal.trigger();
|
|
152
|
+
expect(effectFn).toHaveBeenCalledTimes(3);
|
|
153
|
+
|
|
154
|
+
$signal.trigger();
|
|
155
|
+
expect(effectFn).toHaveBeenCalledTimes(4);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test("called when triggered (chained dependencies)", () => {
|
|
159
|
+
const $signal = signal();
|
|
160
|
+
const $derivation1 = derivation((t) => $signal.watch(t));
|
|
161
|
+
const $derivation2 = derivation((t) => $derivation1.watch(t));
|
|
162
|
+
const effectFn = vi.fn();
|
|
163
|
+
effect((t) => {
|
|
164
|
+
$derivation2.watch(t);
|
|
165
|
+
effectFn();
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
169
|
+
|
|
170
|
+
$signal.trigger();
|
|
171
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("called when triggered (multiple dependencies)", () => {
|
|
175
|
+
const $signal1 = signal();
|
|
176
|
+
const $signal2 = signal();
|
|
177
|
+
const $derivation = derivation((t) => {
|
|
178
|
+
$signal1.watch(t);
|
|
179
|
+
$signal2.watch(t);
|
|
180
|
+
});
|
|
181
|
+
const effectFn = vi.fn();
|
|
182
|
+
effect((t) => {
|
|
183
|
+
$derivation.watch(t);
|
|
184
|
+
effectFn();
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
188
|
+
|
|
189
|
+
$signal1.trigger();
|
|
190
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
191
|
+
|
|
192
|
+
$signal2.trigger();
|
|
193
|
+
expect(effectFn).toHaveBeenCalledTimes(3);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test("called when is updated (multiple dependants)", () => {
|
|
197
|
+
const $signal = signal();
|
|
198
|
+
const $derivation1 = derivation((t) => $signal.watch(t));
|
|
199
|
+
const $derivation2 = derivation((t) => $signal.watch(t));
|
|
200
|
+
const effect1Fn = vi.fn();
|
|
201
|
+
const effect2Fn = vi.fn();
|
|
202
|
+
|
|
203
|
+
effect((t) => effect1Fn($derivation1.get(t)));
|
|
204
|
+
effect((t) => effect2Fn($derivation2.get(t)));
|
|
205
|
+
|
|
206
|
+
expect(effect1Fn).toHaveBeenCalledTimes(1);
|
|
207
|
+
expect(effect2Fn).toHaveBeenCalledTimes(1);
|
|
208
|
+
|
|
209
|
+
$signal.trigger();
|
|
210
|
+
expect(effect1Fn).toHaveBeenCalledTimes(2);
|
|
211
|
+
expect(effect2Fn).toHaveBeenCalledTimes(2);
|
|
212
|
+
|
|
213
|
+
$signal.trigger();
|
|
214
|
+
expect(effect1Fn).toHaveBeenCalledTimes(3);
|
|
215
|
+
expect(effect2Fn).toHaveBeenCalledTimes(3);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test("called when triggered (with state)", () => {
|
|
219
|
+
const $signal = signal();
|
|
220
|
+
const $state = state(1);
|
|
221
|
+
const $derivation = derivation((t) => {
|
|
222
|
+
$signal.watch(t);
|
|
223
|
+
return $state.get(t) * 2;
|
|
224
|
+
});
|
|
225
|
+
const effectFn = vi.fn();
|
|
226
|
+
effect((t) => effectFn($derivation.get(t)));
|
|
227
|
+
|
|
228
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
229
|
+
expect(effectFn).toHaveBeenLastCalledWith(2);
|
|
230
|
+
|
|
231
|
+
$signal.trigger();
|
|
232
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
233
|
+
expect(effectFn).toHaveBeenLastCalledWith(2);
|
|
234
|
+
|
|
235
|
+
$signal.trigger();
|
|
236
|
+
expect(effectFn).toHaveBeenCalledTimes(3);
|
|
237
|
+
expect(effectFn).toHaveBeenLastCalledWith(2);
|
|
238
|
+
|
|
239
|
+
$state.set(2);
|
|
240
|
+
expect(effectFn).toHaveBeenCalledTimes(4);
|
|
241
|
+
expect(effectFn).toHaveBeenLastCalledWith(4);
|
|
242
|
+
|
|
243
|
+
$signal.trigger();
|
|
244
|
+
expect(effectFn).toHaveBeenCalledTimes(5);
|
|
245
|
+
expect(effectFn).toHaveBeenLastCalledWith(4);
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
describe("from state", () => {
|
|
250
|
+
test("called when is updated", () => {
|
|
251
|
+
const $state = state(1);
|
|
252
|
+
const $derivation = derivation((t) => $state.get(t) * 2);
|
|
253
|
+
const effectFn = vi.fn();
|
|
254
|
+
effect((t) => effectFn($derivation.get(t)));
|
|
255
|
+
|
|
256
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
257
|
+
expect(effectFn).toHaveBeenLastCalledWith(2);
|
|
258
|
+
|
|
259
|
+
$state.set(2);
|
|
260
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
261
|
+
expect(effectFn).toHaveBeenLastCalledWith(4);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
test("called when is updated (chained dependencies)", () => {
|
|
265
|
+
const $state = state(1);
|
|
266
|
+
const $derivation1 = derivation((t) => $state.get(t) * 2);
|
|
267
|
+
const $derivation2 = derivation((t) => $derivation1.get(t) * 2);
|
|
268
|
+
const effectFn = vi.fn();
|
|
269
|
+
effect((t) => effectFn($derivation2.get(t)));
|
|
270
|
+
|
|
271
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
272
|
+
expect(effectFn).toHaveBeenLastCalledWith(4);
|
|
273
|
+
|
|
274
|
+
$state.set(2);
|
|
275
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
276
|
+
expect(effectFn).toHaveBeenLastCalledWith(8);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
test("called when is updated (multiple dependencies)", () => {
|
|
280
|
+
const $state1 = state(1);
|
|
281
|
+
const $state2 = state(2);
|
|
282
|
+
const $derivation = derivation((t) => $state1.get(t) + $state2.get(t));
|
|
283
|
+
const effectFn = vi.fn();
|
|
284
|
+
effect((t) => effectFn($derivation.get(t)));
|
|
285
|
+
|
|
286
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
287
|
+
expect(effectFn).toHaveBeenLastCalledWith(3);
|
|
288
|
+
|
|
289
|
+
$state1.set(2);
|
|
290
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
291
|
+
expect(effectFn).toHaveBeenLastCalledWith(4);
|
|
292
|
+
|
|
293
|
+
$state2.set(3);
|
|
294
|
+
expect(effectFn).toHaveBeenCalledTimes(3);
|
|
295
|
+
expect(effectFn).toHaveBeenLastCalledWith(5);
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
test("called when is updated (multiple dependants)", () => {
|
|
299
|
+
const $state = state(1);
|
|
300
|
+
const $derivation1 = derivation((t) => $state.get(t) * 2);
|
|
301
|
+
const $derivation2 = derivation((t) => $state.get(t) * 3);
|
|
302
|
+
const effect1Fn = vi.fn();
|
|
303
|
+
const effect2Fn = vi.fn();
|
|
304
|
+
|
|
305
|
+
effect((t) => effect1Fn($derivation1.get(t)));
|
|
306
|
+
effect((t) => effect2Fn($derivation2.get(t)));
|
|
307
|
+
|
|
308
|
+
expect(effect1Fn).toHaveBeenCalledTimes(1);
|
|
309
|
+
expect(effect1Fn).toHaveBeenLastCalledWith(2);
|
|
310
|
+
expect(effect2Fn).toHaveBeenCalledTimes(1);
|
|
311
|
+
expect(effect2Fn).toHaveBeenLastCalledWith(3);
|
|
312
|
+
|
|
313
|
+
$state.set(2);
|
|
314
|
+
expect(effect1Fn).toHaveBeenCalledTimes(2);
|
|
315
|
+
expect(effect1Fn).toHaveBeenLastCalledWith(4);
|
|
316
|
+
expect(effect2Fn).toHaveBeenCalledTimes(2);
|
|
317
|
+
expect(effect2Fn).toHaveBeenLastCalledWith(6);
|
|
318
|
+
|
|
319
|
+
$state.set(3);
|
|
320
|
+
expect(effect1Fn).toHaveBeenCalledTimes(3);
|
|
321
|
+
expect(effect1Fn).toHaveBeenLastCalledWith(6);
|
|
322
|
+
expect(effect2Fn).toHaveBeenCalledTimes(3);
|
|
323
|
+
expect(effect2Fn).toHaveBeenLastCalledWith(9);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
test("called when is updated (async)", async () => {
|
|
327
|
+
const $state = state(1);
|
|
328
|
+
const $derivation = derivation(async (t) => $state.get(t) * 2);
|
|
329
|
+
const effectFn = vi.fn();
|
|
330
|
+
effect((t) => effectFn($derivation.get(t)));
|
|
331
|
+
|
|
332
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
333
|
+
expect(await effectFn.mock.calls[0][0]).toEqual(2);
|
|
334
|
+
|
|
335
|
+
$state.set(2);
|
|
336
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
337
|
+
expect(await effectFn.mock.calls[1][0]).toEqual(4);
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
test("called when is updated (async chain)", async () => {
|
|
341
|
+
const $state = state(1);
|
|
342
|
+
const $derivation1 = derivation(async (t) => $state.get(t) * 2);
|
|
343
|
+
const $derivation2 = derivation(
|
|
344
|
+
async (t) => (await $derivation1.get(t)) * 2,
|
|
345
|
+
);
|
|
346
|
+
const effectFn = vi.fn();
|
|
347
|
+
effect((t) => effectFn($derivation2.get(t)));
|
|
348
|
+
|
|
349
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
350
|
+
expect(await effectFn.mock.calls[0][0]).toEqual(4);
|
|
351
|
+
|
|
352
|
+
$state.set(2);
|
|
353
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
354
|
+
expect(await effectFn.mock.calls[1][0]).toEqual(8);
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
test("NOT called when derivation is disposed", () => {
|
|
358
|
+
const $state = state(1);
|
|
359
|
+
const $derivation = derivation((t) => $state.get(t) * 2);
|
|
360
|
+
const effectFn = vi.fn();
|
|
361
|
+
effect((t) => effectFn($derivation.get(t)));
|
|
362
|
+
|
|
363
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
364
|
+
expect(effectFn).toHaveBeenLastCalledWith(2);
|
|
365
|
+
|
|
366
|
+
$state.set(2);
|
|
367
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
368
|
+
expect(effectFn).toHaveBeenLastCalledWith(4);
|
|
369
|
+
|
|
370
|
+
$derivation.dispose();
|
|
371
|
+
|
|
372
|
+
$state.set(3);
|
|
373
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
374
|
+
expect(effectFn).toHaveBeenLastCalledWith(4);
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
test("NOT called when derivation is disposed (watch)", () => {
|
|
378
|
+
const $state = state(1);
|
|
379
|
+
const $derivation = derivation((t) => $state.get(t) * 2);
|
|
380
|
+
const effectFn = vi.fn();
|
|
381
|
+
effect((t) => {
|
|
382
|
+
$derivation.watch(t);
|
|
383
|
+
effectFn();
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
387
|
+
|
|
388
|
+
$state.set(2);
|
|
389
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
390
|
+
|
|
391
|
+
$derivation.dispose();
|
|
392
|
+
|
|
393
|
+
$state.set(3);
|
|
394
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
test("NOT called when disposed", () => {
|
|
398
|
+
const $state = state(1);
|
|
399
|
+
const $derivation = derivation((t) => $state.get(t) * 2);
|
|
400
|
+
const effectFn = vi.fn();
|
|
401
|
+
const $effect = effect((t) => effectFn($derivation.get(t)));
|
|
402
|
+
|
|
403
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
404
|
+
expect(effectFn).toHaveBeenLastCalledWith(2);
|
|
405
|
+
|
|
406
|
+
$state.set(2);
|
|
407
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
408
|
+
expect(effectFn).toHaveBeenLastCalledWith(4);
|
|
409
|
+
|
|
410
|
+
$effect.dispose();
|
|
411
|
+
|
|
412
|
+
$state.set(3);
|
|
413
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
414
|
+
expect(effectFn).toHaveBeenLastCalledWith(4);
|
|
415
|
+
});
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
describe("from resource", () => {
|
|
419
|
+
test("called when is updated", async () => {
|
|
420
|
+
let resourceCounter = 0;
|
|
421
|
+
const fetchResource = async () => {
|
|
422
|
+
resourceCounter++;
|
|
423
|
+
return resourceCounter;
|
|
424
|
+
};
|
|
425
|
+
const $resource = resource(fetchResource);
|
|
426
|
+
const $derivation = derivation((t) => ($resource.get(t) ?? 0) * 2);
|
|
427
|
+
const effectFn = vi.fn();
|
|
428
|
+
effect((t) => effectFn($derivation.get(t)));
|
|
429
|
+
|
|
430
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
431
|
+
expect(effectFn).toHaveBeenLastCalledWith(0);
|
|
432
|
+
|
|
433
|
+
await $resource.fetch();
|
|
434
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
435
|
+
expect(effectFn).toHaveBeenLastCalledWith(2);
|
|
436
|
+
|
|
437
|
+
await $resource.fetch();
|
|
438
|
+
expect(effectFn).toHaveBeenCalledTimes(3);
|
|
439
|
+
expect(effectFn).toHaveBeenLastCalledWith(4);
|
|
440
|
+
});
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
describe("complex test", () => {
|
|
444
|
+
test("test", () => {
|
|
445
|
+
const obj1 = {
|
|
446
|
+
cond: state(false),
|
|
447
|
+
num: state(2),
|
|
448
|
+
dispose: (options: { self: boolean }) => {
|
|
449
|
+
obj1.cond.dispose(options);
|
|
450
|
+
obj1.num.dispose(options);
|
|
451
|
+
},
|
|
452
|
+
};
|
|
453
|
+
const obj2 = {
|
|
454
|
+
cond: state(false),
|
|
455
|
+
num: state(4),
|
|
456
|
+
dispose: (options: { self: boolean }) => {
|
|
457
|
+
obj2.cond.dispose(options);
|
|
458
|
+
obj2.num.dispose(options);
|
|
459
|
+
},
|
|
460
|
+
};
|
|
461
|
+
const $state = state(obj1);
|
|
462
|
+
const $derivationCond = derivation((t) => $state.get(t).cond.get(t));
|
|
463
|
+
const $derivationNum = derivation((t) => $state.get(t).num.get(t) * 2);
|
|
464
|
+
const effectCondFn = vi.fn();
|
|
465
|
+
const effectNumFn = vi.fn();
|
|
466
|
+
effect((t) => effectCondFn($derivationCond.get(t)));
|
|
467
|
+
effect((t) => effectNumFn($derivationNum.get(t)));
|
|
468
|
+
|
|
469
|
+
expect(effectCondFn).toHaveBeenCalledTimes(1);
|
|
470
|
+
expect(effectCondFn).toHaveBeenLastCalledWith(false);
|
|
471
|
+
expect(effectNumFn).toHaveBeenCalledTimes(1);
|
|
472
|
+
expect(effectNumFn).toHaveBeenLastCalledWith(4);
|
|
473
|
+
|
|
474
|
+
$state.pick().num.set(3);
|
|
475
|
+
expect(effectCondFn).toHaveBeenCalledTimes(1);
|
|
476
|
+
expect(effectCondFn).toHaveBeenLastCalledWith(false);
|
|
477
|
+
expect(effectNumFn).toHaveBeenCalledTimes(2);
|
|
478
|
+
expect(effectNumFn).toHaveBeenLastCalledWith(6);
|
|
479
|
+
|
|
480
|
+
$state.pick().cond.set(true);
|
|
481
|
+
expect(effectCondFn).toHaveBeenCalledTimes(2);
|
|
482
|
+
expect(effectCondFn).toHaveBeenLastCalledWith(true);
|
|
483
|
+
expect(effectNumFn).toHaveBeenCalledTimes(2);
|
|
484
|
+
expect(effectNumFn).toHaveBeenLastCalledWith(6);
|
|
485
|
+
|
|
486
|
+
$state.set(obj2);
|
|
487
|
+
expect(effectCondFn).toHaveBeenCalledTimes(3);
|
|
488
|
+
expect(effectCondFn).toHaveBeenLastCalledWith(false);
|
|
489
|
+
expect(effectNumFn).toHaveBeenCalledTimes(3);
|
|
490
|
+
expect(effectNumFn).toHaveBeenLastCalledWith(8);
|
|
491
|
+
|
|
492
|
+
$state.pick().cond.set(true);
|
|
493
|
+
expect(effectCondFn).toHaveBeenCalledTimes(4); // fails -> called 3 times
|
|
494
|
+
expect(effectCondFn).toHaveBeenLastCalledWith(true);
|
|
495
|
+
expect(effectNumFn).toHaveBeenCalledTimes(3);
|
|
496
|
+
expect(effectNumFn).toHaveBeenLastCalledWith(8);
|
|
497
|
+
|
|
498
|
+
$state.pick().num.set(5);
|
|
499
|
+
expect(effectCondFn).toHaveBeenCalledTimes(4);
|
|
500
|
+
expect(effectCondFn).toHaveBeenLastCalledWith(true);
|
|
501
|
+
expect(effectNumFn).toHaveBeenCalledTimes(4);
|
|
502
|
+
expect(effectNumFn).toHaveBeenLastCalledWith(10);
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
test("test", () => {
|
|
506
|
+
const obj = {
|
|
507
|
+
cond: state(false),
|
|
508
|
+
b: state(2),
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
const $state = state(obj);
|
|
512
|
+
const $derivation = derivation((t) => {
|
|
513
|
+
const cond = $state.get(t).cond.get(t);
|
|
514
|
+
if (cond) {
|
|
515
|
+
return $state.get(t).b.get(t) * 2;
|
|
516
|
+
}
|
|
517
|
+
return 0;
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
const effectFn = vi.fn();
|
|
521
|
+
effect((t) => effectFn($derivation.get(t)));
|
|
522
|
+
|
|
523
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
524
|
+
expect(effectFn).toHaveBeenLastCalledWith(0);
|
|
525
|
+
|
|
526
|
+
$state.pick().cond.set(true);
|
|
527
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
528
|
+
expect(effectFn).toHaveBeenLastCalledWith(4);
|
|
529
|
+
|
|
530
|
+
$state.set({ cond: state(false), b: state(3) });
|
|
531
|
+
expect(effectFn).toHaveBeenCalledTimes(3);
|
|
532
|
+
expect(effectFn).toHaveBeenLastCalledWith(0);
|
|
533
|
+
|
|
534
|
+
$state.pick().cond.set(true);
|
|
535
|
+
expect(effectFn).toHaveBeenCalledTimes(4);
|
|
536
|
+
expect(effectFn).toHaveBeenLastCalledWith(6);
|
|
537
|
+
});
|
|
538
|
+
});
|
|
544
539
|
});
|