@tldraw/state 3.16.0-next.282b7be564ae → 3.16.0-next.4337ae1ab96d
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/dist-cjs/index.js
CHANGED
package/dist-esm/index.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tldraw/state",
|
|
3
3
|
"description": "tldraw infinite canvas SDK (state).",
|
|
4
|
-
"version": "3.16.0-next.
|
|
4
|
+
"version": "3.16.0-next.4337ae1ab96d",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "tldraw Inc.",
|
|
7
7
|
"email": "hello@tldraw.com"
|
|
@@ -32,31 +32,24 @@
|
|
|
32
32
|
"src"
|
|
33
33
|
],
|
|
34
34
|
"scripts": {
|
|
35
|
-
"test-ci": "
|
|
36
|
-
"test": "yarn run -T
|
|
37
|
-
"test-coverage": "
|
|
35
|
+
"test-ci": "yarn run -T vitest run --passWithNoTests",
|
|
36
|
+
"test": "yarn run -T vitest --passWithNoTests",
|
|
37
|
+
"test-coverage": "yarn run -T vitest run --coverage --passWithNoTests",
|
|
38
38
|
"build": "yarn run -T tsx ../../internal/scripts/build-package.ts",
|
|
39
39
|
"build-api": "yarn run -T tsx ../../internal/scripts/build-api.ts",
|
|
40
40
|
"prepack": "yarn run -T tsx ../../internal/scripts/prepack.ts",
|
|
41
41
|
"postpack": "../../internal/scripts/postpack.sh",
|
|
42
42
|
"pack-tarball": "yarn pack",
|
|
43
|
-
"lint": "yarn run -T tsx ../../internal/scripts/lint.ts"
|
|
44
|
-
|
|
45
|
-
"jest": {
|
|
46
|
-
"preset": "../../internal/config/jest/node/jest-preset.js",
|
|
47
|
-
"setupFiles": [
|
|
48
|
-
"raf/polyfill"
|
|
49
|
-
],
|
|
50
|
-
"moduleNameMapper": {
|
|
51
|
-
"^~(.*)": "<rootDir>/src/$1"
|
|
52
|
-
}
|
|
43
|
+
"lint": "yarn run -T tsx ../../internal/scripts/lint.ts",
|
|
44
|
+
"context": "yarn run -T tsx ../../internal/scripts/context.ts"
|
|
53
45
|
},
|
|
54
46
|
"devDependencies": {
|
|
55
47
|
"@types/lodash": "^4.17.14",
|
|
56
|
-
"lodash": "^4.17.21"
|
|
48
|
+
"lodash": "^4.17.21",
|
|
49
|
+
"vitest": "^3.2.4"
|
|
57
50
|
},
|
|
58
51
|
"dependencies": {
|
|
59
|
-
"@tldraw/utils": "3.16.0-next.
|
|
52
|
+
"@tldraw/utils": "3.16.0-next.4337ae1ab96d"
|
|
60
53
|
},
|
|
61
54
|
"typedoc": {
|
|
62
55
|
"readmeFile": "none",
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { vi } from 'vitest'
|
|
1
2
|
import { atom } from '../Atom'
|
|
2
3
|
import { Computed, _Computed, computed, getComputedInstance, isUninitialized } from '../Computed'
|
|
3
4
|
import { reactor } from '../EffectScheduler'
|
|
@@ -11,7 +12,7 @@ function getLastCheckedEpoch(derivation: Computed<any>): number {
|
|
|
11
12
|
|
|
12
13
|
describe('derivations', () => {
|
|
13
14
|
it('will cache a value forever if it has no parents', () => {
|
|
14
|
-
const derive =
|
|
15
|
+
const derive = vi.fn(() => 1)
|
|
15
16
|
const startEpoch = getGlobalEpoch()
|
|
16
17
|
const derivation = computed('', derive)
|
|
17
18
|
|
|
@@ -45,7 +46,7 @@ describe('derivations', () => {
|
|
|
45
46
|
|
|
46
47
|
it('will update when parent atoms update', () => {
|
|
47
48
|
const a = atom('', 1)
|
|
48
|
-
const double =
|
|
49
|
+
const double = vi.fn(() => a.get() * 2)
|
|
49
50
|
const derivation = computed('', double)
|
|
50
51
|
const startEpoch = getGlobalEpoch()
|
|
51
52
|
expect(double).toHaveBeenCalledTimes(0)
|
|
@@ -122,7 +123,7 @@ describe('derivations', () => {
|
|
|
122
123
|
const startEpoch = getGlobalEpoch()
|
|
123
124
|
const a = atom('', 1)
|
|
124
125
|
|
|
125
|
-
const floor =
|
|
126
|
+
const floor = vi.fn((n: number) => Math.floor(n))
|
|
126
127
|
const derivation = computed('', () => floor(a.get()), {
|
|
127
128
|
historyLength: 3,
|
|
128
129
|
computeDiff: (a, b) => {
|
|
@@ -162,7 +163,7 @@ describe('derivations', () => {
|
|
|
162
163
|
const startEpoch = getGlobalEpoch()
|
|
163
164
|
const a = atom('', 1)
|
|
164
165
|
|
|
165
|
-
const double =
|
|
166
|
+
const double = vi.fn(() => a.get() * 2)
|
|
166
167
|
const derivation = computed('', double)
|
|
167
168
|
|
|
168
169
|
derivation.get()
|
|
@@ -179,7 +180,7 @@ describe('derivations', () => {
|
|
|
179
180
|
|
|
180
181
|
it('receives UNINTIALIZED as the previousValue the first time it computes', () => {
|
|
181
182
|
const a = atom('', 1)
|
|
182
|
-
const double =
|
|
183
|
+
const double = vi.fn((_prevValue) => a.get() * 2)
|
|
183
184
|
const derivation = computed('', double)
|
|
184
185
|
|
|
185
186
|
expect(derivation.get()).toBe(2)
|
|
@@ -195,7 +196,7 @@ describe('derivations', () => {
|
|
|
195
196
|
|
|
196
197
|
it('receives the lastChangedEpoch as the second parameter each time it recomputes', () => {
|
|
197
198
|
const a = atom('', 1)
|
|
198
|
-
const double =
|
|
199
|
+
const double = vi.fn((_prevValue, lastChangedEpoch) => {
|
|
199
200
|
expect(lastChangedEpoch).toBe(derivation.lastChangedEpoch)
|
|
200
201
|
return a.get() * 2
|
|
201
202
|
})
|
|
@@ -438,7 +439,7 @@ describe('incremental derivations', () => {
|
|
|
438
439
|
}
|
|
439
440
|
)
|
|
440
441
|
|
|
441
|
-
const mapper =
|
|
442
|
+
const mapper = vi.fn((val) => val * 2)
|
|
442
443
|
|
|
443
444
|
const doubledNodes = getIncrementalRecordMapper(nodes, mapper)
|
|
444
445
|
|
|
@@ -590,7 +591,7 @@ describe(getComputedInstance, () => {
|
|
|
590
591
|
|
|
591
592
|
describe('computed isEqual', () => {
|
|
592
593
|
it('does not get called for the initialization', () => {
|
|
593
|
-
const isEqual =
|
|
594
|
+
const isEqual = vi.fn((a, b) => a === b)
|
|
594
595
|
|
|
595
596
|
const a = atom('a', 1)
|
|
596
597
|
const b = computed('b', () => a.get() * 2, { isEqual })
|
|
@@ -11,7 +11,7 @@ describe('reactors that error', () => {
|
|
|
11
11
|
react('', () => {
|
|
12
12
|
if (a.get() === 2) throw new Error('test')
|
|
13
13
|
})
|
|
14
|
-
expect(() => a.set(2)).toThrowErrorMatchingInlineSnapshot(`
|
|
14
|
+
expect(() => a.set(2)).toThrowErrorMatchingInlineSnapshot(`[Error: test]`)
|
|
15
15
|
expect(a.get()).toBe(2)
|
|
16
16
|
})
|
|
17
17
|
it('will not roll back the changes in a transaction', () => {
|
|
@@ -27,7 +27,7 @@ describe('reactors that error', () => {
|
|
|
27
27
|
a.set(3)
|
|
28
28
|
b.set(1)
|
|
29
29
|
})
|
|
30
|
-
).toThrowErrorMatchingInlineSnapshot(`
|
|
30
|
+
).toThrowErrorMatchingInlineSnapshot(`[Error: test]`)
|
|
31
31
|
|
|
32
32
|
expect(a.get()).toBe(3)
|
|
33
33
|
expect(b.get()).toBe(1)
|
|
@@ -49,11 +49,11 @@ describe('derivations that error', () => {
|
|
|
49
49
|
|
|
50
50
|
a.set(2)
|
|
51
51
|
|
|
52
|
-
expect(() => b.get()).toThrowErrorMatchingInlineSnapshot(`
|
|
52
|
+
expect(() => b.get()).toThrowErrorMatchingInlineSnapshot(`[Error: test]`)
|
|
53
53
|
expect(numComputations).toBe(2)
|
|
54
|
-
expect(() => b.get()).toThrowErrorMatchingInlineSnapshot(`
|
|
54
|
+
expect(() => b.get()).toThrowErrorMatchingInlineSnapshot(`[Error: test]`)
|
|
55
55
|
expect(numComputations).toBe(2)
|
|
56
|
-
expect(() => b.get()).toThrowErrorMatchingInlineSnapshot(`
|
|
56
|
+
expect(() => b.get()).toThrowErrorMatchingInlineSnapshot(`[Error: test]`)
|
|
57
57
|
expect(numComputations).toBe(2)
|
|
58
58
|
|
|
59
59
|
a.set(3)
|
|
@@ -128,7 +128,7 @@ describe('derivations that error', () => {
|
|
|
128
128
|
|
|
129
129
|
a.set(5)
|
|
130
130
|
|
|
131
|
-
expect(() => b.get()).toThrowErrorMatchingInlineSnapshot(`
|
|
131
|
+
expect(() => b.get()).toThrowErrorMatchingInlineSnapshot(`[Error: test]`)
|
|
132
132
|
expect(b.getDiffSince(startEpoch)).toEqual(RESET_VALUE)
|
|
133
133
|
const errorEpoch = getGlobalEpoch()
|
|
134
134
|
|
|
@@ -153,11 +153,11 @@ test('haveParentsChanged will not throw if one of the parents is throwing', () =
|
|
|
153
153
|
expect(() => {
|
|
154
154
|
scheduler.attach()
|
|
155
155
|
scheduler.execute()
|
|
156
|
-
}).toThrowErrorMatchingInlineSnapshot(`
|
|
156
|
+
}).toThrowErrorMatchingInlineSnapshot(`[Error: test]`)
|
|
157
157
|
|
|
158
158
|
expect(haveParentsChanged(scheduler)).toBe(false)
|
|
159
159
|
|
|
160
|
-
expect(() => a.set(2)).toThrowErrorMatchingInlineSnapshot(`
|
|
160
|
+
expect(() => a.set(2)).toThrowErrorMatchingInlineSnapshot(`[Error: test]`)
|
|
161
161
|
|
|
162
162
|
// haveParentsChanged should still be false because it already
|
|
163
163
|
// executed the effect and it errored
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { vi } from 'vitest'
|
|
1
2
|
import { atom } from '../Atom'
|
|
2
3
|
import { react, reactor } from '../EffectScheduler'
|
|
3
4
|
import { advanceGlobalEpoch, transact } from '../transactions'
|
|
@@ -25,7 +26,7 @@ describe('reactors', () => {
|
|
|
25
26
|
}
|
|
26
27
|
})
|
|
27
28
|
expect(() => r.start()).toThrowErrorMatchingInlineSnapshot(
|
|
28
|
-
`
|
|
29
|
+
`[Error: Reaction update depth limit exceeded]`
|
|
29
30
|
)
|
|
30
31
|
})
|
|
31
32
|
|
|
@@ -33,7 +34,7 @@ describe('reactors', () => {
|
|
|
33
34
|
const atomA = atom('', 1)
|
|
34
35
|
const atomB = atom('', 1)
|
|
35
36
|
|
|
36
|
-
const react =
|
|
37
|
+
const react = vi.fn(() => {
|
|
37
38
|
atomA.get()
|
|
38
39
|
atomB.get()
|
|
39
40
|
})
|
|
@@ -52,7 +53,7 @@ describe('reactors', () => {
|
|
|
52
53
|
|
|
53
54
|
it('will not react if stopped', () => {
|
|
54
55
|
const a = atom('', 1)
|
|
55
|
-
const react =
|
|
56
|
+
const react = vi.fn(() => {
|
|
56
57
|
a.get()
|
|
57
58
|
})
|
|
58
59
|
const r = reactor('', react)
|
|
@@ -64,7 +65,7 @@ describe('reactors', () => {
|
|
|
64
65
|
|
|
65
66
|
it('will not react if the parents have not changed', () => {
|
|
66
67
|
const a = atom('', 1)
|
|
67
|
-
const react =
|
|
68
|
+
const react = vi
|
|
68
69
|
.fn(() => {
|
|
69
70
|
a.get()
|
|
70
71
|
})
|
|
@@ -84,7 +85,7 @@ describe('stopping', () => {
|
|
|
84
85
|
it('works', () => {
|
|
85
86
|
const a = atom('', 1)
|
|
86
87
|
|
|
87
|
-
const rfn =
|
|
88
|
+
const rfn = vi.fn(() => {
|
|
88
89
|
a.get()
|
|
89
90
|
})
|
|
90
91
|
const r = reactor('', rfn)
|
|
@@ -119,7 +120,7 @@ describe('stopping', () => {
|
|
|
119
120
|
test('.start() by default does not trigger a reaction if nothing has changed', () => {
|
|
120
121
|
const a = atom('', 1)
|
|
121
122
|
|
|
122
|
-
const rfn =
|
|
123
|
+
const rfn = vi.fn(() => {
|
|
123
124
|
a.get()
|
|
124
125
|
})
|
|
125
126
|
|
|
@@ -138,7 +139,7 @@ test('.start() by default does not trigger a reaction if nothing has changed', (
|
|
|
138
139
|
test('.start({force: true}) will trigger a reaction even if nothing has changed', () => {
|
|
139
140
|
const a = atom('', 1)
|
|
140
141
|
|
|
141
|
-
const rfn =
|
|
142
|
+
const rfn = vi.fn(() => {
|
|
142
143
|
a.get()
|
|
143
144
|
})
|
|
144
145
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { promiseWithResolve, sleep } from '@tldraw/utils'
|
|
2
|
+
import { vi } from 'vitest'
|
|
2
3
|
import { atom } from '../Atom'
|
|
3
4
|
import { computed } from '../Computed'
|
|
4
5
|
import { react } from '../EffectScheduler'
|
|
@@ -222,7 +223,7 @@ describe('setting atoms during a reaction', () => {
|
|
|
222
223
|
react('', () => {
|
|
223
224
|
a.set(a.get() + 1)
|
|
224
225
|
})
|
|
225
|
-
}).toThrowErrorMatchingInlineSnapshot(`
|
|
226
|
+
}).toThrowErrorMatchingInlineSnapshot(`[Error: Reaction update depth limit exceeded]`)
|
|
226
227
|
})
|
|
227
228
|
|
|
228
229
|
it('should work with a transaction running', () => {
|
|
@@ -360,9 +361,9 @@ it('should defer all side effects until the end of the outer transaction', () =>
|
|
|
360
361
|
const b = atom('', 0)
|
|
361
362
|
const c = atom('', 0)
|
|
362
363
|
|
|
363
|
-
const aChanged =
|
|
364
|
-
const bChanged =
|
|
365
|
-
const cChanged =
|
|
364
|
+
const aChanged = vi.fn()
|
|
365
|
+
const bChanged = vi.fn()
|
|
366
|
+
const cChanged = vi.fn()
|
|
366
367
|
|
|
367
368
|
react('', () => {
|
|
368
369
|
a.get()
|
|
@@ -442,7 +443,7 @@ describe('asyncTransaction', () => {
|
|
|
442
443
|
a.set(1)
|
|
443
444
|
})
|
|
444
445
|
|
|
445
|
-
expect(txp).rejects.toMatchInlineSnapshot(
|
|
446
|
+
await expect(txp).rejects.toMatchInlineSnapshot(
|
|
446
447
|
`[Error: deferAsyncEffects cannot be called during a sync transaction]`
|
|
447
448
|
)
|
|
448
449
|
})
|
|
@@ -536,7 +537,7 @@ describe('async tests generated by claude', () => {
|
|
|
536
537
|
it('should defer effects until async transaction commits', async () => {
|
|
537
538
|
const a = atom('', 0)
|
|
538
539
|
const b = atom('', 0)
|
|
539
|
-
const effectCalls =
|
|
540
|
+
const effectCalls = vi.fn()
|
|
540
541
|
|
|
541
542
|
react('', () => {
|
|
542
543
|
a.get()
|