@zeix/cause-effect 0.14.1 → 0.15.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.
Files changed (45) hide show
  1. package/README.md +256 -27
  2. package/biome.json +35 -0
  3. package/index.d.ts +32 -7
  4. package/index.dev.js +629 -0
  5. package/index.js +1 -1
  6. package/index.ts +41 -21
  7. package/package.json +6 -7
  8. package/src/computed.ts +30 -21
  9. package/src/diff.ts +136 -0
  10. package/src/effect.ts +59 -49
  11. package/src/match.ts +57 -0
  12. package/src/resolve.ts +58 -0
  13. package/src/scheduler.ts +3 -3
  14. package/src/signal.ts +48 -15
  15. package/src/state.ts +4 -3
  16. package/src/store.ts +325 -0
  17. package/src/util.ts +57 -5
  18. package/test/batch.test.ts +29 -25
  19. package/test/benchmark.test.ts +81 -45
  20. package/test/computed.test.ts +43 -39
  21. package/test/diff.test.ts +638 -0
  22. package/test/effect.test.ts +657 -49
  23. package/test/match.test.ts +378 -0
  24. package/test/resolve.test.ts +156 -0
  25. package/test/state.test.ts +33 -33
  26. package/test/store.test.ts +719 -0
  27. package/test/util/framework-types.ts +2 -2
  28. package/test/util/perf-tests.ts +2 -2
  29. package/test/util/reactive-framework.ts +1 -1
  30. package/tsconfig.json +9 -10
  31. package/types/index.d.ts +15 -0
  32. package/{src → types/src}/computed.d.ts +2 -2
  33. package/types/src/diff.d.ts +27 -0
  34. package/types/src/effect.d.ts +16 -0
  35. package/types/src/match.d.ts +21 -0
  36. package/types/src/resolve.d.ts +29 -0
  37. package/{src → types/src}/scheduler.d.ts +2 -2
  38. package/types/src/signal.d.ts +40 -0
  39. package/{src → types/src}/state.d.ts +1 -1
  40. package/types/src/store.d.ts +57 -0
  41. package/types/src/util.d.ts +15 -0
  42. package/types/test-new-effect.d.ts +1 -0
  43. package/src/effect.d.ts +0 -17
  44. package/src/signal.d.ts +0 -26
  45. package/src/util.d.ts +0 -7
@@ -1,9 +1,9 @@
1
- import { describe, test, expect } from 'bun:test'
1
+ import { describe, expect, test } from 'bun:test'
2
2
  import { isComputed, isState, state } from '../'
3
3
 
4
4
  /* === Tests === */
5
5
 
6
- describe('State', function () {
6
+ describe('State', () => {
7
7
  describe('State type guard', () => {
8
8
  test('isState identifies state signals', () => {
9
9
  const count = state(42)
@@ -12,108 +12,108 @@ describe('State', function () {
12
12
  })
13
13
  })
14
14
 
15
- describe('Boolean cause', function () {
16
- test('should be boolean', function () {
15
+ describe('Boolean cause', () => {
16
+ test('should be boolean', () => {
17
17
  const cause = state(false)
18
18
  expect(typeof cause.get()).toBe('boolean')
19
19
  })
20
20
 
21
- test('should set initial value to false', function () {
21
+ test('should set initial value to false', () => {
22
22
  const cause = state(false)
23
23
  expect(cause.get()).toBe(false)
24
24
  })
25
25
 
26
- test('should set initial value to true', function () {
26
+ test('should set initial value to true', () => {
27
27
  const cause = state(true)
28
28
  expect(cause.get()).toBe(true)
29
29
  })
30
30
 
31
- test('should set new value with .set(true)', function () {
31
+ test('should set new value with .set(true)', () => {
32
32
  const cause = state(false)
33
33
  cause.set(true)
34
34
  expect(cause.get()).toBe(true)
35
35
  })
36
36
 
37
- test('should toggle initial value with .set(v => !v)', function () {
37
+ test('should toggle initial value with .set(v => !v)', () => {
38
38
  const cause = state(false)
39
39
  cause.update(v => !v)
40
40
  expect(cause.get()).toBe(true)
41
41
  })
42
42
  })
43
43
 
44
- describe('Number cause', function () {
45
- test('should be number', function () {
44
+ describe('Number cause', () => {
45
+ test('should be number', () => {
46
46
  const cause = state(0)
47
47
  expect(typeof cause.get()).toBe('number')
48
48
  })
49
49
 
50
- test('should set initial value to 0', function () {
50
+ test('should set initial value to 0', () => {
51
51
  const cause = state(0)
52
52
  expect(cause.get()).toBe(0)
53
53
  })
54
54
 
55
- test('should set new value with .set(42)', function () {
55
+ test('should set new value with .set(42)', () => {
56
56
  const cause = state(0)
57
57
  cause.set(42)
58
58
  expect(cause.get()).toBe(42)
59
59
  })
60
60
 
61
- test('should increment value with .set(v => ++v)', function () {
61
+ test('should increment value with .set(v => ++v)', () => {
62
62
  const cause = state(0)
63
63
  cause.update(v => ++v)
64
64
  expect(cause.get()).toBe(1)
65
65
  })
66
66
  })
67
67
 
68
- describe('String cause', function () {
69
- test('should be string', function () {
68
+ describe('String cause', () => {
69
+ test('should be string', () => {
70
70
  const cause = state('foo')
71
71
  expect(typeof cause.get()).toBe('string')
72
72
  })
73
73
 
74
- test('should set initial value to "foo"', function () {
74
+ test('should set initial value to "foo"', () => {
75
75
  const cause = state('foo')
76
76
  expect(cause.get()).toBe('foo')
77
77
  })
78
78
 
79
- test('should set new value with .set("bar")', function () {
79
+ test('should set new value with .set("bar")', () => {
80
80
  const cause = state('foo')
81
81
  cause.set('bar')
82
82
  expect(cause.get()).toBe('bar')
83
83
  })
84
84
 
85
- test('should upper case value with .set(v => v.toUpperCase())', function () {
85
+ test('should upper case value with .set(v => v.toUpperCase())', () => {
86
86
  const cause = state('foo')
87
87
  cause.update(v => (v ? v.toUpperCase() : ''))
88
88
  expect(cause.get()).toBe('FOO')
89
89
  })
90
90
  })
91
91
 
92
- describe('Array cause', function () {
93
- test('should be array', function () {
92
+ describe('Array cause', () => {
93
+ test('should be array', () => {
94
94
  const cause = state([1, 2, 3])
95
95
  expect(Array.isArray(cause.get())).toBe(true)
96
96
  })
97
97
 
98
- test('should set initial value to [1, 2, 3]', function () {
98
+ test('should set initial value to [1, 2, 3]', () => {
99
99
  const cause = state([1, 2, 3])
100
100
  expect(cause.get()).toEqual([1, 2, 3])
101
101
  })
102
102
 
103
- test('should set new value with .set([4, 5, 6])', function () {
103
+ test('should set new value with .set([4, 5, 6])', () => {
104
104
  const cause = 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
- test('should reflect current value of array after modification', function () {
109
+ test('should reflect current value of array after modification', () => {
110
110
  const array = [1, 2, 3]
111
111
  const cause = 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
- test('should set new value with .set([...array, 4])', function () {
116
+ test('should set new value with .set([...array, 4])', () => {
117
117
  const array = [1, 2, 3]
118
118
  const cause = state(array)
119
119
  cause.set([...array, 4]) // use destructuring instead!
@@ -121,34 +121,34 @@ describe('State', function () {
121
121
  })
122
122
  })
123
123
 
124
- describe('Object cause', function () {
125
- test('should be object', function () {
124
+ describe('Object cause', () => {
125
+ test('should be object', () => {
126
126
  const cause = state({ a: 'a', b: 1 })
127
127
  expect(typeof cause.get()).toBe('object')
128
128
  })
129
129
 
130
- test('should set initial value to { a: "a", b: 1 }', function () {
130
+ test('should set initial value to { a: "a", b: 1 }', () => {
131
131
  const cause = state({ a: 'a', b: 1 })
132
132
  expect(cause.get()).toEqual({ a: 'a', b: 1 })
133
133
  })
134
134
 
135
- test('should set new value with .set({ c: true })', function () {
136
- const cause = state<Record<string, any>>({ a: 'a', b: 1 })
135
+ test('should set new value with .set({ c: true })', () => {
136
+ const cause = state<Record<string, unknown>>({ a: 'a', b: 1 })
137
137
  cause.set({ c: true })
138
138
  expect(cause.get()).toEqual({ c: true })
139
139
  })
140
140
 
141
- test('should reflect current value of object after modification', function () {
141
+ test('should reflect current value of object after modification', () => {
142
142
  const obj = { a: 'a', b: 1 }
143
- const cause = state<Record<string, any>>(obj)
143
+ const cause = state<Record<string, unknown>>(obj)
144
144
  // @ts-expect-error Property 'c' does not exist on type '{ a: string; b: number; }'. (ts 2339)
145
145
  obj.c = true // don't do this! the result will be correct, but we can't trigger effects
146
146
  expect(cause.get()).toEqual({ a: 'a', b: 1, c: true })
147
147
  })
148
148
 
149
- test('should set new value with .set({...obj, c: true})', function () {
149
+ test('should set new value with .set({...obj, c: true})', () => {
150
150
  const obj = { a: 'a', b: 1 }
151
- const cause = state<Record<string, any>>(obj)
151
+ const cause = state<Record<string, unknown>>(obj)
152
152
  cause.set({ ...obj, c: true }) // use destructuring instead!
153
153
  expect(cause.get()).toEqual({ a: 'a', b: 1, c: true })
154
154
  })