@ersbeth/picoflow 0.2.4 → 1.0.1
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 +9 -134
- package/biome.json +32 -32
- package/dist/picoflow.js +610 -436
- 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 -23
- 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/resource.test.ts
CHANGED
|
@@ -2,128 +2,126 @@ import { describe, expect, test, vi } from "vitest";
|
|
|
2
2
|
import { effect, resource } from "#package";
|
|
3
3
|
|
|
4
4
|
describe("resource", () => {
|
|
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
|
-
});
|
|
5
|
+
test("is updated", async () => {
|
|
6
|
+
let resourceCounter = 0;
|
|
7
|
+
const fetchResource = async () => {
|
|
8
|
+
resourceCounter++;
|
|
9
|
+
return resourceCounter;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const $resource = resource(fetchResource);
|
|
13
|
+
expect($resource.pick()).toBe(undefined);
|
|
14
|
+
|
|
15
|
+
await $resource.fetch();
|
|
16
|
+
expect($resource.pick()).toBe(1);
|
|
17
|
+
|
|
18
|
+
await $resource.fetch();
|
|
19
|
+
expect($resource.pick()).toBe(2);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("fetch throws when disposed", async () => {
|
|
23
|
+
let resourceCounter = 0;
|
|
24
|
+
const fetchResource = async () => {
|
|
25
|
+
resourceCounter++;
|
|
26
|
+
return resourceCounter;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const $resource = resource(fetchResource);
|
|
30
|
+
expect($resource.pick()).toBe(undefined);
|
|
31
|
+
|
|
32
|
+
await $resource.fetch();
|
|
33
|
+
expect($resource.pick()).toBe(1);
|
|
34
|
+
|
|
35
|
+
$resource.dispose();
|
|
36
|
+
await expect(() => $resource.fetch()).rejects.toThrow(
|
|
37
|
+
"[PicoFlow] Primitive is disposed",
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("get throws when disposed", async () => {
|
|
42
|
+
let resourceCounter = 0;
|
|
43
|
+
const fetchResource = async () => {
|
|
44
|
+
resourceCounter++;
|
|
45
|
+
return resourceCounter;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const $resource = resource(fetchResource);
|
|
49
|
+
expect($resource.pick()).toBe(undefined);
|
|
50
|
+
|
|
51
|
+
await $resource.fetch();
|
|
52
|
+
expect($resource.pick()).toBe(1);
|
|
53
|
+
|
|
54
|
+
$resource.dispose();
|
|
55
|
+
expect(() => $resource.pick()).toThrow("[PicoFlow] Primitive is disposed");
|
|
56
|
+
});
|
|
59
57
|
});
|
|
60
58
|
|
|
61
59
|
describe("effect", () => {
|
|
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
|
-
|
|
60
|
+
test("called when resource updated", async () => {
|
|
61
|
+
let resourceCounter = 0;
|
|
62
|
+
const fetchResource = async () => {
|
|
63
|
+
resourceCounter++;
|
|
64
|
+
return resourceCounter;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const $resource = resource(fetchResource);
|
|
68
|
+
const effectFn = vi.fn();
|
|
69
|
+
effect((t) => effectFn($resource.get(t)));
|
|
70
|
+
|
|
71
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
72
|
+
expect(effectFn).toHaveBeenLastCalledWith(undefined);
|
|
73
|
+
|
|
74
|
+
await $resource.fetch();
|
|
75
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
76
|
+
expect(effectFn).toHaveBeenLastCalledWith(1);
|
|
77
|
+
|
|
78
|
+
await $resource.fetch();
|
|
79
|
+
expect(effectFn).toHaveBeenCalledTimes(3);
|
|
80
|
+
expect(effectFn).toHaveBeenLastCalledWith(2);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("NOT updated when value don't change", async () => {
|
|
84
|
+
const fetchResource = async () => {
|
|
85
|
+
return 0;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const $resource = resource(fetchResource);
|
|
89
|
+
const effectFn = vi.fn();
|
|
90
|
+
effect((t) => effectFn($resource.get(t)));
|
|
91
|
+
|
|
92
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
93
|
+
expect(effectFn).toHaveBeenLastCalledWith(undefined);
|
|
94
|
+
|
|
95
|
+
await $resource.fetch();
|
|
96
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
97
|
+
expect(effectFn).toHaveBeenLastCalledWith(0);
|
|
98
|
+
|
|
99
|
+
await $resource.fetch();
|
|
100
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
101
|
+
expect(effectFn).toHaveBeenLastCalledWith(0);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("NOT called when disposed", async () => {
|
|
105
|
+
let resourceCounter = 0;
|
|
106
|
+
const fetchResource = async () => {
|
|
107
|
+
resourceCounter++;
|
|
108
|
+
return resourceCounter;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const $resource = resource(fetchResource);
|
|
112
|
+
const effectFn = vi.fn();
|
|
113
|
+
const $effect = effect((t) => effectFn($resource.get(t)));
|
|
114
|
+
|
|
115
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
116
|
+
expect(effectFn).toHaveBeenLastCalledWith(undefined);
|
|
117
|
+
|
|
118
|
+
await $resource.fetch();
|
|
119
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
120
|
+
expect(effectFn).toHaveBeenLastCalledWith(1);
|
|
121
|
+
|
|
122
|
+
$effect.dispose();
|
|
123
|
+
await $resource.fetch();
|
|
124
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
125
|
+
expect(effectFn).toHaveBeenLastCalledWith(1);
|
|
126
|
+
});
|
|
129
127
|
});
|
|
@@ -2,107 +2,105 @@ import { describe, expect, test, vi } from "vitest";
|
|
|
2
2
|
import { effect, resourceAsync } from "#package";
|
|
3
3
|
|
|
4
4
|
describe("resourceAsync", () => {
|
|
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
|
-
});
|
|
5
|
+
test("is updated", async () => {
|
|
6
|
+
let resourceCounter = 0;
|
|
7
|
+
const fetchResource = async () => {
|
|
8
|
+
resourceCounter++;
|
|
9
|
+
return resourceCounter;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const $resource = resourceAsync(fetchResource);
|
|
13
|
+
expect(await $resource.pick()).toBe(1);
|
|
14
|
+
|
|
15
|
+
$resource.fetch();
|
|
16
|
+
expect(await $resource.pick()).toBe(2);
|
|
17
|
+
|
|
18
|
+
$resource.fetch();
|
|
19
|
+
expect(await $resource.pick()).toBe(3);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("fetch throws when disposed", async () => {
|
|
23
|
+
let resourceCounter = 0;
|
|
24
|
+
const fetchResource = async () => {
|
|
25
|
+
resourceCounter++;
|
|
26
|
+
return resourceCounter;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const $resource = resourceAsync(fetchResource);
|
|
30
|
+
expect(await $resource.pick()).toBe(1);
|
|
31
|
+
|
|
32
|
+
$resource.fetch();
|
|
33
|
+
expect(await $resource.pick()).toBe(2);
|
|
34
|
+
|
|
35
|
+
$resource.dispose();
|
|
36
|
+
await expect(() => $resource.fetch()).rejects.toThrow(
|
|
37
|
+
"[PicoFlow] Primitive is disposed",
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("get throws when disposed", async () => {
|
|
42
|
+
let resourceCounter = 0;
|
|
43
|
+
const fetchResource = async () => {
|
|
44
|
+
resourceCounter++;
|
|
45
|
+
return resourceCounter;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const $resource = resourceAsync(fetchResource);
|
|
49
|
+
expect(await $resource.pick()).toBe(1);
|
|
50
|
+
|
|
51
|
+
$resource.fetch();
|
|
52
|
+
expect(await $resource.pick()).toBe(2);
|
|
53
|
+
|
|
54
|
+
$resource.dispose();
|
|
55
|
+
expect(() => $resource.pick()).toThrow("[PicoFlow] Primitive is disposed");
|
|
56
|
+
});
|
|
59
57
|
});
|
|
60
58
|
|
|
61
59
|
describe("effect", () => {
|
|
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
|
-
|
|
60
|
+
test("called when resource updated", async () => {
|
|
61
|
+
let resourceCounter = 0;
|
|
62
|
+
const fetchResource = async () => {
|
|
63
|
+
resourceCounter++;
|
|
64
|
+
return resourceCounter;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const $resource = resourceAsync(fetchResource);
|
|
68
|
+
const effectFn = vi.fn();
|
|
69
|
+
effect((t) => effectFn($resource.get(t)));
|
|
70
|
+
|
|
71
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
72
|
+
expect(await effectFn.mock.calls[0][0]).toBe(1);
|
|
73
|
+
|
|
74
|
+
$resource.fetch();
|
|
75
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
76
|
+
expect(await effectFn.mock.calls[1][0]).toBe(2);
|
|
77
|
+
|
|
78
|
+
await $resource.fetch();
|
|
79
|
+
expect(effectFn).toHaveBeenCalledTimes(3);
|
|
80
|
+
expect(await effectFn.mock.calls[2][0]).toBe(3);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("NOT called when disposed", async () => {
|
|
84
|
+
let resourceCounter = 0;
|
|
85
|
+
const fetchResource = async () => {
|
|
86
|
+
resourceCounter++;
|
|
87
|
+
return resourceCounter;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const $resource = resourceAsync(fetchResource);
|
|
91
|
+
const effectFn = vi.fn();
|
|
92
|
+
const $effect = effect((t) => effectFn($resource.get(t)));
|
|
93
|
+
|
|
94
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
95
|
+
expect(await effectFn.mock.calls[0][0]).toBe(1);
|
|
96
|
+
|
|
97
|
+
await $resource.fetch();
|
|
98
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
99
|
+
expect(await effectFn.mock.calls[1][0]).toBe(2);
|
|
100
|
+
|
|
101
|
+
$effect.dispose();
|
|
102
|
+
await $resource.fetch();
|
|
103
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
104
|
+
expect(await effectFn.mock.calls[1][0]).toBe(2);
|
|
105
|
+
});
|
|
108
106
|
});
|
package/test/signal.test.ts
CHANGED
|
@@ -2,75 +2,71 @@ import { describe, expect, test, vi } from "vitest";
|
|
|
2
2
|
import { effect, signal } from "#package";
|
|
3
3
|
|
|
4
4
|
describe("signal", () => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
test("disposed is correct", () => {
|
|
6
|
+
const $signal = signal();
|
|
7
|
+
expect($signal.disposed).toBe(false);
|
|
8
|
+
$signal.dispose();
|
|
9
|
+
expect($signal.disposed).toBe(true);
|
|
10
|
+
});
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
test("throws when disposed", () => {
|
|
13
|
+
const $signal = signal();
|
|
14
|
+
const effectFn = vi.fn();
|
|
15
|
+
effect((t) => {
|
|
16
|
+
$signal.watch(t);
|
|
17
|
+
effectFn();
|
|
18
|
+
});
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
$signal.trigger();
|
|
23
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
);
|
|
29
|
-
});
|
|
25
|
+
$signal.dispose();
|
|
26
|
+
expect(() => $signal.trigger()).toThrow("[PicoFlow] Primitive is disposed");
|
|
27
|
+
});
|
|
30
28
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
);
|
|
37
|
-
});
|
|
29
|
+
test("throws when disposed twice", () => {
|
|
30
|
+
const $signal = signal();
|
|
31
|
+
$signal.dispose();
|
|
32
|
+
expect(() => $signal.dispose()).toThrow("[PicoFlow] Primitive is disposed");
|
|
33
|
+
});
|
|
38
34
|
});
|
|
39
35
|
|
|
40
36
|
describe("effect", () => {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
37
|
+
test("called when signal triggered", () => {
|
|
38
|
+
const $signal = signal();
|
|
39
|
+
const effectFn = vi.fn();
|
|
40
|
+
effect((t) => {
|
|
41
|
+
$signal.watch(t);
|
|
42
|
+
effectFn();
|
|
43
|
+
});
|
|
48
44
|
|
|
49
|
-
|
|
45
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
50
46
|
|
|
51
|
-
|
|
52
|
-
|
|
47
|
+
$signal.trigger();
|
|
48
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
53
49
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
50
|
+
$signal.trigger();
|
|
51
|
+
expect(effectFn).toHaveBeenCalledTimes(3);
|
|
52
|
+
});
|
|
57
53
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
54
|
+
test("NOT called when disposed", () => {
|
|
55
|
+
const $signal = signal();
|
|
56
|
+
const effectFn = vi.fn();
|
|
57
|
+
const $effect = effect((t) => {
|
|
58
|
+
$signal.watch(t);
|
|
59
|
+
effectFn();
|
|
60
|
+
});
|
|
65
61
|
|
|
66
|
-
|
|
62
|
+
expect(effectFn).toHaveBeenCalledTimes(1);
|
|
67
63
|
|
|
68
|
-
|
|
69
|
-
|
|
64
|
+
$signal.trigger();
|
|
65
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
70
66
|
|
|
71
|
-
|
|
67
|
+
$effect.dispose();
|
|
72
68
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
69
|
+
$signal.trigger();
|
|
70
|
+
expect(effectFn).toHaveBeenCalledTimes(2);
|
|
71
|
+
});
|
|
76
72
|
});
|