packages/state/ARCHITECTURE.md
This document provides a machine-readable guide to the @tldraw/state package structure, optimized for AI agents and automated tools.
Package Name: @tldraw/state
Purpose: Reactive state management library using signals
Entry Point: src/index.ts
Main Export: Complete reactive signals API
Dependencies: @tldraw/utils
packages/state/
├── src/
│ ├── index.ts # Main entry point, all exports
│ └── lib/ # Core implementation modules
│ ├── Atom.ts # Mutable reactive state containers
│ ├── Computed.ts # Derived reactive values
│ ├── EffectScheduler.ts # Side effects and reactions
│ ├── ArraySet.ts # Optimized set data structure
│ ├── HistoryBuffer.ts # Change tracking and history
│ ├── capture.ts # Dependency tracking system
│ ├── transactions.ts # Batched updates and rollbacks
│ ├── types.ts # Core type definitions
│ ├── constants.ts # System constants
│ ├── helpers.ts # Utility functions
│ ├── isSignal.ts # Type guards
│ ├── warnings.ts # Development warnings
│ └── __tests__/ # Test files
├── DOCS.md # Human-readable documentation
├── README.md # Package introduction
└── ARCHITECTURE.md # This file
Atom.tsatom(), isAtom(), Atom interface, AtomOptions_AtomComputed.tscomputed(), @computed, getComputedInstance(), isUninitialized(), UNINITIALIZED, withDiff()_ComputedEffectScheduler.tsEffectScheduler, react(), reactor(), EffectSchedulerOptions, Reactor__EffectScheduler__, _Reactortypes.tsSignal<Value, Diff>, Child, ComputeDiff, RESET_VALUEcapture.tsunsafe__withoutCapture(), whyAmIRunning()CaptureStackFrametransactions.tstransact(), transaction(), deferAsyncEffects()Transactionhelpers.tsEMPTY_ARRAY, utility functionsequals(), singleton(), attach(), detach(), haveParentsChanged()ArraySet.tsArraySetHistoryBuffer.ts// Core reactive primitives
atom(name, value, options?) -> Atom<T>
computed(name, fn, options?) -> Computed<T>
react(name, fn, options?) -> () => void
reactor(name, fn) -> Reactor<T>
// Transaction management
transact(fn) -> void
transaction(fn) -> void
deferAsyncEffects(fn) -> void
// Debugging and utilities
unsafe__withoutCapture(fn) -> T
whyAmIRunning() -> void
isSignal(value) -> boolean
isAtom(value) -> boolean
isUninitialized(value) -> boolean
// Advanced features
@computed -> PropertyDecorator
getComputedInstance(obj, prop) -> Computed
withDiff(value, diff) -> WithDiff
UNINITIALIZED -> symbol
RESET_VALUE -> symbol
EMPTY_ARRAY -> readonly array
index.ts
├── Atom.ts
│ ├── ArraySet.ts
│ ├── HistoryBuffer.ts
│ ├── capture.ts
│ ├── helpers.ts
│ ├── transactions.ts
│ └── types.ts
├── Computed.ts
│ ├── ArraySet.ts
│ ├── HistoryBuffer.ts
│ ├── capture.ts
│ ├── constants.ts
│ ├── helpers.ts
│ ├── transactions.ts
│ ├── types.ts
│ └── warnings.ts
├── EffectScheduler.ts
│ ├── ArraySet.ts
│ ├── capture.ts
│ ├── constants.ts
│ ├── helpers.ts
│ ├── transactions.ts
│ └── types.ts
├── capture.ts
│ ├── Computed.ts
│ ├── helpers.ts
│ └── types.ts
└── transactions.ts
├── Atom.ts
├── EffectScheduler.ts
├── constants.ts
├── helpers.ts
└── types.ts
Signal<Value, Diff> interfaceget(), getDiffSince(), __unsafe__getWithoutCapture()capture.ts manages parent-child relationshipsChild interface for dependents, ArraySet<Child> for parentsmaybeCaptureParent()lastChangedEpoch tracking for invalidationhaveParentsChanged() for efficient dirty checkinginitialAtomValues map for restorationHistoryBuffer for change trackingcomputeDiff functions for incremental updatesRESET_VALUE for uncomputable diffsArraySet for efficient parent-child trackingEMPTY_ARRAY singleton for empty dependenciesunsafe__withoutCapture for hot pathsscheduleEffect for batchingdeferAsyncEffects for transaction controlisActivelyListening state managementatom() - mutable statecomputed() - derived statereact() - side effectsreactor() - controllable reactionstransact() - batched updates@computed - class-based computed propertiesunsafe__withoutCapture() - performance optimizationwhyAmIRunning() - dependency debuggingisSignal(), isAtom(), isUninitialized()UNINITIALIZED, RESET_VALUE constantswithDiff() for incremental computation@tldraw/utils: registerTldrawLibraryVersion(), assert()@tldraw/state-react: React integration layer@tldraw/store: Record storage using @tldraw/state@tldraw/editor: Canvas editor using reactive stateAtomOptions.isEqual - custom equality functionsComputeDiff - custom diff computationEffectSchedulerOptions.scheduleEffect - custom schedulingTo understand signals: Start with types.ts → Atom.ts → Computed.ts
To understand reactivity: Start with EffectScheduler.ts → capture.ts
To understand transactions: Start with transactions.ts
To understand the API: Start with index.ts exports
To understand performance: Focus on helpers.ts → ArraySet.ts
To understand change tracking: Focus on HistoryBuffer.ts
Key entry points for specific functionality:
Atom.ts:atom()Computed.ts:computed()EffectScheduler.ts:react()transactions.ts:transact()capture.ts:whyAmIRunning()