@zeix/cause-effect 0.16.1 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.ai-context.md +71 -21
- package/.cursorrules +3 -2
- package/.github/copilot-instructions.md +59 -13
- package/CLAUDE.md +170 -24
- package/LICENSE +1 -1
- package/README.md +156 -52
- package/archive/benchmark.ts +688 -0
- package/archive/collection.ts +312 -0
- package/{src → archive}/computed.ts +19 -19
- package/archive/list.ts +551 -0
- package/archive/memo.ts +138 -0
- package/{src → archive}/state.ts +13 -11
- package/archive/store.ts +368 -0
- package/archive/task.ts +194 -0
- package/eslint.config.js +1 -0
- package/index.dev.js +899 -503
- package/index.js +1 -1
- package/index.ts +41 -22
- package/package.json +1 -1
- package/src/classes/collection.ts +272 -0
- package/src/classes/composite.ts +176 -0
- package/src/classes/computed.ts +333 -0
- package/src/classes/list.ts +304 -0
- package/src/classes/state.ts +98 -0
- package/src/classes/store.ts +210 -0
- package/src/diff.ts +26 -53
- package/src/effect.ts +9 -9
- package/src/errors.ts +50 -25
- package/src/signal.ts +58 -41
- package/src/system.ts +79 -42
- package/src/util.ts +16 -30
- package/test/batch.test.ts +15 -17
- package/test/benchmark.test.ts +4 -4
- package/test/collection.test.ts +796 -0
- package/test/computed.test.ts +138 -130
- package/test/diff.test.ts +2 -2
- package/test/effect.test.ts +36 -35
- package/test/list.test.ts +754 -0
- package/test/match.test.ts +25 -25
- package/test/resolve.test.ts +17 -19
- package/test/signal.test.ts +70 -119
- package/test/state.test.ts +44 -44
- package/test/store.test.ts +253 -929
- package/types/index.d.ts +10 -8
- package/types/src/classes/collection.d.ts +32 -0
- package/types/src/classes/composite.d.ts +15 -0
- package/types/src/classes/computed.d.ts +97 -0
- package/types/src/classes/list.d.ts +41 -0
- package/types/src/classes/state.d.ts +52 -0
- package/types/src/classes/store.d.ts +51 -0
- package/types/src/diff.d.ts +8 -12
- package/types/src/errors.d.ts +12 -11
- package/types/src/signal.d.ts +27 -14
- package/types/src/system.d.ts +41 -20
- package/types/src/util.d.ts +6 -3
- package/src/store.ts +0 -474
- package/types/src/collection.d.ts +0 -26
- package/types/src/computed.d.ts +0 -33
- package/types/src/scheduler.d.ts +0 -55
- package/types/src/state.d.ts +0 -24
- package/types/src/store.d.ts +0 -65
package/test/state.test.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { describe, expect, test } from 'bun:test'
|
|
2
|
-
import {
|
|
2
|
+
import { isComputed, isState, State } from '../index.ts'
|
|
3
3
|
|
|
4
4
|
/* === Tests === */
|
|
5
5
|
|
|
6
6
|
describe('State', () => {
|
|
7
7
|
describe('State type guard', () => {
|
|
8
8
|
test('isState identifies state signals', () => {
|
|
9
|
-
const count =
|
|
9
|
+
const count = new State(42)
|
|
10
10
|
expect(isState(count)).toBe(true)
|
|
11
11
|
expect(isComputed(count)).toBe(false)
|
|
12
12
|
})
|
|
@@ -14,28 +14,28 @@ describe('State', () => {
|
|
|
14
14
|
|
|
15
15
|
describe('Boolean cause', () => {
|
|
16
16
|
test('should be boolean', () => {
|
|
17
|
-
const cause =
|
|
17
|
+
const cause = new State(false)
|
|
18
18
|
expect(typeof cause.get()).toBe('boolean')
|
|
19
19
|
})
|
|
20
20
|
|
|
21
21
|
test('should set initial value to false', () => {
|
|
22
|
-
const cause =
|
|
22
|
+
const cause = new State(false)
|
|
23
23
|
expect(cause.get()).toBe(false)
|
|
24
24
|
})
|
|
25
25
|
|
|
26
26
|
test('should set initial value to true', () => {
|
|
27
|
-
const cause =
|
|
27
|
+
const cause = new State(true)
|
|
28
28
|
expect(cause.get()).toBe(true)
|
|
29
29
|
})
|
|
30
30
|
|
|
31
31
|
test('should set new value with .set(true)', () => {
|
|
32
|
-
const cause =
|
|
32
|
+
const cause = new State(false)
|
|
33
33
|
cause.set(true)
|
|
34
34
|
expect(cause.get()).toBe(true)
|
|
35
35
|
})
|
|
36
36
|
|
|
37
37
|
test('should toggle initial value with .set(v => !v)', () => {
|
|
38
|
-
const cause =
|
|
38
|
+
const cause = new State(false)
|
|
39
39
|
cause.update(v => !v)
|
|
40
40
|
expect(cause.get()).toBe(true)
|
|
41
41
|
})
|
|
@@ -43,23 +43,23 @@ describe('State', () => {
|
|
|
43
43
|
|
|
44
44
|
describe('Number cause', () => {
|
|
45
45
|
test('should be number', () => {
|
|
46
|
-
const cause =
|
|
46
|
+
const cause = new State(0)
|
|
47
47
|
expect(typeof cause.get()).toBe('number')
|
|
48
48
|
})
|
|
49
49
|
|
|
50
50
|
test('should set initial value to 0', () => {
|
|
51
|
-
const cause =
|
|
51
|
+
const cause = new State(0)
|
|
52
52
|
expect(cause.get()).toBe(0)
|
|
53
53
|
})
|
|
54
54
|
|
|
55
55
|
test('should set new value with .set(42)', () => {
|
|
56
|
-
const cause =
|
|
56
|
+
const cause = new State(0)
|
|
57
57
|
cause.set(42)
|
|
58
58
|
expect(cause.get()).toBe(42)
|
|
59
59
|
})
|
|
60
60
|
|
|
61
61
|
test('should increment value with .set(v => ++v)', () => {
|
|
62
|
-
const cause =
|
|
62
|
+
const cause = new State(0)
|
|
63
63
|
cause.update(v => ++v)
|
|
64
64
|
expect(cause.get()).toBe(1)
|
|
65
65
|
})
|
|
@@ -67,23 +67,23 @@ describe('State', () => {
|
|
|
67
67
|
|
|
68
68
|
describe('String cause', () => {
|
|
69
69
|
test('should be string', () => {
|
|
70
|
-
const cause =
|
|
70
|
+
const cause = new State('foo')
|
|
71
71
|
expect(typeof cause.get()).toBe('string')
|
|
72
72
|
})
|
|
73
73
|
|
|
74
74
|
test('should set initial value to "foo"', () => {
|
|
75
|
-
const cause =
|
|
75
|
+
const cause = new State('foo')
|
|
76
76
|
expect(cause.get()).toBe('foo')
|
|
77
77
|
})
|
|
78
78
|
|
|
79
79
|
test('should set new value with .set("bar")', () => {
|
|
80
|
-
const cause =
|
|
80
|
+
const cause = new State('foo')
|
|
81
81
|
cause.set('bar')
|
|
82
82
|
expect(cause.get()).toBe('bar')
|
|
83
83
|
})
|
|
84
84
|
|
|
85
85
|
test('should upper case value with .set(v => v.toUpperCase())', () => {
|
|
86
|
-
const cause =
|
|
86
|
+
const cause = new State('foo')
|
|
87
87
|
cause.update(v => (v ? v.toUpperCase() : ''))
|
|
88
88
|
expect(cause.get()).toBe('FOO')
|
|
89
89
|
})
|
|
@@ -91,31 +91,31 @@ describe('State', () => {
|
|
|
91
91
|
|
|
92
92
|
describe('Array cause', () => {
|
|
93
93
|
test('should be array', () => {
|
|
94
|
-
const cause =
|
|
94
|
+
const cause = new State([1, 2, 3])
|
|
95
95
|
expect(Array.isArray(cause.get())).toBe(true)
|
|
96
96
|
})
|
|
97
97
|
|
|
98
98
|
test('should set initial value to [1, 2, 3]', () => {
|
|
99
|
-
const cause =
|
|
99
|
+
const cause = new State([1, 2, 3])
|
|
100
100
|
expect(cause.get()).toEqual([1, 2, 3])
|
|
101
101
|
})
|
|
102
102
|
|
|
103
103
|
test('should set new value with .set([4, 5, 6])', () => {
|
|
104
|
-
const cause =
|
|
104
|
+
const cause = new State([1, 2, 3])
|
|
105
105
|
cause.set([4, 5, 6])
|
|
106
106
|
expect(cause.get()).toEqual([4, 5, 6])
|
|
107
107
|
})
|
|
108
108
|
|
|
109
109
|
test('should reflect current value of array after modification', () => {
|
|
110
110
|
const array = [1, 2, 3]
|
|
111
|
-
const cause =
|
|
111
|
+
const cause = new State(array)
|
|
112
112
|
array.push(4) // don't do this! the result will be correct, but we can't trigger effects
|
|
113
113
|
expect(cause.get()).toEqual([1, 2, 3, 4])
|
|
114
114
|
})
|
|
115
115
|
|
|
116
116
|
test('should set new value with .set([...array, 4])', () => {
|
|
117
117
|
const array = [1, 2, 3]
|
|
118
|
-
const cause =
|
|
118
|
+
const cause = new State(array)
|
|
119
119
|
cause.set([...array, 4]) // use destructuring instead!
|
|
120
120
|
expect(cause.get()).toEqual([1, 2, 3, 4])
|
|
121
121
|
})
|
|
@@ -124,17 +124,17 @@ describe('State', () => {
|
|
|
124
124
|
test('should throw NullishSignalValueError when initialValue is nullish', () => {
|
|
125
125
|
expect(() => {
|
|
126
126
|
// @ts-expect-error - Testing invalid input
|
|
127
|
-
|
|
127
|
+
new State(null)
|
|
128
128
|
}).toThrow('Nullish signal values are not allowed in state')
|
|
129
129
|
|
|
130
130
|
expect(() => {
|
|
131
131
|
// @ts-expect-error - Testing invalid input
|
|
132
|
-
|
|
132
|
+
new State(undefined)
|
|
133
133
|
}).toThrow('Nullish signal values are not allowed in state')
|
|
134
134
|
})
|
|
135
135
|
|
|
136
136
|
test('should throw NullishSignalValueError when newValue is nullish in set()', () => {
|
|
137
|
-
const state =
|
|
137
|
+
const state = new State(42)
|
|
138
138
|
|
|
139
139
|
expect(() => {
|
|
140
140
|
// @ts-expect-error - Testing invalid input
|
|
@@ -150,7 +150,7 @@ describe('State', () => {
|
|
|
150
150
|
test('should throw specific error types for nullish values', () => {
|
|
151
151
|
try {
|
|
152
152
|
// @ts-expect-error - Testing invalid input
|
|
153
|
-
|
|
153
|
+
new State(null)
|
|
154
154
|
expect(true).toBe(false) // Should not reach here
|
|
155
155
|
} catch (error) {
|
|
156
156
|
expect(error).toBeInstanceOf(TypeError)
|
|
@@ -160,7 +160,7 @@ describe('State', () => {
|
|
|
160
160
|
)
|
|
161
161
|
}
|
|
162
162
|
|
|
163
|
-
const state =
|
|
163
|
+
const state = new State(42)
|
|
164
164
|
try {
|
|
165
165
|
// @ts-expect-error - Testing invalid input
|
|
166
166
|
state.set(null)
|
|
@@ -177,26 +177,26 @@ describe('State', () => {
|
|
|
177
177
|
test('should allow valid non-nullish values', () => {
|
|
178
178
|
// These should not throw
|
|
179
179
|
expect(() => {
|
|
180
|
-
|
|
180
|
+
new State(0)
|
|
181
181
|
}).not.toThrow()
|
|
182
182
|
|
|
183
183
|
expect(() => {
|
|
184
|
-
|
|
184
|
+
new State('')
|
|
185
185
|
}).not.toThrow()
|
|
186
186
|
|
|
187
187
|
expect(() => {
|
|
188
|
-
|
|
188
|
+
new State(false)
|
|
189
189
|
}).not.toThrow()
|
|
190
190
|
|
|
191
191
|
expect(() => {
|
|
192
|
-
|
|
192
|
+
new State({})
|
|
193
193
|
}).not.toThrow()
|
|
194
194
|
|
|
195
195
|
expect(() => {
|
|
196
|
-
|
|
196
|
+
new State([])
|
|
197
197
|
}).not.toThrow()
|
|
198
198
|
|
|
199
|
-
const state =
|
|
199
|
+
const state = new State(42)
|
|
200
200
|
expect(() => {
|
|
201
201
|
state.set(0)
|
|
202
202
|
}).not.toThrow()
|
|
@@ -208,7 +208,7 @@ describe('State', () => {
|
|
|
208
208
|
})
|
|
209
209
|
|
|
210
210
|
test('should throw InvalidCallbackError for non-function updater in update()', () => {
|
|
211
|
-
const state =
|
|
211
|
+
const state = new State(42)
|
|
212
212
|
|
|
213
213
|
expect(() => {
|
|
214
214
|
// @ts-expect-error - Testing invalid input
|
|
@@ -232,7 +232,7 @@ describe('State', () => {
|
|
|
232
232
|
})
|
|
233
233
|
|
|
234
234
|
test('should throw specific error type for non-function updater', () => {
|
|
235
|
-
const state =
|
|
235
|
+
const state = new State(42)
|
|
236
236
|
|
|
237
237
|
try {
|
|
238
238
|
// @ts-expect-error - Testing invalid input
|
|
@@ -248,7 +248,7 @@ describe('State', () => {
|
|
|
248
248
|
})
|
|
249
249
|
|
|
250
250
|
test('should handle updater function that throws an error', () => {
|
|
251
|
-
const state =
|
|
251
|
+
const state = new State(42)
|
|
252
252
|
|
|
253
253
|
expect(() => {
|
|
254
254
|
state.update(() => {
|
|
@@ -261,7 +261,7 @@ describe('State', () => {
|
|
|
261
261
|
})
|
|
262
262
|
|
|
263
263
|
test('should handle updater function that returns nullish value', () => {
|
|
264
|
-
const state =
|
|
264
|
+
const state = new State(42)
|
|
265
265
|
|
|
266
266
|
expect(() => {
|
|
267
267
|
// @ts-expect-error - Testing invalid return value
|
|
@@ -278,25 +278,25 @@ describe('State', () => {
|
|
|
278
278
|
})
|
|
279
279
|
|
|
280
280
|
test('should handle valid updater functions', () => {
|
|
281
|
-
const numberState =
|
|
281
|
+
const numberState = new State(10)
|
|
282
282
|
expect(() => {
|
|
283
283
|
numberState.update(x => x + 5)
|
|
284
284
|
}).not.toThrow()
|
|
285
285
|
expect(numberState.get()).toBe(15)
|
|
286
286
|
|
|
287
|
-
const stringState =
|
|
287
|
+
const stringState = new State('hello')
|
|
288
288
|
expect(() => {
|
|
289
289
|
stringState.update(x => x.toUpperCase())
|
|
290
290
|
}).not.toThrow()
|
|
291
291
|
expect(stringState.get()).toBe('HELLO')
|
|
292
292
|
|
|
293
|
-
const arrayState =
|
|
293
|
+
const arrayState = new State([1, 2, 3])
|
|
294
294
|
expect(() => {
|
|
295
295
|
arrayState.update(arr => [...arr, 4])
|
|
296
296
|
}).not.toThrow()
|
|
297
297
|
expect(arrayState.get()).toEqual([1, 2, 3, 4])
|
|
298
298
|
|
|
299
|
-
const objectState =
|
|
299
|
+
const objectState = new State({ count: 0 })
|
|
300
300
|
expect(() => {
|
|
301
301
|
objectState.update(obj => ({
|
|
302
302
|
...obj,
|
|
@@ -310,24 +310,24 @@ describe('State', () => {
|
|
|
310
310
|
|
|
311
311
|
describe('Object cause', () => {
|
|
312
312
|
test('should be object', () => {
|
|
313
|
-
const cause =
|
|
313
|
+
const cause = new State({ a: 'a', b: 1 })
|
|
314
314
|
expect(typeof cause.get()).toBe('object')
|
|
315
315
|
})
|
|
316
316
|
|
|
317
317
|
test('should set initial value to { a: "a", b: 1 }', () => {
|
|
318
|
-
const cause =
|
|
318
|
+
const cause = new State({ a: 'a', b: 1 })
|
|
319
319
|
expect(cause.get()).toEqual({ a: 'a', b: 1 })
|
|
320
320
|
})
|
|
321
321
|
|
|
322
322
|
test('should set new value with .set({ c: true })', () => {
|
|
323
|
-
const cause =
|
|
323
|
+
const cause = new State<Record<string, unknown>>({ a: 'a', b: 1 })
|
|
324
324
|
cause.set({ c: true })
|
|
325
325
|
expect(cause.get()).toEqual({ c: true })
|
|
326
326
|
})
|
|
327
327
|
|
|
328
328
|
test('should reflect current value of object after modification', () => {
|
|
329
329
|
const obj = { a: 'a', b: 1 }
|
|
330
|
-
const cause =
|
|
330
|
+
const cause = new State<Record<string, unknown>>(obj)
|
|
331
331
|
// @ts-expect-error Property 'c' does not exist on type '{ a: string; b: number; }'. (ts 2339)
|
|
332
332
|
obj.c = true // don't do this! the result will be correct, but we can't trigger effects
|
|
333
333
|
expect(cause.get()).toEqual({ a: 'a', b: 1, c: true })
|
|
@@ -335,7 +335,7 @@ describe('State', () => {
|
|
|
335
335
|
|
|
336
336
|
test('should set new value with .set({...obj, c: true})', () => {
|
|
337
337
|
const obj = { a: 'a', b: 1 }
|
|
338
|
-
const cause =
|
|
338
|
+
const cause = new State<Record<string, unknown>>(obj)
|
|
339
339
|
cause.set({ ...obj, c: true }) // use destructuring instead!
|
|
340
340
|
expect(cause.get()).toEqual({ a: 'a', b: 1, c: true })
|
|
341
341
|
})
|