@zeix/cause-effect 0.14.1 → 0.14.2

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.
@@ -1,5 +1,5 @@
1
- import { describe, test, expect, mock } from 'bun:test'
2
- import { state, computed, effect, UNSET } from '../'
1
+ import { describe, expect, mock, test } from 'bun:test'
2
+ import { computed, effect, state, UNSET } from '../'
3
3
 
4
4
  /* === Utility Functions === */
5
5
 
@@ -7,11 +7,11 @@ const wait = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
7
7
 
8
8
  /* === Tests === */
9
9
 
10
- describe('Effect', function () {
11
- test('should be triggered after a state change', function () {
10
+ describe('Effect', () => {
11
+ test('should be triggered after a state change', () => {
12
12
  const cause = state('foo')
13
13
  let count = 0
14
- effect(() => {
14
+ effect((): undefined => {
15
15
  cause.get()
16
16
  count++
17
17
  })
@@ -20,7 +20,7 @@ describe('Effect', function () {
20
20
  expect(count).toBe(2)
21
21
  })
22
22
 
23
- test('should be triggered after computed async signals resolve without waterfalls', async function () {
23
+ test('should be triggered after computed async signals resolve without waterfalls', async () => {
24
24
  const a = computed(async () => {
25
25
  await wait(100)
26
26
  return 10
@@ -33,7 +33,7 @@ describe('Effect', function () {
33
33
  let count = 0
34
34
  effect({
35
35
  signals: [a, b],
36
- ok: (aValue, bValue) => {
36
+ ok: (aValue, bValue): undefined => {
37
37
  result = aValue + bValue
38
38
  count++
39
39
  },
@@ -45,11 +45,11 @@ describe('Effect', function () {
45
45
  expect(count).toBe(1)
46
46
  })
47
47
 
48
- test('should be triggered repeatedly after repeated state change', async function () {
48
+ test('should be triggered repeatedly after repeated state change', async () => {
49
49
  const cause = state(0)
50
50
  let result = 0
51
51
  let count = 0
52
- effect(() => {
52
+ effect((): undefined => {
53
53
  result = cause.get()
54
54
  count++
55
55
  })
@@ -60,7 +60,7 @@ describe('Effect', function () {
60
60
  }
61
61
  })
62
62
 
63
- test('should handle errors in effects', function () {
63
+ test('should handle errors in effects', () => {
64
64
  const a = state(1)
65
65
  const b = computed(() => {
66
66
  const v = a.get()
@@ -71,11 +71,11 @@ describe('Effect', function () {
71
71
  let errorCallCount = 0
72
72
  effect({
73
73
  signals: [b],
74
- ok: () => {
74
+ ok: (): undefined => {
75
75
  // console.log('Normal effect:', value)
76
76
  normalCallCount++
77
77
  },
78
- err: error => {
78
+ err: (error: Error): undefined => {
79
79
  // console.log('Error effect:', error)
80
80
  errorCallCount++
81
81
  expect(error.message).toBe('Value too high')
@@ -98,7 +98,7 @@ describe('Effect', function () {
98
98
  expect(errorCallCount).toBe(1)
99
99
  })
100
100
 
101
- test('should handle UNSET values in effects', async function () {
101
+ test('should handle UNSET values in effects', async () => {
102
102
  const a = computed(async () => {
103
103
  await wait(100)
104
104
  return 42
@@ -107,11 +107,11 @@ describe('Effect', function () {
107
107
  let nilCount = 0
108
108
  effect({
109
109
  signals: [a],
110
- ok: aValue => {
110
+ ok: (aValue: number): undefined => {
111
111
  normalCallCount++
112
112
  expect(aValue).toBe(42)
113
113
  },
114
- nil: () => {
114
+ nil: (): undefined => {
115
115
  nilCount++
116
116
  },
117
117
  })
@@ -140,7 +140,7 @@ describe('Effect', function () {
140
140
  })
141
141
 
142
142
  // Create an effect without explicit error handling
143
- effect(() => {
143
+ effect((): undefined => {
144
144
  b.get()
145
145
  })
146
146
 
@@ -164,7 +164,7 @@ describe('Effect', function () {
164
164
  const count = state(42)
165
165
  let received = 0
166
166
 
167
- const cleanup = effect(() => {
167
+ const cleanup = effect((): undefined => {
168
168
  received = count.get()
169
169
  })
170
170
 
@@ -183,12 +183,12 @@ describe('Effect', function () {
183
183
 
184
184
  effect({
185
185
  signals: [count],
186
- ok: () => {
186
+ ok: (): undefined => {
187
187
  okCount++
188
188
  // This effect updates the signal it depends on, creating a circular dependency
189
189
  count.update(v => ++v)
190
190
  },
191
- err: e => {
191
+ err: (e): undefined => {
192
192
  errCount++
193
193
  expect(e).toBeInstanceOf(Error)
194
194
  expect(e.message).toBe('Circular dependency in effect detected')
@@ -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
  })
@@ -1,5 +1,5 @@
1
- import { TestResult } from './perf-tests'
2
- import { ReactiveFramework } from './reactive-framework'
1
+ import type { TestResult } from './perf-tests'
2
+ import type { ReactiveFramework } from './reactive-framework'
3
3
 
4
4
  /** Parameters for a running a performance benchmark test
5
5
  *
@@ -1,4 +1,4 @@
1
- import { FrameworkInfo, TestConfig } from './framework-types'
1
+ import type { FrameworkInfo, TestConfig } from './framework-types'
2
2
 
3
3
  export interface TestResult {
4
4
  sum: number
@@ -25,7 +25,7 @@ export function verifyBenchResult(
25
25
 
26
26
  if (expected.sum) {
27
27
  console.assert(
28
- result.sum == expected.sum,
28
+ result.sum === expected.sum,
29
29
  `sum ${framework.name} ${config.name} result:${result.sum} expected:${expected.sum}`,
30
30
  )
31
31
  }
@@ -6,7 +6,7 @@ export interface ReactiveFramework {
6
6
  name: string
7
7
  signal<T>(initialValue: T): Signal<T>
8
8
  computed<T>(fn: () => T): Computed<T>
9
- effect(fn: () => void): void
9
+ effect(fn: () => undefined): void
10
10
  withBatch<T>(fn: () => T): void
11
11
  withBuild<T>(fn: () => T): T
12
12
  }