@tldraw/state 4.6.0-internal.c7df3c92455a → 4.6.0-next.0eb36d65eec3
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 +1 -1
- package/dist-cjs/lib/ArraySet.js +1 -1
- package/dist-cjs/lib/ArraySet.js.map +1 -1
- package/dist-cjs/lib/Atom.js +1 -1
- package/dist-cjs/lib/Atom.js.map +2 -2
- package/dist-cjs/lib/Computed.js +7 -7
- package/dist-cjs/lib/Computed.js.map +2 -2
- package/dist-cjs/lib/EffectScheduler.js +3 -3
- package/dist-cjs/lib/EffectScheduler.js.map +1 -1
- package/dist-cjs/lib/helpers.js +1 -1
- package/dist-cjs/lib/helpers.js.map +1 -1
- package/dist-cjs/lib/transactions.js +1 -1
- package/dist-cjs/lib/transactions.js.map +1 -1
- package/dist-cjs/lib/types.js +1 -1
- package/dist-cjs/lib/types.js.map +1 -1
- package/dist-esm/index.mjs +1 -1
- package/dist-esm/lib/ArraySet.mjs +1 -1
- package/dist-esm/lib/ArraySet.mjs.map +1 -1
- package/dist-esm/lib/Atom.mjs +1 -1
- package/dist-esm/lib/Atom.mjs.map +2 -2
- package/dist-esm/lib/Computed.mjs +7 -7
- package/dist-esm/lib/Computed.mjs.map +2 -2
- package/dist-esm/lib/EffectScheduler.mjs +3 -3
- package/dist-esm/lib/EffectScheduler.mjs.map +1 -1
- package/dist-esm/lib/helpers.mjs +1 -1
- package/dist-esm/lib/helpers.mjs.map +1 -1
- package/dist-esm/lib/transactions.mjs +1 -1
- package/dist-esm/lib/transactions.mjs.map +1 -1
- package/dist-esm/lib/types.mjs +1 -1
- package/dist-esm/lib/types.mjs.map +1 -1
- package/package.json +3 -3
- package/src/lib/ArraySet.ts +1 -1
- package/src/lib/Atom.ts +1 -1
- package/src/lib/Computed.ts +2 -2
- package/src/lib/EffectScheduler.ts +4 -4
- package/src/lib/__tests__/capture.test.ts +2 -2
- package/src/lib/__tests__/fuzz.tlstate.test.ts +2 -2
- package/src/lib/transactions.ts +1 -1
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/lib/types.ts"],
|
|
4
4
|
"sourcesContent": ["import { ArraySet } from './ArraySet'\n\n/**\n * A unique symbol used to indicate that a signal's value should be reset or that\n * there is insufficient history to compute diffs between epochs.\n *\n * This value is returned by {@link Signal.getDiffSince} when the requested epoch\n * is too far in the past and the diff sequence cannot be reconstructed.\n *\n * @example\n * ```ts\n * import { atom, getGlobalEpoch, RESET_VALUE } from '@tldraw/state'\n *\n * const count = atom('count', 0, { historyLength: 3 })\n * const oldEpoch = getGlobalEpoch()\n *\n * // Make many changes that exceed history length\n * count.set(1)\n * count.set(2)\n * count.set(3)\n * count.set(4)\n *\n * const diffs = count.getDiffSince(oldEpoch)\n * if (diffs === RESET_VALUE) {\n * console.log('Too many changes, need to reset state')\n * }\n * ```\n *\n * @public\n */\nexport const RESET_VALUE: unique symbol = Symbol.for('com.tldraw.state/RESET_VALUE')\n\n/**\n * Type representing the the unique symbol RESET_VALUE symbol, used in type annotations\n * to indicate when a signal value should be reset or when diff computation\n * cannot proceed due to insufficient history.\n *\n * @public\n */\nexport type RESET_VALUE = typeof RESET_VALUE\n\n/**\n * A reactive value container that can change over time and track diffs between sequential values.\n *\n * Signals are the foundation of the \\@tldraw/state reactive system. They automatically manage\n * dependencies and trigger updates when their values change. Any computed signal or effect\n * that reads from this signal will be automatically recomputed when the signal's value changes.\n *\n * There are two types of signal:\n * - **Atomic signals** - Created using `atom()`. These are mutable containers that can be\n * directly updated using `set()` or `update()` methods.\n * - **Computed signals** - Created using `computed()`. These derive their values from other\n * signals and are automatically recomputed when dependencies change.\n *\n * @example\n * ```ts\n * import { atom, computed } from '@tldraw/state'\n *\n * // Create an atomic signal\n * const count = atom('count', 0)\n *\n * // Create a computed signal that derives from the atom\n * const doubled = computed('doubled', () => count.get() * 2)\n *\n * console.log(doubled.get()) // 0\n * count.set(5)\n * console.log(doubled.get()) // 10\n * ```\n *\n * @public\n */\nexport interface Signal<Value, Diff = unknown> {\n\t/**\n\t * A human-readable identifier for this signal, used primarily for debugging and performance profiling.\n\t *\n\t * The name is displayed in debug output from {@link whyAmIRunning} and other diagnostic tools.\n\t * It does not need to be globally unique within your application.\n\t */\n\tname: string\n\t/**\n\t * Gets the current value of the signal and establishes a dependency relationship.\n\t *\n\t * When called from within a computed signal or effect, this signal will be automatically\n\t * tracked as a dependency. If this signal's value changes, any dependent computations\n\t * or effects will be marked for re-execution.\n\t *\n\t * @returns The current value stored in the signal\n\t */\n\tget(): Value\n\n\t/**\n\t * The global epoch number when this signal's value last changed.\n\t *\n\t * Note that this represents when the value actually changed, not when it was last computed.\n\t * A computed signal may recalculate and produce the same value without changing its epoch.\n\t * This is used internally for dependency tracking and history management.\n\t */\n\tlastChangedEpoch: number\n\t/**\n\t * Gets the sequence of diffs that occurred between a specific epoch and the current state.\n\t *\n\t * This method enables incremental synchronization by providing a list of changes that\n\t * have occurred since a specific point in time. If the requested epoch is too far in\n\t * the past or the signal doesn't have enough history, it returns the unique symbol RESET_VALUE\n\t * to indicate that a full state reset is required.\n\t *\n\t * @param epoch - The epoch timestamp to get diffs since\n\t * @returns An array of diff objects representing changes since the epoch, or the unique symbol RESET_VALUE if insufficient history is available\n\t */\n\tgetDiffSince(epoch: number): RESET_VALUE | Diff[]\n\t/**\n\t * Gets the current value of the signal without establishing a dependency relationship.\n\t *\n\t * This method bypasses the automatic dependency tracking system, making it useful for\n\t * performance-critical code paths where the overhead of dependency capture would be\n\t * problematic. Use with caution as it breaks the reactive guarantees of the system.\n\t *\n\t * **Warning**: This method should only be used when you're certain that you don't need\n\t * the calling context to react to changes in this signal.\n\t *\n\t * @param ignoreErrors - Whether to suppress errors during value retrieval (optional)\n\t * @returns The current value without establishing dependencies\n\t */\n\t__unsafe__getWithoutCapture(ignoreErrors?: boolean): Value\n\t/** @internal */\n\tchildren: ArraySet<Child>\n}\n\n/**\n * Internal interface representing a child node in the signal dependency graph.\n *\n * This interface is used internally by the reactive system to manage dependencies\n * between signals, computed values, and effects. Each child tracks its parent\n * signals and maintains state needed for efficient dependency graph traversal\n * and change propagation.\n *\n * @internal\n */\nexport interface Child {\n\t/**\n\t * The epoch when this child was last traversed during dependency graph updates.\n\t * Used to prevent redundant traversals during change propagation.\n\t */\n\tlastTraversedEpoch: number\n\n\t/**\n\t * Set of parent signals that this child depends on.\n\t * Used for efficient lookup and cleanup operations.\n\t */\n\treadonly parentSet: ArraySet<Signal<any, any>>\n\n\t/**\n\t * Array of parent signals that this child depends on.\n\t * Maintained in parallel with parentSet for ordered access.\n\t */\n\treadonly parents: Signal<any, any>[]\n\n\t/**\n\t * Array of epochs corresponding to each parent signal.\n\t * Used to detect which parents have changed since last computation.\n\t */\n\treadonly parentEpochs: number[]\n\n\t/**\n\t * Human-readable name for this child, used in debugging output.\n\t */\n\treadonly name: string\n\n\t/**\n\t * Whether this child is currently subscribed to change notifications.\n\t * Used to optimize resource usage by unsubscribing inactive dependencies.\n\t */\n\tisActivelyListening: boolean\n\n\t/**\n\t * Debug information tracking ancestor epochs in the dependency graph.\n\t * Only populated in debug builds for diagnostic purposes.\n\t */\n\t__debug_ancestor_epochs__: Map<Signal<any, any>, number> | null\n}\n\n/**\n * A function type that computes the difference between two values of a signal.\n *\n * This function is used to generate incremental diffs that can be applied to\n * reconstruct state changes over time. It's particularly useful for features\n * like undo/redo, synchronization, and change tracking.\n *\n * The function should analyze the previous and current values and return a\n * diff object that represents the change. If the diff cannot be computed\n * (e.g., the values are too different or incompatible), it should return\n * the unique symbol RESET_VALUE to indicate that a full state reset is required.\n *\n * @param previousValue - The previous value of the signal\n * @param currentValue - The current value of the signal\n * @param lastComputedEpoch - The epoch when the previous value was set\n * @param currentEpoch - The epoch when the current value was set\n * @returns A diff object representing the change, or the unique symbol RESET_VALUE if no diff can be computed\n *\n * @example\n * ```ts\n * import { atom, RESET_VALUE } from '@tldraw/state'\n *\n * // Simple numeric diff\n * const numberDiff: ComputeDiff<number, number> = (prev, curr) => curr - prev\n *\n * // Array diff with reset fallback\n * const arrayDiff: ComputeDiff<string[], { added: string[], removed: string[] }> = (prev, curr) => {\n * if (prev.length > 1000 || curr.length > 1000) {\n * return RESET_VALUE // Too complex, force reset\n * }\n * return {\n * added: curr.filter(item => !prev.includes(item)),\n * removed: prev.filter(item => !curr.includes(item))\n * }\n * }\n *\n * const count = atom('count', 0, { computeDiff: numberDiff })\n * ```\n *\n * @public\n */\nexport type ComputeDiff<Value, Diff> = (\n\tpreviousValue: Value,\n\tcurrentValue: Value,\n\tlastComputedEpoch: number,\n\tcurrentEpoch: number\n) => Diff | RESET_VALUE\n"],
|
|
5
|
-
"mappings": "AA8BO,MAAM,cAA6B,
|
|
5
|
+
"mappings": "AA8BO,MAAM,cAA6B,uBAAO,IAAI,8BAA8B;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
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": "4.6.0-
|
|
4
|
+
"version": "4.6.0-next.0eb36d65eec3",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "tldraw Inc.",
|
|
7
7
|
"email": "hello@tldraw.com"
|
|
@@ -40,7 +40,7 @@
|
|
|
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
|
|
43
|
+
"lint": "cd ../.. && yarn run -T oxlint packages/state"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@types/lodash": "^4.17.14",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"vitest": "^3.2.4"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@tldraw/utils": "4.6.0-
|
|
51
|
+
"@tldraw/utils": "4.6.0-next.0eb36d65eec3"
|
|
52
52
|
},
|
|
53
53
|
"typedoc": {
|
|
54
54
|
"readmeFile": "none",
|
package/src/lib/ArraySet.ts
CHANGED
|
@@ -28,7 +28,7 @@ export class ArraySet<T> {
|
|
|
28
28
|
*
|
|
29
29
|
* @returns True if this ArraySet has any elements, false otherwise.
|
|
30
30
|
*/
|
|
31
|
-
// eslint-disable-next-line no-
|
|
31
|
+
// eslint-disable-next-line tldraw/no-setter-getter
|
|
32
32
|
get isEmpty() {
|
|
33
33
|
if (this.array) {
|
|
34
34
|
return this.arraySize === 0
|
package/src/lib/Atom.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ArraySet } from './ArraySet'
|
|
2
|
-
import { HistoryBuffer } from './HistoryBuffer'
|
|
3
2
|
import { maybeCaptureParent } from './capture'
|
|
4
3
|
import { EMPTY_ARRAY, equals, singleton } from './helpers'
|
|
4
|
+
import { HistoryBuffer } from './HistoryBuffer'
|
|
5
5
|
import { advanceGlobalEpoch, atomDidChange, getGlobalEpoch } from './transactions'
|
|
6
6
|
import { Child, ComputeDiff, RESET_VALUE, Signal } from './types'
|
|
7
7
|
|
package/src/lib/Computed.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/* eslint-disable prefer-rest-params */
|
|
2
2
|
import { assert } from '@tldraw/utils'
|
|
3
3
|
import { ArraySet } from './ArraySet'
|
|
4
|
-
import { HistoryBuffer } from './HistoryBuffer'
|
|
5
4
|
import { maybeCaptureParent, startCapturingParents, stopCapturingParents } from './capture'
|
|
6
5
|
import { GLOBAL_START_EPOCH } from './constants'
|
|
7
6
|
import { EMPTY_ARRAY, equals, haveParentsChanged, singleton } from './helpers'
|
|
7
|
+
import { HistoryBuffer } from './HistoryBuffer'
|
|
8
8
|
import { getGlobalEpoch, getIsReacting, getReactionEpoch } from './transactions'
|
|
9
9
|
import { Child, ComputeDiff, RESET_VALUE, Signal } from './types'
|
|
10
10
|
import { logComputedGetterWarning } from './warnings'
|
|
@@ -227,7 +227,7 @@ class __UNSAFE__Computed<Value, Diff = unknown> implements Computed<Value, Diff>
|
|
|
227
227
|
|
|
228
228
|
children = new ArraySet<Child>()
|
|
229
229
|
|
|
230
|
-
// eslint-disable-next-line no-
|
|
230
|
+
// eslint-disable-next-line tldraw/no-setter-getter
|
|
231
231
|
get isActivelyListening(): boolean {
|
|
232
232
|
return !this.children.isEmpty
|
|
233
233
|
}
|
|
@@ -36,7 +36,7 @@ export interface EffectSchedulerOptions {
|
|
|
36
36
|
* @param execute - A function that will execute the effect.
|
|
37
37
|
* @returns void
|
|
38
38
|
*/
|
|
39
|
-
// eslint-disable-next-line
|
|
39
|
+
// eslint-disable-next-line tldraw/method-signature-style
|
|
40
40
|
scheduleEffect?: (execute: () => void) => void
|
|
41
41
|
}
|
|
42
42
|
|
|
@@ -48,7 +48,7 @@ class __EffectScheduler__<Result> implements EffectScheduler<Result> {
|
|
|
48
48
|
* Whether this scheduler is attached and actively listening to its parents.
|
|
49
49
|
* @public
|
|
50
50
|
*/
|
|
51
|
-
// eslint-disable-next-line no-
|
|
51
|
+
// eslint-disable-next-line tldraw/no-setter-getter
|
|
52
52
|
get isActivelyListening() {
|
|
53
53
|
return this._isActivelyListening
|
|
54
54
|
}
|
|
@@ -67,7 +67,7 @@ class __EffectScheduler__<Result> implements EffectScheduler<Result> {
|
|
|
67
67
|
* The number of times this effect has been scheduled.
|
|
68
68
|
* @public
|
|
69
69
|
*/
|
|
70
|
-
// eslint-disable-next-line no-
|
|
70
|
+
// eslint-disable-next-line tldraw/no-setter-getter
|
|
71
71
|
get scheduleCount() {
|
|
72
72
|
return this._scheduleCount
|
|
73
73
|
}
|
|
@@ -117,7 +117,7 @@ class __EffectScheduler__<Result> implements EffectScheduler<Result> {
|
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
/** @internal */
|
|
120
|
-
// eslint-disable-next-line
|
|
120
|
+
// eslint-disable-next-line tldraw/prefer-class-methods
|
|
121
121
|
readonly maybeExecute = () => {
|
|
122
122
|
// bail out if we have been detached before this runs
|
|
123
123
|
if (!this._isActivelyListening) return
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { ArraySet } from '../ArraySet'
|
|
2
2
|
import { atom } from '../Atom'
|
|
3
|
-
import { computed } from '../Computed'
|
|
4
|
-
import { react } from '../EffectScheduler'
|
|
5
3
|
import {
|
|
6
4
|
maybeCaptureParent,
|
|
7
5
|
startCapturingParents,
|
|
8
6
|
stopCapturingParents,
|
|
9
7
|
unsafe__withoutCapture,
|
|
10
8
|
} from '../capture'
|
|
9
|
+
import { computed } from '../Computed'
|
|
10
|
+
import { react } from '../EffectScheduler'
|
|
11
11
|
import { advanceGlobalEpoch, getGlobalEpoch } from '../transactions'
|
|
12
12
|
import { Child } from '../types'
|
|
13
13
|
|
|
@@ -75,7 +75,7 @@ const unpack = (value: unknown): Letter => {
|
|
|
75
75
|
interface FuzzSystemState {
|
|
76
76
|
atoms: Record<string, Atom<Letter>>
|
|
77
77
|
atomsInAtoms: Record<string, Atom<Atom<Letter>>>
|
|
78
|
-
// eslint-disable-next-line
|
|
78
|
+
// eslint-disable-next-line tldraw/method-signature-style
|
|
79
79
|
derivations: Record<string, { derivation: Computed<Letter>; sneakyGet: () => Letter }>
|
|
80
80
|
derivationsInDerivations: Record<string, Computed<Computed<Letter>>>
|
|
81
81
|
atomsInDerivations: Record<string, Computed<Atom<Letter>>>
|
|
@@ -112,7 +112,7 @@ class Test {
|
|
|
112
112
|
reactors: {},
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
-
// eslint-disable-next-line
|
|
115
|
+
// eslint-disable-next-line tldraw/prefer-class-methods
|
|
116
116
|
unpack_sneaky = (value: unknown): Letter => {
|
|
117
117
|
if (isComputed(value)) {
|
|
118
118
|
if (this.systemState.derivations[value.name]) {
|