@tldraw/state 5.1.1 → 5.2.0-canary.0878dbd31f0d

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,197 +0,0 @@
1
- import { vi } from 'vitest'
2
- import { atom } from '../Atom'
3
- import { react, reactor } from '../EffectScheduler'
4
- import { advanceGlobalEpoch, transact } from '../transactions'
5
-
6
- describe('reactors', () => {
7
- it('can be started and stopped ', () => {
8
- const a = atom('', 1)
9
- const r = reactor('', () => {
10
- a.get()
11
- })
12
- expect(r.scheduler.isActivelyListening).toBe(false)
13
- r.start()
14
- expect(r.scheduler.isActivelyListening).toBe(true)
15
- r.stop()
16
- expect(r.scheduler.isActivelyListening).toBe(false)
17
- r.start()
18
- expect(r.scheduler.isActivelyListening).toBe(true)
19
- })
20
-
21
- it('can not set atom values indefinitely', () => {
22
- const a = atom('', 1)
23
- const r = reactor('', () => {
24
- if (a.get() < +Infinity) {
25
- a.update((a) => a + 1)
26
- }
27
- })
28
- expect(() => r.start()).toThrowErrorMatchingInlineSnapshot(
29
- `[Error: Reaction update depth limit exceeded]`
30
- )
31
- })
32
-
33
- it('will never be called twice after a single state update, even if that update affects multiple atoms to which the reactor is subscribed', () => {
34
- const atomA = atom('', 1)
35
- const atomB = atom('', 1)
36
-
37
- const react = vi.fn(() => {
38
- atomA.get()
39
- atomB.get()
40
- })
41
- const r = reactor('', react)
42
-
43
- r.start()
44
- expect(react).toHaveBeenCalledTimes(1)
45
-
46
- transact(() => {
47
- atomA.set(2)
48
- atomB.set(2)
49
- })
50
-
51
- expect(react).toHaveBeenCalledTimes(2)
52
- })
53
-
54
- it('will not react if stopped', () => {
55
- const a = atom('', 1)
56
- const react = vi.fn(() => {
57
- a.get()
58
- })
59
- const r = reactor('', react)
60
-
61
- r.scheduler.maybeScheduleEffect()
62
-
63
- expect(react).not.toHaveBeenCalled()
64
- })
65
-
66
- it('will not react if the parents have not changed', () => {
67
- const a = atom('', 1)
68
- const react = vi
69
- .fn(() => {
70
- a.get()
71
- })
72
- .mockName('react')
73
- const r = reactor('', react)
74
-
75
- r.start()
76
- expect(react).toHaveBeenCalledTimes(1)
77
-
78
- advanceGlobalEpoch()
79
- r.scheduler.maybeScheduleEffect()
80
- expect(react).toHaveBeenCalledTimes(1)
81
- })
82
- })
83
-
84
- describe('stopping', () => {
85
- it('works', () => {
86
- const a = atom('', 1)
87
-
88
- const rfn = vi.fn(() => {
89
- a.get()
90
- })
91
- const r = reactor('', rfn)
92
-
93
- expect(a.children.isEmpty).toBe(true)
94
-
95
- r.start()
96
-
97
- expect(a.children.isEmpty).toBe(false)
98
-
99
- a.set(8)
100
-
101
- expect(rfn).toHaveBeenCalledTimes(2)
102
-
103
- r.stop()
104
-
105
- expect(a.children.isEmpty).toBe(true)
106
- expect(rfn).toHaveBeenCalledTimes(2)
107
-
108
- a.set(2)
109
-
110
- expect(rfn).toHaveBeenCalledTimes(2)
111
-
112
- a.set(3)
113
-
114
- expect(rfn).toHaveBeenCalledTimes(2)
115
-
116
- expect(a.children.isEmpty).toBe(true)
117
- })
118
- })
119
-
120
- test('.start() by default does not trigger a reaction if nothing has changed', () => {
121
- const a = atom('', 1)
122
-
123
- const rfn = vi.fn(() => {
124
- a.get()
125
- })
126
-
127
- const r = reactor('', rfn)
128
- r.start()
129
-
130
- expect(rfn).toHaveBeenCalledTimes(1)
131
-
132
- r.stop()
133
-
134
- r.start()
135
-
136
- expect(rfn).toHaveBeenCalledTimes(1)
137
- })
138
-
139
- test('.start({force: true}) will trigger a reaction even if nothing has changed', () => {
140
- const a = atom('', 1)
141
-
142
- const rfn = vi.fn(() => {
143
- a.get()
144
- })
145
-
146
- const r = reactor('', rfn)
147
- r.start()
148
-
149
- expect(rfn).toHaveBeenCalledTimes(1)
150
-
151
- r.stop()
152
-
153
- r.start({ force: true })
154
-
155
- expect(rfn).toHaveBeenCalledTimes(2)
156
- })
157
-
158
- test('.start with a custom scheduler only schedules an effect, it does not execute it immediately', () => {
159
- let numSchedules = 0
160
- let numExecutes = 0
161
-
162
- const r = reactor(
163
- '',
164
- () => {
165
- numExecutes++
166
- },
167
- {
168
- scheduleEffect: () => {
169
- numSchedules++
170
- },
171
- }
172
- )
173
- r.start()
174
-
175
- expect(numSchedules).toBe(1)
176
- expect(numExecutes).toBe(0)
177
- })
178
-
179
- test('react() with a custom scheduler only schedules an effect, it does not execute it immediately', () => {
180
- let numSchedules = 0
181
- let numExecutes = 0
182
-
183
- react(
184
- '',
185
- () => {
186
- numExecutes++
187
- },
188
- {
189
- scheduleEffect: () => {
190
- numSchedules++
191
- },
192
- }
193
- )
194
-
195
- expect(numSchedules).toBe(1)
196
- expect(numExecutes).toBe(0)
197
- })