@pyreon/reactivity 0.11.4 → 0.11.6
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/README.md +2 -2
- package/lib/index.js.map +1 -1
- package/package.json +10 -10
- package/src/computed.ts +5 -5
- package/src/createSelector.ts +2 -2
- package/src/debug.ts +8 -8
- package/src/effect.ts +4 -4
- package/src/index.ts +13 -13
- package/src/reconcile.ts +4 -4
- package/src/resource.ts +4 -4
- package/src/scope.ts +1 -1
- package/src/signal.ts +7 -7
- package/src/store.ts +9 -9
- package/src/tests/batch.test.ts +16 -16
- package/src/tests/bind.test.ts +8 -8
- package/src/tests/branches.test.ts +66 -66
- package/src/tests/cell.test.ts +22 -22
- package/src/tests/computed.test.ts +24 -24
- package/src/tests/createSelector.test.ts +17 -17
- package/src/tests/debug.test.ts +29 -29
- package/src/tests/effect.test.ts +59 -59
- package/src/tests/resource.test.ts +39 -39
- package/src/tests/scope.test.ts +20 -20
- package/src/tests/signal.test.ts +39 -39
- package/src/tests/store.test.ts +64 -64
- package/src/tests/tracking.test.ts +17 -17
- package/src/tests/watch.test.ts +32 -32
- package/src/tracking.ts +1 -1
- package/src/watch.ts +3 -3
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { batch } from
|
|
2
|
-
import { effect, renderEffect } from
|
|
3
|
-
import { signal } from
|
|
4
|
-
import { runUntracked } from
|
|
5
|
-
|
|
6
|
-
describe(
|
|
7
|
-
describe(
|
|
8
|
-
test(
|
|
1
|
+
import { batch } from '../batch'
|
|
2
|
+
import { effect, renderEffect } from '../effect'
|
|
3
|
+
import { signal } from '../signal'
|
|
4
|
+
import { runUntracked } from '../tracking'
|
|
5
|
+
|
|
6
|
+
describe('tracking', () => {
|
|
7
|
+
describe('notifySubscribers', () => {
|
|
8
|
+
test('multi-subscriber notification without batching (snapshot path)', () => {
|
|
9
9
|
const s = signal(0)
|
|
10
10
|
let runs1 = 0
|
|
11
11
|
let runs2 = 0
|
|
@@ -28,7 +28,7 @@ describe("tracking", () => {
|
|
|
28
28
|
expect(runs2).toBe(2)
|
|
29
29
|
})
|
|
30
30
|
|
|
31
|
-
test(
|
|
31
|
+
test('multi-subscriber notification during batching', () => {
|
|
32
32
|
const s = signal(0)
|
|
33
33
|
let runs1 = 0
|
|
34
34
|
let runs2 = 0
|
|
@@ -50,7 +50,7 @@ describe("tracking", () => {
|
|
|
50
50
|
expect(runs2).toBe(2)
|
|
51
51
|
})
|
|
52
52
|
|
|
53
|
-
test(
|
|
53
|
+
test('single subscriber batching path', () => {
|
|
54
54
|
const s = signal(0)
|
|
55
55
|
let runs = 0
|
|
56
56
|
|
|
@@ -67,8 +67,8 @@ describe("tracking", () => {
|
|
|
67
67
|
})
|
|
68
68
|
})
|
|
69
69
|
|
|
70
|
-
describe(
|
|
71
|
-
test(
|
|
70
|
+
describe('runUntracked', () => {
|
|
71
|
+
test('signal reads inside runUntracked do not create dependencies', () => {
|
|
72
72
|
const s = signal(0)
|
|
73
73
|
let runs = 0
|
|
74
74
|
|
|
@@ -82,7 +82,7 @@ describe("tracking", () => {
|
|
|
82
82
|
expect(runs).toBe(1) // not re-run
|
|
83
83
|
})
|
|
84
84
|
|
|
85
|
-
test(
|
|
85
|
+
test('restores tracking context after runUntracked', () => {
|
|
86
86
|
const tracked = signal(0)
|
|
87
87
|
const untracked = signal(0)
|
|
88
88
|
let runs = 0
|
|
@@ -105,8 +105,8 @@ describe("tracking", () => {
|
|
|
105
105
|
})
|
|
106
106
|
})
|
|
107
107
|
|
|
108
|
-
describe(
|
|
109
|
-
test(
|
|
108
|
+
describe('trackSubscriber with depsCollector', () => {
|
|
109
|
+
test('renderEffect uses fast deps collector path', () => {
|
|
110
110
|
const s = signal(0)
|
|
111
111
|
let runs = 0
|
|
112
112
|
|
|
@@ -124,8 +124,8 @@ describe("tracking", () => {
|
|
|
124
124
|
})
|
|
125
125
|
})
|
|
126
126
|
|
|
127
|
-
describe(
|
|
128
|
-
test(
|
|
127
|
+
describe('cleanupEffect', () => {
|
|
128
|
+
test('effect dynamically tracks/untracks deps on re-run', () => {
|
|
129
129
|
const cond = signal(true)
|
|
130
130
|
const a = signal(0)
|
|
131
131
|
const b = signal(0)
|
package/src/tests/watch.test.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { signal } from
|
|
2
|
-
import { watch } from
|
|
1
|
+
import { signal } from '../signal'
|
|
2
|
+
import { watch } from '../watch'
|
|
3
3
|
|
|
4
|
-
describe(
|
|
5
|
-
test(
|
|
4
|
+
describe('watch', () => {
|
|
5
|
+
test('calls callback when source changes', () => {
|
|
6
6
|
const s = signal(1)
|
|
7
7
|
const calls: [number, number | undefined][] = []
|
|
8
8
|
|
|
@@ -25,7 +25,7 @@ describe("watch", () => {
|
|
|
25
25
|
])
|
|
26
26
|
})
|
|
27
27
|
|
|
28
|
-
test(
|
|
28
|
+
test('immediate option calls callback on first run', () => {
|
|
29
29
|
const s = signal(1)
|
|
30
30
|
const calls: [number, number | undefined][] = []
|
|
31
31
|
|
|
@@ -40,7 +40,7 @@ describe("watch", () => {
|
|
|
40
40
|
expect(calls).toEqual([[1, undefined]])
|
|
41
41
|
})
|
|
42
42
|
|
|
43
|
-
test(
|
|
43
|
+
test('stop function disposes the watcher', () => {
|
|
44
44
|
const s = signal(1)
|
|
45
45
|
let callCount = 0
|
|
46
46
|
|
|
@@ -60,7 +60,7 @@ describe("watch", () => {
|
|
|
60
60
|
expect(callCount).toBe(1) // no more calls
|
|
61
61
|
})
|
|
62
62
|
|
|
63
|
-
test(
|
|
63
|
+
test('cleanup function is called before each re-run', () => {
|
|
64
64
|
const s = signal(1)
|
|
65
65
|
const log: string[] = []
|
|
66
66
|
|
|
@@ -73,13 +73,13 @@ describe("watch", () => {
|
|
|
73
73
|
)
|
|
74
74
|
|
|
75
75
|
s.set(2)
|
|
76
|
-
expect(log).toEqual([
|
|
76
|
+
expect(log).toEqual(['run-2'])
|
|
77
77
|
|
|
78
78
|
s.set(3)
|
|
79
|
-
expect(log).toEqual([
|
|
79
|
+
expect(log).toEqual(['run-2', 'cleanup-2', 'run-3'])
|
|
80
80
|
})
|
|
81
81
|
|
|
82
|
-
test(
|
|
82
|
+
test('cleanup function from immediate is called on next change', () => {
|
|
83
83
|
const s = signal(1)
|
|
84
84
|
const log: string[] = []
|
|
85
85
|
|
|
@@ -92,13 +92,13 @@ describe("watch", () => {
|
|
|
92
92
|
{ immediate: true },
|
|
93
93
|
)
|
|
94
94
|
|
|
95
|
-
expect(log).toEqual([
|
|
95
|
+
expect(log).toEqual(['run-1'])
|
|
96
96
|
|
|
97
97
|
s.set(2)
|
|
98
|
-
expect(log).toEqual([
|
|
98
|
+
expect(log).toEqual(['run-1', 'cleanup-1', 'run-2'])
|
|
99
99
|
})
|
|
100
100
|
|
|
101
|
-
test(
|
|
101
|
+
test('cleanup is called on stop', () => {
|
|
102
102
|
const s = signal(1)
|
|
103
103
|
const log: string[] = []
|
|
104
104
|
|
|
@@ -111,13 +111,13 @@ describe("watch", () => {
|
|
|
111
111
|
)
|
|
112
112
|
|
|
113
113
|
s.set(2)
|
|
114
|
-
expect(log).toEqual([
|
|
114
|
+
expect(log).toEqual(['run-2'])
|
|
115
115
|
|
|
116
116
|
stop()
|
|
117
|
-
expect(log).toEqual([
|
|
117
|
+
expect(log).toEqual(['run-2', 'cleanup-2'])
|
|
118
118
|
})
|
|
119
119
|
|
|
120
|
-
test(
|
|
120
|
+
test('callback returning non-function does not set cleanup', () => {
|
|
121
121
|
const s = signal(1)
|
|
122
122
|
let callCount = 0
|
|
123
123
|
|
|
@@ -134,7 +134,7 @@ describe("watch", () => {
|
|
|
134
134
|
expect(callCount).toBe(2)
|
|
135
135
|
})
|
|
136
136
|
|
|
137
|
-
test(
|
|
137
|
+
test('stop without cleanup does not throw', () => {
|
|
138
138
|
const s = signal(1)
|
|
139
139
|
const stop = watch(
|
|
140
140
|
() => s(),
|
|
@@ -144,8 +144,8 @@ describe("watch", () => {
|
|
|
144
144
|
stop() // no cleanup function was set, should not throw
|
|
145
145
|
})
|
|
146
146
|
|
|
147
|
-
test(
|
|
148
|
-
const s = signal(
|
|
147
|
+
test('oldValue tracks previous value across multiple changes', () => {
|
|
148
|
+
const s = signal('a')
|
|
149
149
|
const history: [string, string | undefined][] = []
|
|
150
150
|
|
|
151
151
|
watch(
|
|
@@ -155,18 +155,18 @@ describe("watch", () => {
|
|
|
155
155
|
},
|
|
156
156
|
)
|
|
157
157
|
|
|
158
|
-
s.set(
|
|
159
|
-
s.set(
|
|
160
|
-
s.set(
|
|
158
|
+
s.set('b')
|
|
159
|
+
s.set('c')
|
|
160
|
+
s.set('d')
|
|
161
161
|
|
|
162
162
|
expect(history).toEqual([
|
|
163
|
-
[
|
|
164
|
-
[
|
|
165
|
-
[
|
|
163
|
+
['b', 'a'],
|
|
164
|
+
['c', 'b'],
|
|
165
|
+
['d', 'c'],
|
|
166
166
|
])
|
|
167
167
|
})
|
|
168
168
|
|
|
169
|
-
test(
|
|
169
|
+
test('oldValue is undefined on immediate first call', () => {
|
|
170
170
|
const s = signal(42)
|
|
171
171
|
let receivedOld: number | undefined = -1
|
|
172
172
|
|
|
@@ -181,7 +181,7 @@ describe("watch", () => {
|
|
|
181
181
|
expect(receivedOld).toBeUndefined()
|
|
182
182
|
})
|
|
183
183
|
|
|
184
|
-
test(
|
|
184
|
+
test('watch with derived source (computed-like)', () => {
|
|
185
185
|
const a = signal(1)
|
|
186
186
|
const b = signal(10)
|
|
187
187
|
const calls: [number, number | undefined][] = []
|
|
@@ -203,7 +203,7 @@ describe("watch", () => {
|
|
|
203
203
|
])
|
|
204
204
|
})
|
|
205
205
|
|
|
206
|
-
test(
|
|
206
|
+
test('stop prevents cleanup from running on future changes', () => {
|
|
207
207
|
const s = signal(1)
|
|
208
208
|
const log: string[] = []
|
|
209
209
|
|
|
@@ -216,17 +216,17 @@ describe("watch", () => {
|
|
|
216
216
|
)
|
|
217
217
|
|
|
218
218
|
s.set(2)
|
|
219
|
-
expect(log).toEqual([
|
|
219
|
+
expect(log).toEqual(['run-2'])
|
|
220
220
|
|
|
221
221
|
stop()
|
|
222
|
-
expect(log).toEqual([
|
|
222
|
+
expect(log).toEqual(['run-2', 'cleanup-2'])
|
|
223
223
|
|
|
224
224
|
// Further changes should not trigger anything
|
|
225
225
|
s.set(3)
|
|
226
|
-
expect(log).toEqual([
|
|
226
|
+
expect(log).toEqual(['run-2', 'cleanup-2'])
|
|
227
227
|
})
|
|
228
228
|
|
|
229
|
-
test(
|
|
229
|
+
test('watch does not fire when value stays the same', () => {
|
|
230
230
|
const s = signal(1)
|
|
231
231
|
let callCount = 0
|
|
232
232
|
|
package/src/tracking.ts
CHANGED
package/src/watch.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { effect } from
|
|
1
|
+
import { effect } from './effect'
|
|
2
2
|
|
|
3
3
|
export interface WatchOptions {
|
|
4
4
|
/** If true, call the callback immediately with the current value on setup. Default: false. */
|
|
@@ -44,7 +44,7 @@ export function watch<T>(
|
|
|
44
44
|
oldVal = newVal
|
|
45
45
|
if (opts.immediate) {
|
|
46
46
|
const result = callback(newVal, undefined)
|
|
47
|
-
if (typeof result ===
|
|
47
|
+
if (typeof result === 'function') cleanupFn = result
|
|
48
48
|
}
|
|
49
49
|
return
|
|
50
50
|
}
|
|
@@ -55,7 +55,7 @@ export function watch<T>(
|
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
const result = callback(newVal, oldVal)
|
|
58
|
-
if (typeof result ===
|
|
58
|
+
if (typeof result === 'function') cleanupFn = result
|
|
59
59
|
oldVal = newVal
|
|
60
60
|
})
|
|
61
61
|
|