@ersbeth/picoflow 1.0.1 → 1.1.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/unifier-flowresource-avec-flowderivation-c9506e24.plan.md +372 -0
- package/README.md +17 -1
- package/biome.json +4 -1
- package/dist/picoflow.js +1155 -582
- package/dist/types/flow/base/flowDisposable.d.ts +67 -0
- package/dist/types/flow/base/flowDisposable.d.ts.map +1 -0
- package/dist/types/flow/base/flowEffect.d.ts +127 -0
- package/dist/types/flow/base/flowEffect.d.ts.map +1 -0
- package/dist/types/flow/base/flowGraph.d.ts +97 -0
- package/dist/types/flow/base/flowGraph.d.ts.map +1 -0
- package/dist/types/flow/base/flowSignal.d.ts +134 -0
- package/dist/types/flow/base/flowSignal.d.ts.map +1 -0
- package/dist/types/flow/base/flowTracker.d.ts +15 -0
- package/dist/types/flow/base/flowTracker.d.ts.map +1 -0
- package/dist/types/flow/base/index.d.ts +7 -0
- package/dist/types/flow/base/index.d.ts.map +1 -0
- package/dist/types/flow/base/utils.d.ts +20 -0
- package/dist/types/flow/base/utils.d.ts.map +1 -0
- package/dist/types/{advanced/array.d.ts → flow/collections/flowArray.d.ts} +50 -12
- package/dist/types/flow/collections/flowArray.d.ts.map +1 -0
- package/dist/types/flow/collections/flowMap.d.ts +224 -0
- package/dist/types/flow/collections/flowMap.d.ts.map +1 -0
- package/dist/types/flow/collections/index.d.ts +3 -0
- package/dist/types/flow/collections/index.d.ts.map +1 -0
- package/dist/types/flow/index.d.ts +4 -0
- package/dist/types/flow/index.d.ts.map +1 -0
- package/dist/types/flow/nodes/async/flowConstantAsync.d.ts +137 -0
- package/dist/types/flow/nodes/async/flowConstantAsync.d.ts.map +1 -0
- package/dist/types/flow/nodes/async/flowDerivationAsync.d.ts +137 -0
- package/dist/types/flow/nodes/async/flowDerivationAsync.d.ts.map +1 -0
- package/dist/types/flow/nodes/async/flowNodeAsync.d.ts +343 -0
- package/dist/types/flow/nodes/async/flowNodeAsync.d.ts.map +1 -0
- package/dist/types/flow/nodes/async/flowReadonlyAsync.d.ts +81 -0
- package/dist/types/flow/nodes/async/flowReadonlyAsync.d.ts.map +1 -0
- package/dist/types/flow/nodes/async/flowStateAsync.d.ts +111 -0
- package/dist/types/flow/nodes/async/flowStateAsync.d.ts.map +1 -0
- package/dist/types/flow/nodes/async/index.d.ts +6 -0
- package/dist/types/flow/nodes/async/index.d.ts.map +1 -0
- package/dist/types/flow/nodes/index.d.ts +3 -0
- package/dist/types/flow/nodes/index.d.ts.map +1 -0
- package/dist/types/flow/nodes/sync/flowConstant.d.ts +108 -0
- package/dist/types/flow/nodes/sync/flowConstant.d.ts.map +1 -0
- package/dist/types/flow/nodes/sync/flowDerivation.d.ts +100 -0
- package/dist/types/flow/nodes/sync/flowDerivation.d.ts.map +1 -0
- package/dist/types/flow/nodes/sync/flowNode.d.ts +314 -0
- package/dist/types/flow/nodes/sync/flowNode.d.ts.map +1 -0
- package/dist/types/flow/nodes/sync/flowReadonly.d.ts +57 -0
- package/dist/types/flow/nodes/sync/flowReadonly.d.ts.map +1 -0
- package/dist/types/flow/nodes/sync/flowState.d.ts +96 -0
- package/dist/types/flow/nodes/sync/flowState.d.ts.map +1 -0
- package/dist/types/flow/nodes/sync/index.d.ts +6 -0
- package/dist/types/flow/nodes/sync/index.d.ts.map +1 -0
- package/dist/types/index.d.ts +2 -4
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/solid/converters.d.ts +34 -45
- 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 +1 -0
- package/dist/types/solid/primitives.d.ts.map +1 -1
- package/docs/.vitepress/config.mts +1 -1
- package/docs/api/typedoc-sidebar.json +81 -1
- package/package.json +60 -58
- package/src/flow/base/flowDisposable.ts +71 -0
- package/src/flow/base/flowEffect.ts +171 -0
- package/src/flow/base/flowGraph.ts +288 -0
- package/src/flow/base/flowSignal.ts +207 -0
- package/src/flow/base/flowTracker.ts +17 -0
- package/src/flow/base/index.ts +6 -0
- package/src/flow/base/utils.ts +19 -0
- package/src/flow/collections/flowArray.ts +409 -0
- package/src/flow/collections/flowMap.ts +398 -0
- package/src/flow/collections/index.ts +2 -0
- package/src/flow/index.ts +3 -0
- package/src/flow/nodes/async/flowConstantAsync.ts +142 -0
- package/src/flow/nodes/async/flowDerivationAsync.ts +143 -0
- package/src/flow/nodes/async/flowNodeAsync.ts +474 -0
- package/src/flow/nodes/async/flowReadonlyAsync.ts +81 -0
- package/src/flow/nodes/async/flowStateAsync.ts +116 -0
- package/src/flow/nodes/async/index.ts +5 -0
- package/src/flow/nodes/await/advanced/index.ts +5 -0
- package/src/{advanced → flow/nodes/await/advanced}/resource.ts +37 -3
- package/src/{advanced → flow/nodes/await/advanced}/resourceAsync.ts +35 -3
- package/src/{advanced → flow/nodes/await/advanced}/stream.ts +40 -2
- package/src/{advanced → flow/nodes/await/advanced}/streamAsync.ts +38 -3
- package/src/flow/nodes/await/flowConstantAwait.ts +154 -0
- package/src/flow/nodes/await/flowDerivationAwait.ts +154 -0
- package/src/flow/nodes/await/flowNodeAwait.ts +508 -0
- package/src/flow/nodes/await/flowReadonlyAwait.ts +89 -0
- package/src/flow/nodes/await/flowStateAwait.ts +130 -0
- package/src/flow/nodes/await/index.ts +5 -0
- package/src/flow/nodes/index.ts +3 -0
- package/src/flow/nodes/sync/flowConstant.ts +111 -0
- package/src/flow/nodes/sync/flowDerivation.ts +105 -0
- package/src/flow/nodes/sync/flowNode.ts +439 -0
- package/src/flow/nodes/sync/flowReadonly.ts +57 -0
- package/src/flow/nodes/sync/flowState.ts +101 -0
- package/src/flow/nodes/sync/index.ts +5 -0
- package/src/index.ts +2 -47
- package/src/solid/converters.ts +60 -199
- package/src/solid/index.ts +2 -8
- package/src/solid/primitives.ts +4 -0
- package/test/base/flowEffect.test.ts +108 -0
- package/test/base/flowGraph.test.ts +485 -0
- package/test/base/flowSignal.test.ts +372 -0
- package/test/collections/flowArray.asyncStates.test.ts +1553 -0
- package/test/collections/flowArray.scalars.test.ts +1129 -0
- package/test/collections/flowArray.states.test.ts +1365 -0
- package/test/collections/flowMap.asyncStates.test.ts +1105 -0
- package/test/collections/flowMap.scalars.test.ts +877 -0
- package/test/collections/flowMap.states.test.ts +1097 -0
- package/test/nodes/async/flowConstantAsync.test.ts +860 -0
- package/test/nodes/async/flowDerivationAsync.test.ts +1517 -0
- package/test/nodes/async/flowStateAsync.test.ts +1387 -0
- package/test/{resource.test.ts → nodes/await/advanced/resource.test.ts} +21 -19
- package/test/{resourceAsync.test.ts → nodes/await/advanced/resourceAsync.test.ts} +3 -1
- package/test/{stream.test.ts → nodes/await/advanced/stream.test.ts} +30 -28
- package/test/{streamAsync.test.ts → nodes/await/advanced/streamAsync.test.ts} +16 -14
- package/test/nodes/await/flowConstantAwait.test.ts +643 -0
- package/test/nodes/await/flowDerivationAwait.test.ts +1583 -0
- package/test/nodes/await/flowStateAwait.test.ts +999 -0
- package/test/nodes/mixed/derivation.test.ts +1527 -0
- package/test/nodes/sync/flowConstant.test.ts +620 -0
- package/test/nodes/sync/flowDerivation.test.ts +1373 -0
- package/test/nodes/sync/flowState.test.ts +945 -0
- package/test/solid/converters.test.ts +721 -0
- package/test/solid/primitives.test.ts +1031 -0
- package/tsconfig.json +2 -1
- package/vitest.config.ts +7 -1
- package/IMPLEMENTATION_GUIDE.md +0 -1578
- package/dist/types/advanced/array.d.ts.map +0 -1
- package/dist/types/advanced/index.d.ts +0 -9
- package/dist/types/advanced/index.d.ts.map +0 -1
- package/dist/types/advanced/map.d.ts +0 -166
- package/dist/types/advanced/map.d.ts.map +0 -1
- package/dist/types/advanced/resource.d.ts +0 -78
- package/dist/types/advanced/resource.d.ts.map +0 -1
- package/dist/types/advanced/resourceAsync.d.ts +0 -56
- package/dist/types/advanced/resourceAsync.d.ts.map +0 -1
- package/dist/types/advanced/stream.d.ts +0 -117
- package/dist/types/advanced/stream.d.ts.map +0 -1
- package/dist/types/advanced/streamAsync.d.ts +0 -97
- package/dist/types/advanced/streamAsync.d.ts.map +0 -1
- package/dist/types/basic/constant.d.ts +0 -60
- package/dist/types/basic/constant.d.ts.map +0 -1
- package/dist/types/basic/derivation.d.ts +0 -89
- package/dist/types/basic/derivation.d.ts.map +0 -1
- package/dist/types/basic/disposable.d.ts +0 -82
- package/dist/types/basic/disposable.d.ts.map +0 -1
- package/dist/types/basic/effect.d.ts +0 -67
- package/dist/types/basic/effect.d.ts.map +0 -1
- package/dist/types/basic/index.d.ts +0 -10
- package/dist/types/basic/index.d.ts.map +0 -1
- package/dist/types/basic/observable.d.ts +0 -83
- package/dist/types/basic/observable.d.ts.map +0 -1
- package/dist/types/basic/signal.d.ts +0 -69
- package/dist/types/basic/signal.d.ts.map +0 -1
- package/dist/types/basic/state.d.ts +0 -47
- package/dist/types/basic/state.d.ts.map +0 -1
- package/dist/types/basic/trackingContext.d.ts +0 -33
- package/dist/types/basic/trackingContext.d.ts.map +0 -1
- package/dist/types/creators.d.ts +0 -340
- package/dist/types/creators.d.ts.map +0 -1
- package/src/advanced/array.ts +0 -222
- package/src/advanced/index.ts +0 -12
- package/src/advanced/map.ts +0 -193
- package/src/basic/constant.ts +0 -97
- package/src/basic/derivation.ts +0 -147
- package/src/basic/disposable.ts +0 -86
- package/src/basic/effect.ts +0 -104
- package/src/basic/index.ts +0 -9
- package/src/basic/observable.ts +0 -109
- package/src/basic/signal.ts +0 -145
- package/src/basic/state.ts +0 -60
- package/src/basic/trackingContext.ts +0 -45
- package/src/creators.ts +0 -395
- package/test/array.test.ts +0 -600
- package/test/constant.test.ts +0 -44
- package/test/derivation.test.ts +0 -539
- package/test/effect.test.ts +0 -29
- package/test/map.test.ts +0 -240
- package/test/signal.test.ts +0 -72
- package/test/state.test.ts +0 -212
|
@@ -0,0 +1,877 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { effect, FlowGraph, map, state } from "#package";
|
|
3
|
+
|
|
4
|
+
describe("FlowMap", () => {
|
|
5
|
+
describe("unit", () => {
|
|
6
|
+
describe("initialization", () => {
|
|
7
|
+
it("should initialize with provided Record values", async () => {
|
|
8
|
+
const $map = map({
|
|
9
|
+
key1: 1,
|
|
10
|
+
key2: 2,
|
|
11
|
+
key3: 3,
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
expect(Array.from((await $map.pick()).entries())).toEqual([
|
|
15
|
+
["key1", 1],
|
|
16
|
+
["key2", 2],
|
|
17
|
+
["key3", 3],
|
|
18
|
+
]);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("should initialize with provided Map", async () => {
|
|
22
|
+
const initialMap = new Map([
|
|
23
|
+
["key1", 1],
|
|
24
|
+
["key2", 2],
|
|
25
|
+
]);
|
|
26
|
+
const $map = map(initialMap);
|
|
27
|
+
|
|
28
|
+
expect(Array.from((await $map.pick()).entries())).toEqual([
|
|
29
|
+
["key1", 1],
|
|
30
|
+
["key2", 2],
|
|
31
|
+
]);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should initialize with empty map when no value provided", async () => {
|
|
35
|
+
const $map = map<string, number>();
|
|
36
|
+
|
|
37
|
+
expect(Array.from((await $map.pick()).entries())).toEqual([]);
|
|
38
|
+
expect((await $map.pick()).size).toBe(0);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should initialize $lastAction with set action on creation", async () => {
|
|
42
|
+
const $map = map({ key1: 1, key2: 2 });
|
|
43
|
+
const action = await $map.$lastAction.pick();
|
|
44
|
+
expect(action.type).toBe("set");
|
|
45
|
+
if (action.type === "set") {
|
|
46
|
+
expect(Array.from(action.map.entries())).toEqual([
|
|
47
|
+
["key1", 1],
|
|
48
|
+
["key2", 2],
|
|
49
|
+
]);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("should initialize $lastAction with empty map when no value provided", async () => {
|
|
54
|
+
const $map = map<string, number>();
|
|
55
|
+
const action = await $map.$lastAction.pick();
|
|
56
|
+
expect(action.type).toBe("set");
|
|
57
|
+
if (action.type === "set") {
|
|
58
|
+
expect(action.map.size).toBe(0);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe("disposal", () => {
|
|
64
|
+
it("should have disposed property set to false initially and true after disposal", () => {
|
|
65
|
+
const $map = map({ key1: 1, key2: 2 });
|
|
66
|
+
expect($map.disposed).toBe(false);
|
|
67
|
+
$map.dispose();
|
|
68
|
+
expect($map.disposed).toBe(true);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("should throw error when disposed twice", () => {
|
|
72
|
+
const $map = map({ key1: 1, key2: 2 });
|
|
73
|
+
$map.dispose();
|
|
74
|
+
expect(() => $map.dispose()).toThrow(
|
|
75
|
+
"[PicoFlow] Primitive is disposed",
|
|
76
|
+
);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("should throw when pick is called after disposal", async () => {
|
|
80
|
+
const $map = map({ key1: 1, key2: 2, key3: 3 });
|
|
81
|
+
$map.dispose();
|
|
82
|
+
await expect($map.pick()).rejects.toThrow(
|
|
83
|
+
"[PicoFlow] Primitive is disposed",
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("should throw when set is called after disposal", async () => {
|
|
88
|
+
const $map = map<string, number>({ key1: 1 });
|
|
89
|
+
$map.dispose();
|
|
90
|
+
await expect($map.set(new Map([["key2", 2]]))).rejects.toThrow(
|
|
91
|
+
"[PicoFlow] Primitive is disposed",
|
|
92
|
+
);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("should throw when add is called after disposal", async () => {
|
|
96
|
+
const $map = map<string, number>();
|
|
97
|
+
$map.dispose();
|
|
98
|
+
await expect($map.add("key1", 2)).rejects.toThrow(
|
|
99
|
+
"[PicoFlow] Primitive is disposed",
|
|
100
|
+
);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("should throw when update is called after disposal", async () => {
|
|
104
|
+
const $map = map<string, number>();
|
|
105
|
+
$map.add("key1", 1);
|
|
106
|
+
$map.dispose();
|
|
107
|
+
await expect($map.update("key1", 2)).rejects.toThrow(
|
|
108
|
+
"[PicoFlow] Primitive is disposed",
|
|
109
|
+
);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("should throw when delete is called after disposal", async () => {
|
|
113
|
+
const $map = map<string, number>({ key1: 1, key2: 2, key3: 3 });
|
|
114
|
+
$map.dispose();
|
|
115
|
+
await expect($map.delete("key1")).rejects.toThrow(
|
|
116
|
+
"[PicoFlow] Primitive is disposed",
|
|
117
|
+
);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("should throw when clear is called after disposal", async () => {
|
|
121
|
+
const $map = map<string, number>({ key1: 1, key2: 2 });
|
|
122
|
+
$map.dispose();
|
|
123
|
+
await expect($map.clear()).rejects.toThrow(
|
|
124
|
+
"[PicoFlow] Primitive is disposed",
|
|
125
|
+
);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("should dispose values when FlowMap is disposed", () => {
|
|
129
|
+
const $value1 = state(1);
|
|
130
|
+
const $value2 = state(2);
|
|
131
|
+
const $value3 = state(3);
|
|
132
|
+
const $map = map(
|
|
133
|
+
new Map([
|
|
134
|
+
["key1", $value1],
|
|
135
|
+
["key2", $value2],
|
|
136
|
+
["key3", $value3],
|
|
137
|
+
]),
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
expect($value1.disposed).toBe(false);
|
|
141
|
+
expect($value2.disposed).toBe(false);
|
|
142
|
+
expect($value3.disposed).toBe(false);
|
|
143
|
+
|
|
144
|
+
$map.dispose();
|
|
145
|
+
|
|
146
|
+
expect($value1.disposed).toBe(true);
|
|
147
|
+
expect($value2.disposed).toBe(true);
|
|
148
|
+
expect($value3.disposed).toBe(true);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("should dispose values with provided options when FlowMap is disposed", () => {
|
|
152
|
+
const $value1 = state(1);
|
|
153
|
+
const $value2 = state(2);
|
|
154
|
+
const $map = map(
|
|
155
|
+
new Map([
|
|
156
|
+
["key1", $value1],
|
|
157
|
+
["key2", $value2],
|
|
158
|
+
]),
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
const $effect = effect((t) => {
|
|
162
|
+
$value1.get(t);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
expect($effect.disposed).toBe(false);
|
|
166
|
+
|
|
167
|
+
$map.dispose({ self: true });
|
|
168
|
+
|
|
169
|
+
expect($value1.disposed).toBe(true);
|
|
170
|
+
expect($value2.disposed).toBe(true);
|
|
171
|
+
expect($effect.disposed).toBe(false);
|
|
172
|
+
|
|
173
|
+
$effect.dispose();
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("should handle non-disposable values gracefully", async () => {
|
|
177
|
+
const $map = map<string, number>();
|
|
178
|
+
$map.add("key1", 1);
|
|
179
|
+
$map.add("key2", 2);
|
|
180
|
+
$map.add("key3", 3);
|
|
181
|
+
|
|
182
|
+
expect((await $map.pick()).size).toBe(3);
|
|
183
|
+
|
|
184
|
+
expect(() => $map.dispose()).not.toThrow();
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
describe("set", () => {
|
|
189
|
+
it("should replace entire map when set is called", async () => {
|
|
190
|
+
const $map = map<string, number>({ key1: 1, key2: 2 });
|
|
191
|
+
const newMap = new Map<string, number>([
|
|
192
|
+
["key3", 3],
|
|
193
|
+
["key4", 4],
|
|
194
|
+
]);
|
|
195
|
+
|
|
196
|
+
await $map.set(newMap);
|
|
197
|
+
expect(Array.from((await $map.pick()).entries())).toEqual([
|
|
198
|
+
["key3", 3],
|
|
199
|
+
["key4", 4],
|
|
200
|
+
]);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("should dispose values when set replaces map", async () => {
|
|
204
|
+
const $oldValue1 = state(1);
|
|
205
|
+
const $oldValue2 = state(2);
|
|
206
|
+
const $map = map(
|
|
207
|
+
new Map([
|
|
208
|
+
["key1", $oldValue1],
|
|
209
|
+
["key2", $oldValue2],
|
|
210
|
+
]),
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
expect($oldValue1.disposed).toBe(false);
|
|
214
|
+
expect($oldValue2.disposed).toBe(false);
|
|
215
|
+
|
|
216
|
+
await $map.set(
|
|
217
|
+
new Map([
|
|
218
|
+
["key3", state(3)],
|
|
219
|
+
["key4", state(4)],
|
|
220
|
+
]),
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
expect($oldValue1.disposed).toBe(true);
|
|
224
|
+
expect($oldValue2.disposed).toBe(true);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("should dispose values with self option when set replaces map", async () => {
|
|
228
|
+
const $oldValue = state(1);
|
|
229
|
+
const $map = map(new Map([["key1", $oldValue]]));
|
|
230
|
+
|
|
231
|
+
const $effect = effect((t) => {
|
|
232
|
+
$oldValue.get(t);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
expect($effect.disposed).toBe(false);
|
|
236
|
+
|
|
237
|
+
await $map.set(new Map([["key2", state(2)]]));
|
|
238
|
+
|
|
239
|
+
expect($oldValue.disposed).toBe(true);
|
|
240
|
+
expect($effect.disposed).toBe(false);
|
|
241
|
+
|
|
242
|
+
$effect.dispose();
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it("should update $lastAction with set action when set is called", async () => {
|
|
246
|
+
const $map = map<string, number>({ key1: 1, key2: 2 });
|
|
247
|
+
const newMap = new Map<string, number>([
|
|
248
|
+
["key3", 3],
|
|
249
|
+
["key4", 4],
|
|
250
|
+
]);
|
|
251
|
+
|
|
252
|
+
await $map.set(newMap);
|
|
253
|
+
const action = await $map.$lastAction.pick();
|
|
254
|
+
expect(action).toEqual({
|
|
255
|
+
type: "set",
|
|
256
|
+
map: newMap,
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
describe("add", () => {
|
|
262
|
+
it("should add key-value pair to map", async () => {
|
|
263
|
+
const $map = map<string, number>();
|
|
264
|
+
|
|
265
|
+
$map.add("key1", 1);
|
|
266
|
+
expect((await $map.pick()).get("key1")).toBe(1);
|
|
267
|
+
expect((await $map.pick()).size).toBe(1);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it("should add multiple keys successively", async () => {
|
|
271
|
+
const $map = map<string, number>();
|
|
272
|
+
|
|
273
|
+
$map.add("key1", 1);
|
|
274
|
+
$map.add("key2", 2);
|
|
275
|
+
$map.add("key3", 3);
|
|
276
|
+
|
|
277
|
+
expect((await $map.pick()).size).toBe(3);
|
|
278
|
+
expect((await $map.pick()).get("key1")).toBe(1);
|
|
279
|
+
expect((await $map.pick()).get("key2")).toBe(2);
|
|
280
|
+
expect((await $map.pick()).get("key3")).toBe(3);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it("should throw when key already exists", async () => {
|
|
284
|
+
const $map = map<string, number>();
|
|
285
|
+
$map.add("key1", 1);
|
|
286
|
+
await expect($map.add("key1", 2)).rejects.toThrow(
|
|
287
|
+
"[PicoFlow] Key already exists",
|
|
288
|
+
);
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it("should update $lastAction with add action when add is called", async () => {
|
|
292
|
+
const $map = map<string, number>();
|
|
293
|
+
$map.add("key1", 1);
|
|
294
|
+
const action = await $map.$lastAction.pick();
|
|
295
|
+
expect(action).toEqual({
|
|
296
|
+
type: "add",
|
|
297
|
+
key: "key1",
|
|
298
|
+
value: 1,
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it("should trigger reactivity when add is called", async () => {
|
|
303
|
+
const $map = map<string, number>();
|
|
304
|
+
const effectFn = vi.fn();
|
|
305
|
+
effect((t) => effectFn($map.get(t)));
|
|
306
|
+
|
|
307
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
308
|
+
|
|
309
|
+
$map.add("key1", 1);
|
|
310
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
describe("update", () => {
|
|
315
|
+
it("should update existing key-value pair", async () => {
|
|
316
|
+
const $map = map<string, number>();
|
|
317
|
+
$map.add("key1", 1);
|
|
318
|
+
|
|
319
|
+
$map.update("key1", 2);
|
|
320
|
+
expect((await $map.pick()).get("key1")).toBe(2);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it("should update multiple keys successively", async () => {
|
|
324
|
+
const $map = map<string, number>();
|
|
325
|
+
$map.add("key1", 1);
|
|
326
|
+
$map.add("key2", 2);
|
|
327
|
+
|
|
328
|
+
$map.update("key1", 10);
|
|
329
|
+
$map.update("key2", 20);
|
|
330
|
+
|
|
331
|
+
expect((await $map.pick()).get("key1")).toBe(10);
|
|
332
|
+
expect((await $map.pick()).get("key2")).toBe(20);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it("should throw when key does not exist", async () => {
|
|
336
|
+
const $map = map<string, number>();
|
|
337
|
+
await expect($map.update("key1", 1)).rejects.toThrow(
|
|
338
|
+
"[PicoFlow] Key does not exist",
|
|
339
|
+
);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it("should update $lastAction with update action when update is called", async () => {
|
|
343
|
+
const $map = map<string, number>();
|
|
344
|
+
$map.add("key1", 1);
|
|
345
|
+
$map.update("key1", 2);
|
|
346
|
+
const action = await $map.$lastAction.pick();
|
|
347
|
+
expect(action).toEqual({
|
|
348
|
+
type: "update",
|
|
349
|
+
key: "key1",
|
|
350
|
+
value: 2,
|
|
351
|
+
});
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
it("should trigger reactivity when update is called", async () => {
|
|
355
|
+
const $map = map<string, number>();
|
|
356
|
+
$map.add("key1", 1);
|
|
357
|
+
const effectFn = vi.fn();
|
|
358
|
+
effect((t) => effectFn($map.get(t)));
|
|
359
|
+
|
|
360
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
361
|
+
|
|
362
|
+
$map.update("key1", 2);
|
|
363
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
364
|
+
});
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
describe("delete", () => {
|
|
368
|
+
it("should delete key from map", async () => {
|
|
369
|
+
const $map = map<string, number>();
|
|
370
|
+
$map.add("key1", 1);
|
|
371
|
+
$map.add("key2", 2);
|
|
372
|
+
|
|
373
|
+
$map.delete("key1");
|
|
374
|
+
expect((await $map.pick()).get("key1")).toBe(undefined);
|
|
375
|
+
expect((await $map.pick()).get("key2")).toBe(2);
|
|
376
|
+
expect((await $map.pick()).size).toBe(1);
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
it("should delete multiple keys successively", async () => {
|
|
380
|
+
const $map = map<string, number>();
|
|
381
|
+
$map.add("key1", 1);
|
|
382
|
+
$map.add("key2", 2);
|
|
383
|
+
$map.add("key3", 3);
|
|
384
|
+
|
|
385
|
+
$map.delete("key1");
|
|
386
|
+
$map.delete("key2");
|
|
387
|
+
|
|
388
|
+
expect((await $map.pick()).size).toBe(1);
|
|
389
|
+
expect((await $map.pick()).get("key3")).toBe(3);
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
it("should throw when key does not exist", async () => {
|
|
393
|
+
const $map = map<string, number>();
|
|
394
|
+
await expect($map.delete("key1")).rejects.toThrow(
|
|
395
|
+
"[PicoFlow] Key does not exist",
|
|
396
|
+
);
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
it("should update $lastAction with delete action when delete is called", async () => {
|
|
400
|
+
const $map = map<string, number>();
|
|
401
|
+
$map.add("key1", 1);
|
|
402
|
+
$map.delete("key1");
|
|
403
|
+
const action = await $map.$lastAction.pick();
|
|
404
|
+
expect(action).toEqual({
|
|
405
|
+
type: "delete",
|
|
406
|
+
key: "key1",
|
|
407
|
+
value: 1,
|
|
408
|
+
});
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
it("should trigger reactivity when delete is called", async () => {
|
|
412
|
+
const $map = map<string, number>();
|
|
413
|
+
$map.add("key1", 1);
|
|
414
|
+
const effectFn = vi.fn();
|
|
415
|
+
effect((t) => effectFn($map.get(t)));
|
|
416
|
+
|
|
417
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
418
|
+
|
|
419
|
+
$map.delete("key1");
|
|
420
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
it("should dispose value when delete is called", () => {
|
|
424
|
+
const $value = state(1);
|
|
425
|
+
const $map = map(new Map([["key1", $value]]));
|
|
426
|
+
|
|
427
|
+
expect($value.disposed).toBe(false);
|
|
428
|
+
|
|
429
|
+
$map.delete("key1");
|
|
430
|
+
|
|
431
|
+
expect($value.disposed).toBe(true);
|
|
432
|
+
});
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
describe("clear", () => {
|
|
436
|
+
it("should remove all entries from map", async () => {
|
|
437
|
+
const $map = map({ key1: 1, key2: 2, key3: 3 });
|
|
438
|
+
expect((await $map.pick()).size).toBe(3);
|
|
439
|
+
|
|
440
|
+
$map.clear();
|
|
441
|
+
expect((await $map.pick()).size).toBe(0);
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
it("should dispose all values when clear is called", () => {
|
|
445
|
+
const $value1 = state(1);
|
|
446
|
+
const $value2 = state(2);
|
|
447
|
+
const $value3 = state(3);
|
|
448
|
+
const $map = map(
|
|
449
|
+
new Map([
|
|
450
|
+
["key1", $value1],
|
|
451
|
+
["key2", $value2],
|
|
452
|
+
["key3", $value3],
|
|
453
|
+
]),
|
|
454
|
+
);
|
|
455
|
+
|
|
456
|
+
expect($value1.disposed).toBe(false);
|
|
457
|
+
expect($value2.disposed).toBe(false);
|
|
458
|
+
expect($value3.disposed).toBe(false);
|
|
459
|
+
|
|
460
|
+
$map.clear();
|
|
461
|
+
|
|
462
|
+
expect($value1.disposed).toBe(true);
|
|
463
|
+
expect($value2.disposed).toBe(true);
|
|
464
|
+
expect($value3.disposed).toBe(true);
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
it("should dispose values with self option when clear is called", async () => {
|
|
468
|
+
const $value = state(1);
|
|
469
|
+
const $value2 = state(2);
|
|
470
|
+
const $map = map(
|
|
471
|
+
new Map([
|
|
472
|
+
["key1", $value],
|
|
473
|
+
["key2", $value2],
|
|
474
|
+
]),
|
|
475
|
+
);
|
|
476
|
+
|
|
477
|
+
const $effect = effect((t) => {
|
|
478
|
+
$value.get(t);
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
expect($effect.disposed).toBe(false);
|
|
482
|
+
|
|
483
|
+
await $map.clear();
|
|
484
|
+
|
|
485
|
+
expect($value.disposed).toBe(true);
|
|
486
|
+
expect($effect.disposed).toBe(false);
|
|
487
|
+
|
|
488
|
+
$effect.dispose();
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
it("should update $lastAction with clear action when clear is called", async () => {
|
|
492
|
+
const $map = map({ key1: 1, key2: 2 });
|
|
493
|
+
$map.clear();
|
|
494
|
+
const action = await $map.$lastAction.pick();
|
|
495
|
+
expect(action).toEqual({ type: "clear" });
|
|
496
|
+
});
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
describe("$lastAction", () => {
|
|
500
|
+
describe("set action", () => {
|
|
501
|
+
it("should initialize $lastAction with set action on creation", async () => {
|
|
502
|
+
const $map = map({ key1: 1, key2: 2 });
|
|
503
|
+
const action = await $map.$lastAction.pick();
|
|
504
|
+
expect(action).toEqual({
|
|
505
|
+
type: "set",
|
|
506
|
+
map: expect.any(Map),
|
|
507
|
+
});
|
|
508
|
+
if (action.type === "set") {
|
|
509
|
+
expect(Array.from(action.map.entries())).toEqual([
|
|
510
|
+
["key1", 1],
|
|
511
|
+
["key2", 2],
|
|
512
|
+
]);
|
|
513
|
+
}
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
it("should update $lastAction with set action when set is called", async () => {
|
|
517
|
+
const $map = map<string, number>({ key1: 1 });
|
|
518
|
+
const newMap = new Map<string, number>([["key2", 2]]);
|
|
519
|
+
await $map.set(newMap);
|
|
520
|
+
const action = await $map.$lastAction.pick();
|
|
521
|
+
expect(action).toEqual({
|
|
522
|
+
type: "set",
|
|
523
|
+
map: newMap,
|
|
524
|
+
});
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
it("should have correct structure for set action (type + map)", async () => {
|
|
528
|
+
const $map = map({ key1: 1 });
|
|
529
|
+
const action = await $map.$lastAction.pick();
|
|
530
|
+
expect(action).toHaveProperty("type", "set");
|
|
531
|
+
if (action.type === "set") {
|
|
532
|
+
expect(action).toHaveProperty("map");
|
|
533
|
+
expect(action.map).toBeInstanceOf(Map);
|
|
534
|
+
}
|
|
535
|
+
});
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
describe("add action", () => {
|
|
539
|
+
it("should update $lastAction with add action when add is called", async () => {
|
|
540
|
+
const $map = map<string, number>();
|
|
541
|
+
$map.add("key1", 1);
|
|
542
|
+
const action = await $map.$lastAction.pick();
|
|
543
|
+
expect(action).toEqual({
|
|
544
|
+
type: "add",
|
|
545
|
+
key: "key1",
|
|
546
|
+
value: 1,
|
|
547
|
+
});
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
it("should have correct structure for add action (type + key + value)", async () => {
|
|
551
|
+
const $map = map<string, number>();
|
|
552
|
+
$map.add("key1", 100);
|
|
553
|
+
const action = await $map.$lastAction.pick();
|
|
554
|
+
expect(action).toHaveProperty("type", "add");
|
|
555
|
+
if (action.type === "add") {
|
|
556
|
+
expect(action).toHaveProperty("key");
|
|
557
|
+
expect(action).toHaveProperty("value");
|
|
558
|
+
expect(action.key).toBe("key1");
|
|
559
|
+
expect(action.value).toBe(100);
|
|
560
|
+
}
|
|
561
|
+
});
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
describe("update action", () => {
|
|
565
|
+
it("should update $lastAction with update action when update is called", async () => {
|
|
566
|
+
const $map = map<string, number>();
|
|
567
|
+
$map.add("key1", 1);
|
|
568
|
+
$map.update("key1", 2);
|
|
569
|
+
const action = await $map.$lastAction.pick();
|
|
570
|
+
expect(action).toEqual({
|
|
571
|
+
type: "update",
|
|
572
|
+
key: "key1",
|
|
573
|
+
value: 2,
|
|
574
|
+
});
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
it("should have correct structure for update action (type + key + value)", async () => {
|
|
578
|
+
const $map = map<string, number>();
|
|
579
|
+
$map.add("key1", 1);
|
|
580
|
+
$map.update("key1", 200);
|
|
581
|
+
const action = await $map.$lastAction.pick();
|
|
582
|
+
expect(action).toHaveProperty("type", "update");
|
|
583
|
+
if (action.type === "update") {
|
|
584
|
+
expect(action).toHaveProperty("key");
|
|
585
|
+
expect(action).toHaveProperty("value");
|
|
586
|
+
expect(action.key).toBe("key1");
|
|
587
|
+
expect(action.value).toBe(200);
|
|
588
|
+
}
|
|
589
|
+
});
|
|
590
|
+
});
|
|
591
|
+
|
|
592
|
+
describe("delete action", () => {
|
|
593
|
+
it("should update $lastAction with delete action when delete is called", async () => {
|
|
594
|
+
const $map = map<string, number>();
|
|
595
|
+
$map.add("key1", 1);
|
|
596
|
+
$map.delete("key1");
|
|
597
|
+
const action = await $map.$lastAction.pick();
|
|
598
|
+
expect(action).toEqual({
|
|
599
|
+
type: "delete",
|
|
600
|
+
key: "key1",
|
|
601
|
+
value: 1,
|
|
602
|
+
});
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
it("should have correct structure for delete action (type + key + value)", async () => {
|
|
606
|
+
const $map = map<string, number>();
|
|
607
|
+
$map.add("key1", 300);
|
|
608
|
+
$map.delete("key1");
|
|
609
|
+
const action = await $map.$lastAction.pick();
|
|
610
|
+
expect(action).toHaveProperty("type", "delete");
|
|
611
|
+
if (action.type === "delete") {
|
|
612
|
+
expect(action).toHaveProperty("key");
|
|
613
|
+
expect(action).toHaveProperty("value");
|
|
614
|
+
expect(action.key).toBe("key1");
|
|
615
|
+
expect(action.value).toBe(300);
|
|
616
|
+
}
|
|
617
|
+
});
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
describe("clear action", () => {
|
|
621
|
+
it("should update $lastAction with clear action when clear is called", async () => {
|
|
622
|
+
const $map = map({ key1: 1, key2: 2 });
|
|
623
|
+
$map.clear();
|
|
624
|
+
const action = await $map.$lastAction.pick();
|
|
625
|
+
expect(action).toEqual({
|
|
626
|
+
type: "clear",
|
|
627
|
+
});
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
it("should have correct structure for clear action (type only)", async () => {
|
|
631
|
+
const $map = map({ key1: 1 });
|
|
632
|
+
$map.clear();
|
|
633
|
+
const action = await $map.$lastAction.pick();
|
|
634
|
+
expect(action).toHaveProperty("type", "clear");
|
|
635
|
+
if (action.type === "clear") {
|
|
636
|
+
expect(action).not.toHaveProperty("key");
|
|
637
|
+
expect(action).not.toHaveProperty("value");
|
|
638
|
+
expect(action).not.toHaveProperty("map");
|
|
639
|
+
}
|
|
640
|
+
});
|
|
641
|
+
});
|
|
642
|
+
});
|
|
643
|
+
|
|
644
|
+
describe("edge cases", () => {
|
|
645
|
+
it("should handle multiple rapid operations", async () => {
|
|
646
|
+
const $map = map<string, number>();
|
|
647
|
+
$map.add("key1", 1);
|
|
648
|
+
$map.add("key2", 2);
|
|
649
|
+
$map.add("key3", 3);
|
|
650
|
+
$map.update("key1", 10);
|
|
651
|
+
$map.delete("key2");
|
|
652
|
+
$map.add("key4", 4);
|
|
653
|
+
$map.clear();
|
|
654
|
+
$map.add("key5", 5);
|
|
655
|
+
|
|
656
|
+
expect((await $map.pick()).size).toBe(1);
|
|
657
|
+
expect((await $map.pick()).get("key5")).toBe(5);
|
|
658
|
+
});
|
|
659
|
+
|
|
660
|
+
it("should handle empty map operations", async () => {
|
|
661
|
+
const $map = map<string, number>();
|
|
662
|
+
expect((await $map.pick()).size).toBe(0);
|
|
663
|
+
|
|
664
|
+
$map.add("key1", 1);
|
|
665
|
+
expect((await $map.pick()).size).toBe(1);
|
|
666
|
+
|
|
667
|
+
$map.delete("key1");
|
|
668
|
+
expect((await $map.pick()).size).toBe(0);
|
|
669
|
+
});
|
|
670
|
+
|
|
671
|
+
it("should handle map with single entry", async () => {
|
|
672
|
+
const $map = map({ key1: 1 });
|
|
673
|
+
expect((await $map.pick()).size).toBe(1);
|
|
674
|
+
|
|
675
|
+
$map.update("key1", 2);
|
|
676
|
+
expect((await $map.pick()).get("key1")).toBe(2);
|
|
677
|
+
|
|
678
|
+
$map.delete("key1");
|
|
679
|
+
expect((await $map.pick()).size).toBe(0);
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
it("should handle map with many entries", async () => {
|
|
683
|
+
const $map = map<string, number>();
|
|
684
|
+
for (let i = 0; i < 100; i++) {
|
|
685
|
+
$map.add(`key${i}`, i);
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
expect((await $map.pick()).size).toBe(100);
|
|
689
|
+
|
|
690
|
+
for (let i = 0; i < 50; i++) {
|
|
691
|
+
$map.delete(`key${i}`);
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
expect((await $map.pick()).size).toBe(50);
|
|
695
|
+
});
|
|
696
|
+
});
|
|
697
|
+
});
|
|
698
|
+
|
|
699
|
+
describe("integration", () => {
|
|
700
|
+
beforeEach(() => {
|
|
701
|
+
FlowGraph.clear();
|
|
702
|
+
});
|
|
703
|
+
|
|
704
|
+
describe("with effects", () => {
|
|
705
|
+
describe("reactivity", () => {
|
|
706
|
+
it("should call effect when initialized", async () => {
|
|
707
|
+
const $map = map({ key1: 1, key2: 2 });
|
|
708
|
+
const effectFn = vi.fn();
|
|
709
|
+
effect((t) => effectFn($map.get(t)));
|
|
710
|
+
|
|
711
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
712
|
+
expect(effectFn).toHaveBeenLastCalledWith(
|
|
713
|
+
expect.objectContaining({
|
|
714
|
+
size: 2,
|
|
715
|
+
}),
|
|
716
|
+
);
|
|
717
|
+
});
|
|
718
|
+
|
|
719
|
+
it("should call effect when updated with set", async () => {
|
|
720
|
+
const $map = map<string, number>({ key1: 1 });
|
|
721
|
+
const effectFn = vi.fn();
|
|
722
|
+
effect((t) => effectFn($map.get(t)));
|
|
723
|
+
|
|
724
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
725
|
+
|
|
726
|
+
await $map.set(new Map([["key2", 2]]));
|
|
727
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
728
|
+
expect(effectFn).toHaveBeenLastCalledWith(
|
|
729
|
+
expect.objectContaining({
|
|
730
|
+
size: 1,
|
|
731
|
+
}),
|
|
732
|
+
);
|
|
733
|
+
});
|
|
734
|
+
|
|
735
|
+
it("should call effect when updated with add", async () => {
|
|
736
|
+
const $map = map<string, number>();
|
|
737
|
+
const effectFn = vi.fn();
|
|
738
|
+
effect((t) => effectFn($map.get(t)));
|
|
739
|
+
|
|
740
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
741
|
+
expect(effectFn).toHaveBeenLastCalledWith(new Map());
|
|
742
|
+
|
|
743
|
+
$map.add("key1", 1);
|
|
744
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
745
|
+
expect(effectFn).toHaveBeenLastCalledWith(new Map([["key1", 1]]));
|
|
746
|
+
|
|
747
|
+
$map.add("key2", 2);
|
|
748
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
|
|
749
|
+
expect(effectFn).toHaveBeenLastCalledWith(
|
|
750
|
+
new Map([
|
|
751
|
+
["key1", 1],
|
|
752
|
+
["key2", 2],
|
|
753
|
+
]),
|
|
754
|
+
);
|
|
755
|
+
});
|
|
756
|
+
|
|
757
|
+
it("should call effect when updated with update", async () => {
|
|
758
|
+
const $map = map<string, number>();
|
|
759
|
+
$map.add("key1", 1);
|
|
760
|
+
const effectFn = vi.fn();
|
|
761
|
+
effect((t) => effectFn($map.get(t)));
|
|
762
|
+
|
|
763
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
764
|
+
|
|
765
|
+
$map.update("key1", 2);
|
|
766
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
767
|
+
expect(effectFn).toHaveBeenLastCalledWith(new Map([["key1", 2]]));
|
|
768
|
+
});
|
|
769
|
+
|
|
770
|
+
it("should call effect when updated with delete", async () => {
|
|
771
|
+
const $map = map<string, number>();
|
|
772
|
+
$map.add("key1", 1);
|
|
773
|
+
$map.add("key2", 2);
|
|
774
|
+
const effectFn = vi.fn();
|
|
775
|
+
effect((t) => effectFn($map.get(t)));
|
|
776
|
+
|
|
777
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
778
|
+
|
|
779
|
+
$map.delete("key1");
|
|
780
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
781
|
+
expect(effectFn).toHaveBeenLastCalledWith(new Map([["key2", 2]]));
|
|
782
|
+
});
|
|
783
|
+
|
|
784
|
+
it("should call effect when updated with clear", async () => {
|
|
785
|
+
const $map = map({ key1: 1, key2: 2 });
|
|
786
|
+
const effectFn = vi.fn();
|
|
787
|
+
effect((t) => effectFn($map.get(t)));
|
|
788
|
+
|
|
789
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
790
|
+
|
|
791
|
+
$map.clear();
|
|
792
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
793
|
+
expect(effectFn).toHaveBeenLastCalledWith(new Map());
|
|
794
|
+
});
|
|
795
|
+
});
|
|
796
|
+
|
|
797
|
+
describe("$lastAction", () => {
|
|
798
|
+
it("should call effect when $lastAction is updated with set action", async () => {
|
|
799
|
+
const $map = map<string, number>({ key1: 1 });
|
|
800
|
+
const effectFn = vi.fn();
|
|
801
|
+
effect((t) => effectFn($map.$lastAction.get(t)));
|
|
802
|
+
|
|
803
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
804
|
+
|
|
805
|
+
await $map.set(new Map([["key2", 2]]));
|
|
806
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
807
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
808
|
+
type: "set",
|
|
809
|
+
map: expect.any(Map),
|
|
810
|
+
});
|
|
811
|
+
});
|
|
812
|
+
|
|
813
|
+
it("should call effect when $lastAction is updated with add action", async () => {
|
|
814
|
+
const $map = map<string, number>();
|
|
815
|
+
const effectFn = vi.fn();
|
|
816
|
+
effect((t) => effectFn($map.$lastAction.get(t)));
|
|
817
|
+
|
|
818
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
819
|
+
|
|
820
|
+
$map.add("key1", 1);
|
|
821
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
822
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
823
|
+
type: "add",
|
|
824
|
+
key: "key1",
|
|
825
|
+
value: 1,
|
|
826
|
+
});
|
|
827
|
+
});
|
|
828
|
+
|
|
829
|
+
it("should call effect when $lastAction is updated with update action", async () => {
|
|
830
|
+
const $map = map<string, number>();
|
|
831
|
+
$map.add("key1", 1);
|
|
832
|
+
const effectFn = vi.fn();
|
|
833
|
+
effect((t) => effectFn($map.$lastAction.get(t)));
|
|
834
|
+
|
|
835
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
836
|
+
|
|
837
|
+
$map.update("key1", 2);
|
|
838
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
839
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
840
|
+
type: "update",
|
|
841
|
+
key: "key1",
|
|
842
|
+
value: 2,
|
|
843
|
+
});
|
|
844
|
+
});
|
|
845
|
+
|
|
846
|
+
it("should call effect when $lastAction is updated with delete action", async () => {
|
|
847
|
+
const $map = map<string, number>();
|
|
848
|
+
$map.add("key1", 1);
|
|
849
|
+
const effectFn = vi.fn();
|
|
850
|
+
effect((t) => effectFn($map.$lastAction.get(t)));
|
|
851
|
+
|
|
852
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
853
|
+
|
|
854
|
+
$map.delete("key1");
|
|
855
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
856
|
+
expect(effectFn).toHaveBeenLastCalledWith({
|
|
857
|
+
type: "delete",
|
|
858
|
+
key: "key1",
|
|
859
|
+
value: 1,
|
|
860
|
+
});
|
|
861
|
+
});
|
|
862
|
+
|
|
863
|
+
it("should call effect when $lastAction is updated with clear action", async () => {
|
|
864
|
+
const $map = map({ key1: 1, key2: 2 });
|
|
865
|
+
const effectFn = vi.fn();
|
|
866
|
+
effect((t) => effectFn($map.$lastAction.get(t)));
|
|
867
|
+
|
|
868
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
|
|
869
|
+
|
|
870
|
+
$map.clear();
|
|
871
|
+
await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
|
|
872
|
+
expect(effectFn).toHaveBeenLastCalledWith({ type: "clear" });
|
|
873
|
+
});
|
|
874
|
+
});
|
|
875
|
+
});
|
|
876
|
+
});
|
|
877
|
+
});
|