@ng-org/alien-deepsignals 0.1.2-alpha.2 → 0.1.2-alpha.4
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/dist/deepSignal.d.ts.map +1 -1
- package/dist/deepSignal.js +244 -100
- package/dist/hooks/react/useDeepSignal.d.ts +4 -4
- package/dist/hooks/react/useDeepSignal.d.ts.map +1 -1
- package/dist/hooks/react/useDeepSignal.js +22 -15
- package/dist/hooks/svelte/useDeepSignal.svelte.d.ts +3 -7
- package/dist/hooks/svelte/useDeepSignal.svelte.d.ts.map +1 -1
- package/dist/hooks/svelte/useDeepSignal.svelte.js +34 -18
- package/dist/hooks/vue/useDeepSignal.d.ts +4 -3
- package/dist/hooks/vue/useDeepSignal.d.ts.map +1 -1
- package/dist/hooks/vue/useDeepSignal.js +52 -24
- package/dist/test/frontend/astro-app/src/components/PerfSuiteClient.d.ts +4 -0
- package/dist/test/frontend/astro-app/src/components/PerfSuiteClient.d.ts.map +1 -0
- package/dist/test/frontend/astro-app/src/components/PerfSuiteClient.js +225 -0
- package/dist/test/frontend/astro-app/src/components/ReactPanel.d.ts +4 -0
- package/dist/test/frontend/astro-app/src/components/ReactPanel.d.ts.map +1 -0
- package/dist/test/frontend/astro-app/src/components/ReactPanel.js +227 -0
- package/dist/test/frontend/astro-app/src/components/perf/react/ReactPerfDeep.d.ts +4 -0
- package/dist/test/frontend/astro-app/src/components/perf/react/ReactPerfDeep.d.ts.map +1 -0
- package/dist/test/frontend/astro-app/src/components/perf/react/ReactPerfDeep.js +150 -0
- package/dist/test/frontend/astro-app/src/components/perf/react/ReactPerfNative.d.ts +4 -0
- package/dist/test/frontend/astro-app/src/components/perf/react/ReactPerfNative.d.ts.map +1 -0
- package/dist/test/frontend/astro-app/src/components/perf/react/ReactPerfNative.js +184 -0
- package/dist/test/frontend/playwright/crossFrameworkHooks.spec.d.ts +2 -0
- package/dist/test/frontend/playwright/crossFrameworkHooks.spec.d.ts.map +1 -0
- package/dist/test/frontend/playwright/crossFrameworkHooks.spec.js +171 -0
- package/dist/test/frontend/playwright/perfSuite.spec.d.ts +2 -0
- package/dist/test/frontend/playwright/perfSuite.spec.d.ts.map +1 -0
- package/dist/test/frontend/playwright/perfSuite.spec.js +128 -0
- package/dist/test/frontend/utils/mockData.d.ts +53 -0
- package/dist/test/frontend/utils/mockData.d.ts.map +1 -0
- package/dist/test/frontend/utils/mockData.js +78 -0
- package/dist/test/frontend/utils/paths.d.ts +4 -0
- package/dist/test/frontend/utils/paths.d.ts.map +1 -0
- package/dist/test/frontend/utils/paths.js +28 -0
- package/dist/test/frontend/utils/perfScenarios.d.ts +15 -0
- package/dist/test/frontend/utils/perfScenarios.d.ts.map +1 -0
- package/dist/test/frontend/utils/perfScenarios.js +287 -0
- package/dist/test/frontend/utils/renderMetrics.d.ts +13 -0
- package/dist/test/frontend/utils/renderMetrics.d.ts.map +1 -0
- package/dist/test/frontend/utils/renderMetrics.js +45 -0
- package/dist/test/frontend/utils/state.d.ts +57 -0
- package/dist/test/frontend/utils/state.d.ts.map +1 -0
- package/dist/test/frontend/utils/state.js +79 -0
- package/dist/test/lib/core.test.d.ts +2 -0
- package/dist/test/lib/core.test.d.ts.map +1 -0
- package/dist/test/lib/core.test.js +53 -0
- package/dist/test/lib/deepSignalOptions.test.d.ts +2 -0
- package/dist/test/lib/deepSignalOptions.test.d.ts.map +1 -0
- package/dist/test/lib/deepSignalOptions.test.js +230 -0
- package/dist/test/lib/index.test.d.ts +2 -0
- package/dist/test/lib/index.test.d.ts.map +1 -0
- package/dist/test/lib/index.test.js +807 -0
- package/dist/test/lib/misc.test.d.ts +2 -0
- package/dist/test/lib/misc.test.d.ts.map +1 -0
- package/dist/test/lib/misc.test.js +140 -0
- package/dist/test/lib/watch.test.d.ts +2 -0
- package/dist/test/lib/watch.test.d.ts.map +1 -0
- package/dist/test/lib/watch.test.js +1280 -0
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +36 -19
- package/src/index.ts +5 -0
|
@@ -0,0 +1,807 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright (c) 2025 Laurin Weger, Par le Peuple, NextGraph.org developers
|
|
3
|
+
// All rights reserved.
|
|
4
|
+
// Licensed under the Apache License, Version 2.0
|
|
5
|
+
// <LICENSE-APACHE2 or http://www.apache.org/licenses/LICENSE-2.0>
|
|
6
|
+
// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
|
|
7
|
+
// at your option. All files in the project carrying such
|
|
8
|
+
// notice may not be copied, modified, or distributed except
|
|
9
|
+
// according to those terms.
|
|
10
|
+
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const index_1 = require("../../index");
|
|
13
|
+
const vitest_1 = require("vitest");
|
|
14
|
+
const __1 = require("../..");
|
|
15
|
+
(0, vitest_1.describe)("deepsignal/core", () => {
|
|
16
|
+
let nested = { b: 2 };
|
|
17
|
+
let array = [3, nested];
|
|
18
|
+
let state = { a: 1, nested, array };
|
|
19
|
+
let store = (0, index_1.deepSignal)(state);
|
|
20
|
+
const window = globalThis;
|
|
21
|
+
(0, vitest_1.beforeEach)(() => {
|
|
22
|
+
nested = { b: 2 };
|
|
23
|
+
array = [3, nested];
|
|
24
|
+
state = { a: 1, nested, array };
|
|
25
|
+
store = (0, index_1.deepSignal)(state);
|
|
26
|
+
});
|
|
27
|
+
(0, vitest_1.describe)("get - plain", () => {
|
|
28
|
+
(0, vitest_1.it)("should return plain objects/arrays", () => {
|
|
29
|
+
(0, vitest_1.expect)(store.nested).to.deep.equal({ b: 2 });
|
|
30
|
+
(0, vitest_1.expect)(store.array).to.deep.equal([3, { b: 2 }]);
|
|
31
|
+
(0, vitest_1.expect)(store.array[1]).to.deep.equal({ b: 2 });
|
|
32
|
+
});
|
|
33
|
+
(0, vitest_1.it)("should return plain primitives", () => {
|
|
34
|
+
(0, vitest_1.expect)(store.a).to.equal(1);
|
|
35
|
+
(0, vitest_1.expect)(store.nested.b).to.equal(2);
|
|
36
|
+
(0, vitest_1.expect)(store.array[0]).to.equal(3);
|
|
37
|
+
(0, vitest_1.expect)(typeof store.array[1] === "object" && store.array[1].b).to.equal(2);
|
|
38
|
+
(0, vitest_1.expect)(store.array.length).to.equal(2);
|
|
39
|
+
});
|
|
40
|
+
(0, vitest_1.it)("should support reading from getters", () => {
|
|
41
|
+
const store = (0, index_1.deepSignal)({
|
|
42
|
+
counter: 1,
|
|
43
|
+
get double() {
|
|
44
|
+
return store.counter * 2;
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
(0, vitest_1.expect)(store.double).to.equal(2);
|
|
48
|
+
store.counter = 2;
|
|
49
|
+
(0, vitest_1.expect)(store.double).to.equal(4);
|
|
50
|
+
});
|
|
51
|
+
(0, vitest_1.it)("should support getters returning other parts of the state", () => {
|
|
52
|
+
const store = (0, index_1.deepSignal)({
|
|
53
|
+
switch: "a",
|
|
54
|
+
a: { data: "a" },
|
|
55
|
+
b: { data: "b" },
|
|
56
|
+
get aOrB() {
|
|
57
|
+
return store.switch === "a" ? store.a : store.b;
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
(0, vitest_1.expect)(store.aOrB.data).to.equal("a");
|
|
61
|
+
store.switch = "b";
|
|
62
|
+
(0, vitest_1.expect)(store.aOrB.data).to.equal("b");
|
|
63
|
+
});
|
|
64
|
+
(0, vitest_1.it)("should support getters using ownKeys traps", () => {
|
|
65
|
+
const state = (0, index_1.deepSignal)({
|
|
66
|
+
x: {
|
|
67
|
+
a: 1,
|
|
68
|
+
b: 2,
|
|
69
|
+
},
|
|
70
|
+
get y() {
|
|
71
|
+
return Object.values(state.x);
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
(0, vitest_1.expect)(state.y).to.deep.equal([1, 2]);
|
|
75
|
+
});
|
|
76
|
+
(0, vitest_1.it)("should work with normal functions", () => {
|
|
77
|
+
const store = (0, index_1.deepSignal)({
|
|
78
|
+
value: 1,
|
|
79
|
+
isBigger: (newValue) => store.value < newValue,
|
|
80
|
+
sum(newValue) {
|
|
81
|
+
return store.value + newValue;
|
|
82
|
+
},
|
|
83
|
+
replace: (newValue) => {
|
|
84
|
+
store.value = newValue;
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
(0, vitest_1.expect)(store.isBigger(2)).to.equal(true);
|
|
88
|
+
(0, vitest_1.expect)(store.sum(2)).to.equal(3);
|
|
89
|
+
(0, vitest_1.expect)(store.value).to.equal(1);
|
|
90
|
+
store.replace(2);
|
|
91
|
+
(0, vitest_1.expect)(store.value).to.equal(2);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
(0, vitest_1.describe)("set", () => {
|
|
95
|
+
(0, vitest_1.it)("should update like plain objects/arrays", () => {
|
|
96
|
+
(0, vitest_1.expect)(store.a).to.equal(1);
|
|
97
|
+
(0, vitest_1.expect)(store.nested.b).to.equal(2);
|
|
98
|
+
store.a = 2;
|
|
99
|
+
store.nested.b = 3;
|
|
100
|
+
(0, vitest_1.expect)(store.a).to.equal(2);
|
|
101
|
+
(0, vitest_1.expect)(store.nested.b).to.equal(3);
|
|
102
|
+
});
|
|
103
|
+
// TODO: Remove the .value access semantics.
|
|
104
|
+
(0, vitest_1.it)("should support setting values with setters", () => {
|
|
105
|
+
const store = (0, index_1.deepSignal)({
|
|
106
|
+
counter: 1,
|
|
107
|
+
get double() {
|
|
108
|
+
return store.counter * 2;
|
|
109
|
+
},
|
|
110
|
+
set double(val) {
|
|
111
|
+
store.counter = val / 2;
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
(0, vitest_1.expect)(store.counter).to.equal(1);
|
|
115
|
+
store.double = 4;
|
|
116
|
+
(0, vitest_1.expect)(store.counter).to.equal(2);
|
|
117
|
+
});
|
|
118
|
+
(0, vitest_1.it)("should update array length", () => {
|
|
119
|
+
let state = { a: 1, nested, array };
|
|
120
|
+
let store = (0, index_1.deepSignal)(state);
|
|
121
|
+
(0, vitest_1.expect)(store.array.length).to.equal(2);
|
|
122
|
+
store.array.push(4);
|
|
123
|
+
(0, vitest_1.expect)(store.array.length).to.equal(3);
|
|
124
|
+
store.array.splice(1, 2);
|
|
125
|
+
(0, vitest_1.expect)(store.array.length).to.equal(1);
|
|
126
|
+
});
|
|
127
|
+
(0, vitest_1.it)("should update when mutations happen", () => {
|
|
128
|
+
(0, vitest_1.expect)(store.a).to.equal(1);
|
|
129
|
+
store.a = 11;
|
|
130
|
+
(0, vitest_1.expect)(store.a).to.equal(11);
|
|
131
|
+
});
|
|
132
|
+
(0, vitest_1.it)("should support setting getters on the fly", () => {
|
|
133
|
+
const store = (0, index_1.deepSignal)({
|
|
134
|
+
counter: 1,
|
|
135
|
+
});
|
|
136
|
+
Object.defineProperty(store, "double", {
|
|
137
|
+
get: function () {
|
|
138
|
+
return store.counter * 2;
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
(0, vitest_1.expect)(store.double).to.equal(2);
|
|
142
|
+
store.counter = 2;
|
|
143
|
+
(0, vitest_1.expect)(store.double).to.equal(4);
|
|
144
|
+
});
|
|
145
|
+
(0, vitest_1.it)("should copy object like plain JavaScript", () => {
|
|
146
|
+
const store = (0, index_1.deepSignal)({
|
|
147
|
+
b: { id: 1, nested: { id: 1 } },
|
|
148
|
+
});
|
|
149
|
+
store.a = store.b;
|
|
150
|
+
(0, vitest_1.expect)(store.a.id).to.equal(1);
|
|
151
|
+
(0, vitest_1.expect)(store.b.id).to.equal(1);
|
|
152
|
+
(0, vitest_1.expect)(store.a.nested.id).to.equal(1);
|
|
153
|
+
(0, vitest_1.expect)(store.b.nested.id).to.equal(1);
|
|
154
|
+
store.a.id = 2;
|
|
155
|
+
store.a.nested.id = 2;
|
|
156
|
+
(0, vitest_1.expect)(store.a.id).to.equal(2);
|
|
157
|
+
(0, vitest_1.expect)(store.b.id).to.equal(2);
|
|
158
|
+
(0, vitest_1.expect)(store.a.nested.id).to.equal(2);
|
|
159
|
+
(0, vitest_1.expect)(store.b.nested.id).to.equal(2);
|
|
160
|
+
store.b.id = 3;
|
|
161
|
+
store.b.nested.id = 3;
|
|
162
|
+
(0, vitest_1.expect)(store.b.id).to.equal(3);
|
|
163
|
+
(0, vitest_1.expect)(store.a.id).to.equal(3);
|
|
164
|
+
(0, vitest_1.expect)(store.a.nested.id).to.equal(3);
|
|
165
|
+
(0, vitest_1.expect)(store.b.nested.id).to.equal(3);
|
|
166
|
+
store.a.id = 4;
|
|
167
|
+
store.a.nested.id = 4;
|
|
168
|
+
(0, vitest_1.expect)(store.a.id).to.equal(4);
|
|
169
|
+
(0, vitest_1.expect)(store.b.id).to.equal(4);
|
|
170
|
+
(0, vitest_1.expect)(store.a.nested.id).to.equal(4);
|
|
171
|
+
(0, vitest_1.expect)(store.b.nested.id).to.equal(4);
|
|
172
|
+
});
|
|
173
|
+
(0, vitest_1.it)("should be able to reset values with Object.assign", () => {
|
|
174
|
+
const initialNested = { ...nested };
|
|
175
|
+
const initialState = { ...state, nested: initialNested };
|
|
176
|
+
store.a = 2;
|
|
177
|
+
store.nested.b = 3;
|
|
178
|
+
Object.assign(store, initialState);
|
|
179
|
+
(0, vitest_1.expect)(store.a).to.equal(1);
|
|
180
|
+
(0, vitest_1.expect)(store.nested.b).to.equal(2);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
(0, vitest_1.describe)("delete", () => {
|
|
184
|
+
(0, vitest_1.it)("should delete properties before they are accessed", () => {
|
|
185
|
+
delete store.a;
|
|
186
|
+
(0, vitest_1.expect)(store.a).to.equal(undefined);
|
|
187
|
+
});
|
|
188
|
+
(0, vitest_1.it)("should delete properties after they are accessed", () => {
|
|
189
|
+
(0, vitest_1.expect)(store.a).to.equal(1);
|
|
190
|
+
delete store.a;
|
|
191
|
+
(0, vitest_1.expect)(store.a).to.equal(undefined);
|
|
192
|
+
});
|
|
193
|
+
(0, vitest_1.it)("should delete nested properties before they are accessed", () => {
|
|
194
|
+
delete store.nested.b;
|
|
195
|
+
(0, vitest_1.expect)(store.nested.b).to.equal(undefined);
|
|
196
|
+
});
|
|
197
|
+
(0, vitest_1.it)("should delete nested properties after they are accessed", () => {
|
|
198
|
+
(0, vitest_1.expect)(store.nested.b).to.equal(2);
|
|
199
|
+
delete store.nested.b;
|
|
200
|
+
(0, vitest_1.expect)(store.nested.b).to.equal(undefined);
|
|
201
|
+
});
|
|
202
|
+
(0, vitest_1.it)("should delete properties in arrays before they are accessed", () => {
|
|
203
|
+
delete store.array[0];
|
|
204
|
+
(0, vitest_1.expect)(store.array[0]).to.equal(undefined);
|
|
205
|
+
});
|
|
206
|
+
(0, vitest_1.it)("should delete properties in arrays after they are accessed", () => {
|
|
207
|
+
(0, vitest_1.expect)(store.array[0]).to.equal(3);
|
|
208
|
+
delete store.array[0];
|
|
209
|
+
(0, vitest_1.expect)(store.array[0]).to.equal(undefined);
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
(0, vitest_1.describe)("ownKeys", () => {
|
|
213
|
+
(0, vitest_1.it)("should return own properties in objects", () => {
|
|
214
|
+
const state = { a: 1, b: 2 };
|
|
215
|
+
const store = (0, index_1.deepSignal)(state);
|
|
216
|
+
let sum = 0;
|
|
217
|
+
for (const property in store) {
|
|
218
|
+
sum += store[property];
|
|
219
|
+
}
|
|
220
|
+
(0, vitest_1.expect)(sum).to.equal(3);
|
|
221
|
+
});
|
|
222
|
+
(0, vitest_1.it)("should return own properties in arrays", () => {
|
|
223
|
+
const state = [1, 2];
|
|
224
|
+
const store = (0, index_1.deepSignal)(state);
|
|
225
|
+
let sum = 0;
|
|
226
|
+
for (const property of store) {
|
|
227
|
+
sum += property;
|
|
228
|
+
}
|
|
229
|
+
(0, vitest_1.expect)(sum).to.equal(3);
|
|
230
|
+
});
|
|
231
|
+
(0, vitest_1.it)("should spread objects correctly", () => {
|
|
232
|
+
const store2 = { ...store };
|
|
233
|
+
(0, vitest_1.expect)(store2.a).to.equal(1);
|
|
234
|
+
(0, vitest_1.expect)(store2.nested.b).to.equal(2);
|
|
235
|
+
(0, vitest_1.expect)(store2.array[0]).to.equal(3);
|
|
236
|
+
(0, vitest_1.expect)(typeof store2.array[1] === "object" && store2.array[1].b).to.equal(2);
|
|
237
|
+
});
|
|
238
|
+
(0, vitest_1.it)("should spread arrays correctly", () => {
|
|
239
|
+
const array2 = [...store.array];
|
|
240
|
+
(0, vitest_1.expect)(array2[0]).to.equal(3);
|
|
241
|
+
(0, vitest_1.expect)(typeof array2[1] === "object" && array2[1].b).to.equal(2);
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
(0, vitest_1.describe)("computations", () => {
|
|
245
|
+
(0, vitest_1.it)("should subscribe to values mutated with setters", () => {
|
|
246
|
+
const store = (0, index_1.deepSignal)({
|
|
247
|
+
counter: 1,
|
|
248
|
+
get double() {
|
|
249
|
+
return store.counter * 2;
|
|
250
|
+
},
|
|
251
|
+
set double(val) {
|
|
252
|
+
store.counter = val / 2;
|
|
253
|
+
},
|
|
254
|
+
});
|
|
255
|
+
let counter = 0;
|
|
256
|
+
let double = 0;
|
|
257
|
+
(0, __1.effect)(() => {
|
|
258
|
+
counter = store.counter;
|
|
259
|
+
double = store.double;
|
|
260
|
+
});
|
|
261
|
+
(0, vitest_1.expect)(counter).to.equal(1);
|
|
262
|
+
(0, vitest_1.expect)(double).to.equal(2);
|
|
263
|
+
store.double = 4;
|
|
264
|
+
(0, vitest_1.expect)(counter).to.equal(2);
|
|
265
|
+
(0, vitest_1.expect)(double).to.equal(4);
|
|
266
|
+
});
|
|
267
|
+
(0, vitest_1.it)("should subscribe to changes when an item is removed from the array", () => {
|
|
268
|
+
const store = (0, index_1.deepSignal)([0, 0, 0]);
|
|
269
|
+
let sum = 0;
|
|
270
|
+
(0, __1.effect)(() => {
|
|
271
|
+
sum = 0;
|
|
272
|
+
sum = store.reduce((sum) => sum + 1, 0);
|
|
273
|
+
});
|
|
274
|
+
(0, vitest_1.expect)(sum).to.equal(3);
|
|
275
|
+
store.splice(2, 1);
|
|
276
|
+
(0, vitest_1.expect)(sum).to.equal(2);
|
|
277
|
+
});
|
|
278
|
+
(0, vitest_1.it)("should subscribe to changes to for..in loops", () => {
|
|
279
|
+
const state = { a: 0, b: 0 };
|
|
280
|
+
const store = (0, index_1.deepSignal)(state);
|
|
281
|
+
let sum = 0;
|
|
282
|
+
(0, __1.effect)(() => {
|
|
283
|
+
sum = 0;
|
|
284
|
+
for (const _ in store) {
|
|
285
|
+
sum += 1;
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
(0, vitest_1.expect)(sum).to.equal(2);
|
|
289
|
+
store.c = 0;
|
|
290
|
+
(0, vitest_1.expect)(sum).to.equal(3);
|
|
291
|
+
delete store.c;
|
|
292
|
+
(0, vitest_1.expect)(sum).to.equal(2);
|
|
293
|
+
});
|
|
294
|
+
(0, vitest_1.it)("should not retrigger effects when unrelated object branches change", () => {
|
|
295
|
+
const store = (0, index_1.deepSignal)({
|
|
296
|
+
alpha: { value: 1 },
|
|
297
|
+
beta: { value: 2 },
|
|
298
|
+
});
|
|
299
|
+
let runs = 0;
|
|
300
|
+
(0, __1.effect)(() => {
|
|
301
|
+
runs += 1;
|
|
302
|
+
store.alpha.value;
|
|
303
|
+
});
|
|
304
|
+
(0, vitest_1.expect)(runs).to.equal(1);
|
|
305
|
+
store.beta.value = 3;
|
|
306
|
+
(0, vitest_1.expect)(runs).to.equal(1);
|
|
307
|
+
});
|
|
308
|
+
(0, vitest_1.it)("should not retrigger effects for untouched Set entries", () => {
|
|
309
|
+
const store = (0, index_1.deepSignal)({
|
|
310
|
+
set: new Set([
|
|
311
|
+
{ id: "a", data: { value: 1 } },
|
|
312
|
+
{ id: "b", data: { value: 2 } },
|
|
313
|
+
]),
|
|
314
|
+
}, {
|
|
315
|
+
syntheticIdPropertyName: "id",
|
|
316
|
+
propGenerator: ({ object }) => ({ syntheticId: object.id }),
|
|
317
|
+
});
|
|
318
|
+
const [entryA, entryB] = Array.from(store.set);
|
|
319
|
+
let runs = 0;
|
|
320
|
+
(0, __1.effect)(() => {
|
|
321
|
+
runs += 1;
|
|
322
|
+
entryA.data.value;
|
|
323
|
+
});
|
|
324
|
+
(0, vitest_1.expect)(runs).to.equal(1);
|
|
325
|
+
entryB.data.value = 5;
|
|
326
|
+
(0, vitest_1.expect)(runs).to.equal(1);
|
|
327
|
+
});
|
|
328
|
+
(0, vitest_1.it)("should subscribe to array iteration via Symbol.iterator", () => {
|
|
329
|
+
const store = (0, index_1.deepSignal)([1, 2]);
|
|
330
|
+
let total = 0;
|
|
331
|
+
(0, __1.effect)(() => {
|
|
332
|
+
total = 0;
|
|
333
|
+
for (const value of store) {
|
|
334
|
+
total += value;
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
(0, vitest_1.expect)(total).to.equal(3);
|
|
338
|
+
store.push(3);
|
|
339
|
+
(0, vitest_1.expect)(total).to.equal(6);
|
|
340
|
+
});
|
|
341
|
+
(0, vitest_1.it)("should subscribe to Set iteration via Symbol.iterator", () => {
|
|
342
|
+
const store = (0, index_1.deepSignal)({ set: new Set([1, 2]) });
|
|
343
|
+
let total = 0;
|
|
344
|
+
(0, __1.effect)(() => {
|
|
345
|
+
total = 0;
|
|
346
|
+
for (const value of store.set) {
|
|
347
|
+
total += value;
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
(0, vitest_1.expect)(total).to.equal(3);
|
|
351
|
+
store.set.add(3);
|
|
352
|
+
(0, vitest_1.expect)(total).to.equal(6);
|
|
353
|
+
store.set.delete(1);
|
|
354
|
+
(0, vitest_1.expect)(total).to.equal(5);
|
|
355
|
+
});
|
|
356
|
+
(0, vitest_1.it)("should subscribe when using Set iterator helper chains", () => {
|
|
357
|
+
const store = (0, index_1.deepSignal)({ set: new Set([1]) });
|
|
358
|
+
let reduced = 0;
|
|
359
|
+
(0, __1.effect)(() => {
|
|
360
|
+
reduced = store.set
|
|
361
|
+
.map((value) => value)
|
|
362
|
+
.reduce((acc, value) => acc + value, 0);
|
|
363
|
+
});
|
|
364
|
+
(0, vitest_1.expect)(reduced).to.equal(1);
|
|
365
|
+
store.set.add(2);
|
|
366
|
+
(0, vitest_1.expect)(reduced).to.equal(3);
|
|
367
|
+
store.set.delete(1);
|
|
368
|
+
(0, vitest_1.expect)(reduced).to.equal(2);
|
|
369
|
+
});
|
|
370
|
+
(0, vitest_1.it)("should subscribe to changes for Object.getOwnPropertyNames()", () => {
|
|
371
|
+
const state = { a: 1, b: 2 };
|
|
372
|
+
const store = (0, index_1.deepSignal)(state);
|
|
373
|
+
let sum = 0;
|
|
374
|
+
(0, __1.effect)(() => {
|
|
375
|
+
sum = 0;
|
|
376
|
+
const keys = Object.getOwnPropertyNames(store);
|
|
377
|
+
for (const _ of keys) {
|
|
378
|
+
sum += 1;
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
(0, vitest_1.expect)(sum).to.equal(2);
|
|
382
|
+
store.c = 0;
|
|
383
|
+
(0, vitest_1.expect)(sum).to.equal(3);
|
|
384
|
+
delete store.a;
|
|
385
|
+
(0, vitest_1.expect)(sum).to.equal(2);
|
|
386
|
+
});
|
|
387
|
+
(0, vitest_1.it)("should subscribe to changes to Object.keys/values/entries()", () => {
|
|
388
|
+
const state = { a: 1, b: 2 };
|
|
389
|
+
const store = (0, index_1.deepSignal)(state);
|
|
390
|
+
let keys = 0;
|
|
391
|
+
let values = 0;
|
|
392
|
+
let entries = 0;
|
|
393
|
+
(0, __1.effect)(() => {
|
|
394
|
+
keys = 0;
|
|
395
|
+
Object.keys(store).forEach(() => (keys += 1));
|
|
396
|
+
});
|
|
397
|
+
(0, __1.effect)(() => {
|
|
398
|
+
values = 0;
|
|
399
|
+
Object.values(store).forEach(() => (values += 1));
|
|
400
|
+
});
|
|
401
|
+
(0, __1.effect)(() => {
|
|
402
|
+
entries = 0;
|
|
403
|
+
Object.entries(store).forEach(() => (entries += 1));
|
|
404
|
+
});
|
|
405
|
+
(0, vitest_1.expect)(keys).to.equal(2);
|
|
406
|
+
(0, vitest_1.expect)(values).to.equal(2);
|
|
407
|
+
(0, vitest_1.expect)(entries).to.equal(2);
|
|
408
|
+
store.c = 0;
|
|
409
|
+
(0, vitest_1.expect)(keys).to.equal(3);
|
|
410
|
+
(0, vitest_1.expect)(values).to.equal(3);
|
|
411
|
+
(0, vitest_1.expect)(entries).to.equal(3);
|
|
412
|
+
delete store.a;
|
|
413
|
+
(0, vitest_1.expect)(keys).to.equal(2);
|
|
414
|
+
(0, vitest_1.expect)(values).to.equal(2);
|
|
415
|
+
(0, vitest_1.expect)(entries).to.equal(2);
|
|
416
|
+
});
|
|
417
|
+
(0, vitest_1.it)("should subscribe to changes to for..of loops", () => {
|
|
418
|
+
const store = (0, index_1.deepSignal)([0, 0]);
|
|
419
|
+
let sum = 0;
|
|
420
|
+
(0, __1.effect)(() => {
|
|
421
|
+
sum = 0;
|
|
422
|
+
for (const _ of store) {
|
|
423
|
+
sum += 1;
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
(0, vitest_1.expect)(sum).to.equal(2);
|
|
427
|
+
store.push(0);
|
|
428
|
+
(0, vitest_1.expect)(sum).to.equal(3);
|
|
429
|
+
store.splice(0, 1);
|
|
430
|
+
(0, vitest_1.expect)(sum).to.equal(2);
|
|
431
|
+
});
|
|
432
|
+
(0, vitest_1.it)("should subscribe to implicit changes in array items", () => {
|
|
433
|
+
const store = (0, index_1.deepSignal)(["foo", "bar"]);
|
|
434
|
+
let x = "";
|
|
435
|
+
(0, __1.effect)(() => {
|
|
436
|
+
x = store.join(" ");
|
|
437
|
+
console.log("joined", x);
|
|
438
|
+
});
|
|
439
|
+
(0, vitest_1.expect)(x).to.equal("foo bar");
|
|
440
|
+
store.push("baz");
|
|
441
|
+
(0, vitest_1.expect)(x).to.equal("foo bar baz");
|
|
442
|
+
store.splice(0, 1);
|
|
443
|
+
(0, vitest_1.expect)(x).to.equal("bar baz");
|
|
444
|
+
store.splice(1, 1, "bam");
|
|
445
|
+
(0, vitest_1.expect)(x).to.equal("bar bam");
|
|
446
|
+
});
|
|
447
|
+
(0, vitest_1.it)("should subscribe to changes when deleting properties", () => {
|
|
448
|
+
let x, y;
|
|
449
|
+
(0, __1.effect)(() => {
|
|
450
|
+
x = store.a;
|
|
451
|
+
});
|
|
452
|
+
(0, __1.effect)(() => {
|
|
453
|
+
y = store.nested.b;
|
|
454
|
+
});
|
|
455
|
+
(0, vitest_1.expect)(x).to.equal(1);
|
|
456
|
+
delete store.a;
|
|
457
|
+
(0, vitest_1.expect)(x).to.equal(undefined);
|
|
458
|
+
(0, vitest_1.expect)(y).to.equal(2);
|
|
459
|
+
delete store.nested.b;
|
|
460
|
+
(0, vitest_1.expect)(y).to.equal(undefined);
|
|
461
|
+
});
|
|
462
|
+
(0, vitest_1.it)("should subscribe to changes when mutating objects", () => {
|
|
463
|
+
let x, y;
|
|
464
|
+
const store = (0, index_1.deepSignal)({
|
|
465
|
+
b: [
|
|
466
|
+
{ id: 1, nested: { id: 1 } },
|
|
467
|
+
{ id: 2, nested: { id: 2 } },
|
|
468
|
+
],
|
|
469
|
+
});
|
|
470
|
+
(0, __1.effect)(() => {
|
|
471
|
+
x = store.a?.id;
|
|
472
|
+
});
|
|
473
|
+
(0, __1.effect)(() => {
|
|
474
|
+
y = store.a?.nested.id;
|
|
475
|
+
});
|
|
476
|
+
(0, vitest_1.expect)(x).to.equal(undefined);
|
|
477
|
+
(0, vitest_1.expect)(y).to.equal(undefined);
|
|
478
|
+
store.a = store.b[0];
|
|
479
|
+
(0, vitest_1.expect)(x).to.equal(1);
|
|
480
|
+
(0, vitest_1.expect)(y).to.equal(1);
|
|
481
|
+
store.a = store.b[1];
|
|
482
|
+
(0, vitest_1.expect)(x).to.equal(2);
|
|
483
|
+
(0, vitest_1.expect)(y).to.equal(2);
|
|
484
|
+
store.a = undefined;
|
|
485
|
+
(0, vitest_1.expect)(x).to.equal(undefined);
|
|
486
|
+
(0, vitest_1.expect)(y).to.equal(undefined);
|
|
487
|
+
store.a = store.b[1];
|
|
488
|
+
(0, vitest_1.expect)(x).to.equal(2);
|
|
489
|
+
(0, vitest_1.expect)(y).to.equal(2);
|
|
490
|
+
});
|
|
491
|
+
(0, vitest_1.it)("should trigger effects after mutations happen", () => {
|
|
492
|
+
let x;
|
|
493
|
+
(0, __1.effect)(() => {
|
|
494
|
+
x = store.a;
|
|
495
|
+
});
|
|
496
|
+
(0, vitest_1.expect)(x).to.equal(1);
|
|
497
|
+
store.a = 11;
|
|
498
|
+
(0, vitest_1.expect)(x).to.equal(11);
|
|
499
|
+
});
|
|
500
|
+
(0, vitest_1.it)("should subscribe corretcly from getters", () => {
|
|
501
|
+
let x;
|
|
502
|
+
const store = (0, index_1.deepSignal)({
|
|
503
|
+
counter: 1,
|
|
504
|
+
get double() {
|
|
505
|
+
return store.counter * 2;
|
|
506
|
+
},
|
|
507
|
+
});
|
|
508
|
+
(0, __1.effect)(() => (x = store.double));
|
|
509
|
+
(0, vitest_1.expect)(x).to.equal(2);
|
|
510
|
+
store.counter = 2;
|
|
511
|
+
(0, vitest_1.expect)(x).to.equal(4);
|
|
512
|
+
});
|
|
513
|
+
(0, vitest_1.it)("should subscribe corretcly from getters returning other parts of the store", () => {
|
|
514
|
+
let data;
|
|
515
|
+
const store = (0, index_1.deepSignal)({
|
|
516
|
+
switch: "a",
|
|
517
|
+
a: { data: "a" },
|
|
518
|
+
b: { data: "b" },
|
|
519
|
+
get aOrB() {
|
|
520
|
+
return store.switch === "a" ? store.a : store.b;
|
|
521
|
+
},
|
|
522
|
+
});
|
|
523
|
+
(0, __1.effect)(() => (data = store.aOrB.data));
|
|
524
|
+
(0, vitest_1.expect)(data).to.equal("a");
|
|
525
|
+
store.switch = "b";
|
|
526
|
+
(0, vitest_1.expect)(data).to.equal("b");
|
|
527
|
+
});
|
|
528
|
+
(0, vitest_1.it)("should be able to reset values with Object.assign and still react to changes", () => {
|
|
529
|
+
const initialNested = { ...nested };
|
|
530
|
+
const initialState = { ...state, nested: initialNested };
|
|
531
|
+
let a, b;
|
|
532
|
+
(0, __1.effect)(() => {
|
|
533
|
+
a = store.a;
|
|
534
|
+
});
|
|
535
|
+
(0, __1.effect)(() => {
|
|
536
|
+
b = store.nested.b;
|
|
537
|
+
});
|
|
538
|
+
store.a = 2;
|
|
539
|
+
store.nested.b = 3;
|
|
540
|
+
(0, vitest_1.expect)(a).to.equal(2);
|
|
541
|
+
(0, vitest_1.expect)(b).to.equal(3);
|
|
542
|
+
Object.assign(store, initialState);
|
|
543
|
+
(0, vitest_1.expect)(a).to.equal(1);
|
|
544
|
+
(0, vitest_1.expect)(b).to.equal(2);
|
|
545
|
+
});
|
|
546
|
+
});
|
|
547
|
+
(0, vitest_1.describe)("refs", () => {
|
|
548
|
+
(0, vitest_1.it)("should change if children changed", async () => {
|
|
549
|
+
const signalObj = (0, index_1.deepSignal)({
|
|
550
|
+
primitive: 1,
|
|
551
|
+
nestedObject: { primitive: 2 },
|
|
552
|
+
nestedSetOfPrimitives: new Set([1, 2, "three"]),
|
|
553
|
+
nestedSetOfObjects: new Set([
|
|
554
|
+
{ "@id": "obj1", primitive: true },
|
|
555
|
+
{ "@id": "obj2", primitive: "false" },
|
|
556
|
+
]),
|
|
557
|
+
nestedArrayOfPrimitives: [1, 2, "three"],
|
|
558
|
+
nestedArrayOfObjects: [
|
|
559
|
+
{ "@id": "obj1", primitive: true },
|
|
560
|
+
{ "@id": "obj2", primitive: "false" },
|
|
561
|
+
],
|
|
562
|
+
});
|
|
563
|
+
// Capture initial references
|
|
564
|
+
let no = signalObj.nestedObject;
|
|
565
|
+
let nop = signalObj.nestedObject.primitive;
|
|
566
|
+
let nsop = signalObj.nestedSetOfPrimitives;
|
|
567
|
+
let nsoo = signalObj.nestedSetOfObjects;
|
|
568
|
+
let [nsoo1, nsoo2] = [...signalObj.nestedSetOfObjects];
|
|
569
|
+
let naop = signalObj.nestedArrayOfPrimitives;
|
|
570
|
+
let naoo = signalObj.nestedArrayOfObjects;
|
|
571
|
+
let [naoo1, naoo2] = signalObj.nestedArrayOfObjects;
|
|
572
|
+
// Mutate root primitive - should not affect nested proxies
|
|
573
|
+
signalObj.primitive = 2;
|
|
574
|
+
(0, vitest_1.expect)(signalObj.nestedObject).toBe(no);
|
|
575
|
+
(0, vitest_1.expect)(signalObj.nestedObject.primitive).toBe(nop);
|
|
576
|
+
(0, vitest_1.expect)(signalObj.nestedSetOfPrimitives).toBe(nsop);
|
|
577
|
+
(0, vitest_1.expect)(signalObj.nestedSetOfObjects).toBe(nsoo);
|
|
578
|
+
(0, vitest_1.expect)(signalObj.nestedArrayOfPrimitives).toBe(naop);
|
|
579
|
+
(0, vitest_1.expect)(signalObj.nestedArrayOfObjects).toBe(naoo);
|
|
580
|
+
// Mutate nested object primitive - should replace nestedObject proxy
|
|
581
|
+
signalObj.nestedObject.primitive = 3;
|
|
582
|
+
(0, vitest_1.expect)(signalObj.nestedObject.primitive).toBe(3);
|
|
583
|
+
(0, vitest_1.expect)(signalObj.nestedObject).not.toBe(no);
|
|
584
|
+
no = signalObj.nestedObject;
|
|
585
|
+
// Unrelated proxies should remain the same
|
|
586
|
+
(0, vitest_1.expect)(signalObj.nestedSetOfPrimitives).toBe(nsop);
|
|
587
|
+
(0, vitest_1.expect)(signalObj.nestedSetOfObjects).toBe(nsoo);
|
|
588
|
+
(0, vitest_1.expect)(signalObj.nestedArrayOfPrimitives).toBe(naop);
|
|
589
|
+
(0, vitest_1.expect)(signalObj.nestedArrayOfObjects).toBe(naoo);
|
|
590
|
+
// Mutate Set of primitives - should replace the Set proxy
|
|
591
|
+
signalObj.nestedSetOfPrimitives.add(4);
|
|
592
|
+
(0, vitest_1.expect)(signalObj.nestedSetOfPrimitives).not.toBe(nsop);
|
|
593
|
+
nsop = signalObj.nestedSetOfPrimitives;
|
|
594
|
+
(0, vitest_1.expect)(signalObj.nestedSetOfPrimitives.has(4)).toBe(true);
|
|
595
|
+
// Unrelated proxies should remain the same
|
|
596
|
+
(0, vitest_1.expect)(signalObj.nestedObject).toBe(no);
|
|
597
|
+
(0, vitest_1.expect)(signalObj.nestedSetOfObjects).toBe(nsoo);
|
|
598
|
+
(0, vitest_1.expect)(signalObj.nestedArrayOfPrimitives).toBe(naop);
|
|
599
|
+
(0, vitest_1.expect)(signalObj.nestedArrayOfObjects).toBe(naoo);
|
|
600
|
+
// Mutate object inside Set - should replace Set proxy and the object proxy
|
|
601
|
+
nsoo1.primitive = false;
|
|
602
|
+
(0, vitest_1.expect)([...signalObj.nestedSetOfObjects][1]).toBe(nsoo2);
|
|
603
|
+
(0, vitest_1.expect)([...signalObj.nestedSetOfObjects][0]).not.toBe(nsoo1);
|
|
604
|
+
(0, vitest_1.expect)(signalObj.nestedSetOfObjects).not.toBe(nsoo);
|
|
605
|
+
nsoo = signalObj.nestedSetOfObjects;
|
|
606
|
+
[nsoo1, nsoo2] = [...signalObj.nestedSetOfObjects];
|
|
607
|
+
// Unrelated proxies should remain the same
|
|
608
|
+
(0, vitest_1.expect)(signalObj.nestedObject).toBe(no);
|
|
609
|
+
(0, vitest_1.expect)(signalObj.nestedSetOfPrimitives).toBe(nsop);
|
|
610
|
+
(0, vitest_1.expect)(signalObj.nestedArrayOfPrimitives).toBe(naop);
|
|
611
|
+
(0, vitest_1.expect)(signalObj.nestedArrayOfObjects).toBe(naoo);
|
|
612
|
+
// Mutate array of primitives - should replace the array proxy
|
|
613
|
+
signalObj.nestedArrayOfPrimitives.push(4);
|
|
614
|
+
(0, vitest_1.expect)(signalObj.nestedArrayOfPrimitives).not.toBe(naop);
|
|
615
|
+
naop = signalObj.nestedArrayOfPrimitives;
|
|
616
|
+
(0, vitest_1.expect)(signalObj.nestedArrayOfPrimitives.length).toBe(4);
|
|
617
|
+
// Unrelated proxies should remain the same
|
|
618
|
+
(0, vitest_1.expect)(signalObj.nestedObject).toBe(no);
|
|
619
|
+
(0, vitest_1.expect)(signalObj.nestedSetOfPrimitives).toBe(nsop);
|
|
620
|
+
(0, vitest_1.expect)(signalObj.nestedSetOfObjects).toBe(nsoo);
|
|
621
|
+
(0, vitest_1.expect)(signalObj.nestedArrayOfObjects).toBe(naoo);
|
|
622
|
+
// Mutate object inside array - should replace array proxy and the object proxy
|
|
623
|
+
naoo1.primitive = false;
|
|
624
|
+
(0, vitest_1.expect)(signalObj.nestedArrayOfObjects[0]).not.toBe(naoo1);
|
|
625
|
+
(0, vitest_1.expect)(signalObj.nestedArrayOfObjects[1]).toBe(naoo2);
|
|
626
|
+
(0, vitest_1.expect)(signalObj.nestedArrayOfObjects).not.toBe(naoo);
|
|
627
|
+
naoo = signalObj.nestedArrayOfObjects;
|
|
628
|
+
[naoo1, naoo2] = signalObj.nestedArrayOfObjects;
|
|
629
|
+
// Unrelated proxies should remain the same
|
|
630
|
+
(0, vitest_1.expect)(signalObj.nestedObject).toBe(no);
|
|
631
|
+
(0, vitest_1.expect)(signalObj.nestedSetOfPrimitives).toBe(nsop);
|
|
632
|
+
(0, vitest_1.expect)(signalObj.nestedSetOfObjects).toBe(nsoo);
|
|
633
|
+
(0, vitest_1.expect)(signalObj.nestedArrayOfPrimitives).toBe(naop);
|
|
634
|
+
});
|
|
635
|
+
(0, vitest_1.it)("should return the same proxy if initialized more than once", () => {
|
|
636
|
+
const state = {};
|
|
637
|
+
const store1 = (0, index_1.deepSignal)(state);
|
|
638
|
+
const store2 = (0, index_1.deepSignal)(state);
|
|
639
|
+
(0, vitest_1.expect)(store1).to.equal(store2);
|
|
640
|
+
});
|
|
641
|
+
});
|
|
642
|
+
(0, vitest_1.describe)("unsupported data structures", () => {
|
|
643
|
+
(0, vitest_1.it)("should throw when trying to deepsignal a class instance", () => {
|
|
644
|
+
class MyClass {
|
|
645
|
+
}
|
|
646
|
+
const obj = new MyClass();
|
|
647
|
+
(0, vitest_1.expect)(() => (0, index_1.deepSignal)(obj)).to.throw();
|
|
648
|
+
});
|
|
649
|
+
(0, vitest_1.it)("should not wrap a class instance", () => {
|
|
650
|
+
class MyClass {
|
|
651
|
+
}
|
|
652
|
+
const obj = new MyClass();
|
|
653
|
+
const store = (0, index_1.deepSignal)({ obj });
|
|
654
|
+
(0, vitest_1.expect)(store.obj).to.equal(obj);
|
|
655
|
+
});
|
|
656
|
+
(0, vitest_1.it)("should not wrap built-ins in proxies", () => {
|
|
657
|
+
window.MyClass = class MyClass {
|
|
658
|
+
};
|
|
659
|
+
const obj = new window.MyClass();
|
|
660
|
+
const store = (0, index_1.deepSignal)({ obj });
|
|
661
|
+
(0, vitest_1.expect)(store.obj).to.equal(obj);
|
|
662
|
+
});
|
|
663
|
+
// it("should not wrap elements in proxies", () => {
|
|
664
|
+
// const el = window.document.createElement("div");
|
|
665
|
+
// const store = deepSignal({ el });
|
|
666
|
+
// expect(store.el).to.equal(el);
|
|
667
|
+
// });
|
|
668
|
+
(0, vitest_1.it)("should wrap global objects", () => {
|
|
669
|
+
window.obj = { b: 2 };
|
|
670
|
+
const store = (0, index_1.deepSignal)(window.obj);
|
|
671
|
+
(0, vitest_1.expect)(store).to.not.equal(window.obj);
|
|
672
|
+
(0, vitest_1.expect)(store).to.deep.equal({ b: 2 });
|
|
673
|
+
(0, vitest_1.expect)(store.b).to.equal(2);
|
|
674
|
+
});
|
|
675
|
+
(0, vitest_1.it)("should not wrap dates", () => {
|
|
676
|
+
const date = new Date();
|
|
677
|
+
const store = (0, index_1.deepSignal)({ date });
|
|
678
|
+
(0, vitest_1.expect)(store.date).to.equal(date);
|
|
679
|
+
});
|
|
680
|
+
(0, vitest_1.it)("should not wrap regular expressions", () => {
|
|
681
|
+
const regex = new RegExp("");
|
|
682
|
+
const store = (0, index_1.deepSignal)({ regex });
|
|
683
|
+
(0, vitest_1.expect)(store.regex).to.equal(regex);
|
|
684
|
+
});
|
|
685
|
+
(0, vitest_1.it)("should not wrap Map", () => {
|
|
686
|
+
const map = new Map();
|
|
687
|
+
const store = (0, index_1.deepSignal)({ map });
|
|
688
|
+
(0, vitest_1.expect)(store.map).to.equal(map);
|
|
689
|
+
});
|
|
690
|
+
(0, vitest_1.it)("should wrap Set and emit patches on structural changes", () => {
|
|
691
|
+
const set = new Set([1]);
|
|
692
|
+
const store = (0, index_1.deepSignal)({ set });
|
|
693
|
+
// The Set itself should be proxied (different reference)
|
|
694
|
+
(0, vitest_1.expect)(store.set).to.not.equal(set);
|
|
695
|
+
// Size observable via manual mutation + patch emission (indirectly validated in watchPatches Set test)
|
|
696
|
+
store.set.add(2);
|
|
697
|
+
store.set.delete(1);
|
|
698
|
+
(0, vitest_1.expect)(store.set.has(2)).to.equal(true);
|
|
699
|
+
});
|
|
700
|
+
(0, vitest_1.it)("should expose reactive Set size without throwing", () => {
|
|
701
|
+
const store = (0, index_1.deepSignal)({ set: new Set([1, 2]) });
|
|
702
|
+
let size = 0;
|
|
703
|
+
(0, __1.effect)(() => {
|
|
704
|
+
size = store.set.size;
|
|
705
|
+
});
|
|
706
|
+
(0, vitest_1.expect)(store.set.size).to.equal(2);
|
|
707
|
+
(0, vitest_1.expect)(size).to.equal(2);
|
|
708
|
+
store.set.add(3);
|
|
709
|
+
(0, vitest_1.expect)(store.set.size).to.equal(3);
|
|
710
|
+
(0, vitest_1.expect)(size).to.equal(3);
|
|
711
|
+
store.set.delete(1);
|
|
712
|
+
(0, vitest_1.expect)(store.set.size).to.equal(2);
|
|
713
|
+
(0, vitest_1.expect)(size).to.equal(2);
|
|
714
|
+
});
|
|
715
|
+
});
|
|
716
|
+
(0, vitest_1.describe)("symbols", () => {
|
|
717
|
+
(0, vitest_1.it)("should observe symbols", () => {
|
|
718
|
+
const key = Symbol("key");
|
|
719
|
+
let x;
|
|
720
|
+
const store = (0, index_1.deepSignal)({});
|
|
721
|
+
(0, __1.effect)(() => (x = store[key]));
|
|
722
|
+
(0, vitest_1.expect)(store[key]).to.equal(undefined);
|
|
723
|
+
(0, vitest_1.expect)(x).to.equal(undefined);
|
|
724
|
+
store[key] = true;
|
|
725
|
+
(0, vitest_1.expect)(store[key]).to.equal(true);
|
|
726
|
+
(0, vitest_1.expect)(x).to.equal(true);
|
|
727
|
+
});
|
|
728
|
+
(0, vitest_1.it)("should not observe well-known symbols", () => {
|
|
729
|
+
const key = Symbol.isConcatSpreadable;
|
|
730
|
+
let x;
|
|
731
|
+
const state = (0, index_1.deepSignal)({});
|
|
732
|
+
(0, __1.effect)(() => (x = state[key]));
|
|
733
|
+
(0, vitest_1.expect)(state[key]).to.equal(undefined);
|
|
734
|
+
(0, vitest_1.expect)(x).to.equal(undefined);
|
|
735
|
+
state[key] = true;
|
|
736
|
+
(0, vitest_1.expect)(state[key]).to.equal(true);
|
|
737
|
+
(0, vitest_1.expect)(x).to.equal(undefined);
|
|
738
|
+
});
|
|
739
|
+
});
|
|
740
|
+
(0, vitest_1.describe)("shallow", () => {
|
|
741
|
+
(0, vitest_1.it)("should not proxy shallow objects", () => {
|
|
742
|
+
const shallowObj1 = { a: 1 };
|
|
743
|
+
let shallowObj2 = { b: 2 };
|
|
744
|
+
const deepObj = { c: 3 };
|
|
745
|
+
shallowObj2 = (0, index_1.shallow)(shallowObj2);
|
|
746
|
+
const store = (0, index_1.deepSignal)({
|
|
747
|
+
shallowObj1: (0, index_1.shallow)(shallowObj1),
|
|
748
|
+
shallowObj2,
|
|
749
|
+
deepObj,
|
|
750
|
+
});
|
|
751
|
+
(0, vitest_1.expect)(store.shallowObj1.a).to.equal(1);
|
|
752
|
+
(0, vitest_1.expect)(store.shallowObj2.b).to.equal(2);
|
|
753
|
+
(0, vitest_1.expect)(store.deepObj.c).to.equal(3);
|
|
754
|
+
(0, vitest_1.expect)(store.shallowObj1).to.equal(shallowObj1);
|
|
755
|
+
(0, vitest_1.expect)(store.shallowObj2).to.equal(shallowObj2);
|
|
756
|
+
(0, vitest_1.expect)(store.deepObj).to.not.equal(deepObj);
|
|
757
|
+
});
|
|
758
|
+
(0, vitest_1.it)("should not proxy shallow objects if shallow is called on the reference before accessing the property", () => {
|
|
759
|
+
const shallowObj = { a: 1 };
|
|
760
|
+
const deepObj = { c: 3 };
|
|
761
|
+
const store = (0, index_1.deepSignal)({ shallowObj, deepObj });
|
|
762
|
+
(0, index_1.shallow)(shallowObj);
|
|
763
|
+
(0, vitest_1.expect)(store.shallowObj.a).to.equal(1);
|
|
764
|
+
(0, vitest_1.expect)(store.deepObj.c).to.equal(3);
|
|
765
|
+
(0, vitest_1.expect)(store.shallowObj).to.equal(shallowObj);
|
|
766
|
+
(0, vitest_1.expect)(store.deepObj).to.not.equal(deepObj);
|
|
767
|
+
});
|
|
768
|
+
(0, vitest_1.it)("should observe changes in the shallow object if the reference changes", () => {
|
|
769
|
+
const obj = { a: 1 };
|
|
770
|
+
const shallowObj = (0, index_1.shallow)(obj);
|
|
771
|
+
const store = (0, index_1.deepSignal)({ shallowObj });
|
|
772
|
+
let x;
|
|
773
|
+
(0, __1.effect)(() => {
|
|
774
|
+
x = store.shallowObj.a;
|
|
775
|
+
});
|
|
776
|
+
(0, vitest_1.expect)(x).to.equal(1);
|
|
777
|
+
store.shallowObj = (0, index_1.shallow)({ a: 2 });
|
|
778
|
+
(0, vitest_1.expect)(x).to.equal(2);
|
|
779
|
+
});
|
|
780
|
+
(0, vitest_1.it)("should stop observing changes in the shallow object if the reference changes and it's not shallow anymore", () => {
|
|
781
|
+
const obj = { a: 1 };
|
|
782
|
+
const shallowObj = (0, index_1.shallow)(obj);
|
|
783
|
+
const store = (0, index_1.deepSignal)({ obj: shallowObj });
|
|
784
|
+
let x;
|
|
785
|
+
(0, __1.effect)(() => {
|
|
786
|
+
x = store.obj.a;
|
|
787
|
+
});
|
|
788
|
+
(0, vitest_1.expect)(x).to.equal(1);
|
|
789
|
+
store.obj = { a: 2 };
|
|
790
|
+
(0, vitest_1.expect)(x).to.equal(2);
|
|
791
|
+
store.obj.a = 3;
|
|
792
|
+
(0, vitest_1.expect)(x).to.equal(3);
|
|
793
|
+
});
|
|
794
|
+
(0, vitest_1.it)("should not observe changes in the props of the shallow object", () => {
|
|
795
|
+
const obj = { a: 1 };
|
|
796
|
+
const shallowObj = (0, index_1.shallow)(obj);
|
|
797
|
+
const store = (0, index_1.deepSignal)({ shallowObj });
|
|
798
|
+
let x;
|
|
799
|
+
(0, __1.effect)(() => {
|
|
800
|
+
x = store.shallowObj.a;
|
|
801
|
+
});
|
|
802
|
+
(0, vitest_1.expect)(x).to.equal(1);
|
|
803
|
+
store.shallowObj.a = 2;
|
|
804
|
+
(0, vitest_1.expect)(x).to.equal(1);
|
|
805
|
+
});
|
|
806
|
+
});
|
|
807
|
+
});
|