Store
See source codeTable of contents
- history
 - id
 - props
 - query
 - schema
 - scopedTypes
 - sideEffects
 - Properties
 - Methods
- _flushHistory
 - allRecords
 - applyDiff
 - clear
 - createComputedCache
 - createSelectedComputedCache
 - dispose
 - extractingChanges
 - filterChangesByScope
 - get
 - getSnapshot
 - getStoreSnapshot
 - has
 - listen
 - loadSnapshot
 - loadStoreSnapshot
 - mergeRemoteChanges
 - migrateSnapshot
 - put
 - remove
 - serialize
 - unsafeGetWithoutCapture
 - update
 - validate
 
 
A store of records.
class Store<R extends UnknownRecord = UnknownRecord, Props = unknown> {}Constructor
Constructs a new instance of the Store class
Parameters
| Name | Description | 
|---|---|
  |  | 
Properties
history
An atom containing the store's history.
readonly history: Atom<number, RecordsDiff<R>>id
The random id of the store.
readonly id: stringprops
readonly props: Propsquery
A StoreQueries instance for this store.
readonly query: StoreQueries<R>schema
readonly schema: StoreSchema<R, Props>scopedTypes
readonly scopedTypes: {
  readonly [K in RecordScope]: ReadonlySet<R['typeName']>
}sideEffects
readonly sideEffects: StoreSideEffects<R>Methods
allRecords()
Get an array of all values in the store.
allRecords(): R[]applyDiff()
applyDiff(
  diff: RecordsDiff<R>,
  {
    runCallbacks,
    ignoreEphemeralKeys,
  }?: {
    ignoreEphemeralKeys?: boolean
    runCallbacks?: boolean
  }
): voidParameters
| Name | Description | 
|---|---|
  |  | 
  |  | 
Returns
voidclear()
Removes all records from the store.
clear(): voidcreateComputedCache()
Create a computed cache.
createComputedCache<Result, Record extends R = R>(
  name: string,
  derive: (record: Record) => Result | undefined,
  isEqual?: (a: Record, b: Record) => boolean
): ComputedCache<Result, Record>Parameters
| Name | Description | 
|---|---|
  | The name of the derivation cache.  | 
  | A function used to derive the value of the cache.  | 
  | A function that determines equality between two records.  | 
Returns
ComputedCache<Result, Record>createSelectedComputedCache()
Create a computed cache from a selector
createSelectedComputedCache<Selection, Result, Record extends R = R>(
  name: string,
  selector: (record: Record) => Selection | undefined,
  derive: (input: Selection) => Result | undefined
): ComputedCache<Result, Record>Parameters
| Name | Description | 
|---|---|
  | The name of the derivation cache.  | 
  | A function that returns a subset of the original shape  | 
  | A function used to derive the value of the cache.  | 
Returns
ComputedCache<Result, Record>dispose()
dispose(): voidextractingChanges()
Run fn and return a RecordsDiff of the changes that occurred as a result.
extractingChanges(fn: () => void): RecordsDiff<R>Parameters
| Name | Description | 
|---|---|
  |  | 
Returns
RecordsDiff<R>filterChangesByScope()
Filters out non-document changes from a diff. Returns null if there are no changes left.
filterChangesByScope(
  change: RecordsDiff<R>,
  scope: RecordScope
): {
  added: { [K in IdOf<R>]: R }
  removed: { [K in IdOf<R>]: R }
  updated: { [K_1 in IdOf<R>]: [from: R, to: R] }
} | nullParameters
| Name | Description | 
|---|---|
  | the records diff  | 
  | the records scope  | 
Returns
{
  added: { [K in IdOf<R>]: R }
  removed: { [K in IdOf<R>]: R }
  updated: { [K_1 in IdOf<R>]: [from: R, to: R] }
} | nullget()
Get the value of a store record by its id.
get<K extends IdOf<R>>(id: K): RecordFromId<K> | undefinedParameters
| Name | Description | 
|---|---|
  | The id of the record to get.  | 
Returns
RecordFromId<K> | undefinedgetSnapshot()
Deprecated:
use getSnapshot from the 'tldraw' package instead.
getSnapshot(scope?: 'all' | RecordScope): StoreSnapshot<R>Parameters
| Name | Description | 
|---|---|
  |  | 
Returns
getStoreSnapshot()
Get a serialized snapshot of the store and its schema.
const snapshot = store.getStoreSnapshot()
store.loadStoreSnapshot(snapshot)getStoreSnapshot(scope?: 'all' | RecordScope): StoreSnapshot<R>Parameters
| Name | Description | 
|---|---|
  | The scope of records to serialize. Defaults to 'document'.  | 
Returns
has()
Get whether the record store has a id.
has<K extends IdOf<R>>(id: K): booleanParameters
| Name | Description | 
|---|---|
  | The id of the record to check.  | 
Returns
booleanlisten()
Add a new listener to the store.
listen(
  onHistory: StoreListener<R>,
  filters?: Partial<StoreListenerFilters>
): () => voidParameters
| Name | Description | 
|---|---|
  | The listener to call when the store updates.  | 
  | Filters to apply to the listener.  | 
Returns
() => voidA function to remove the listener.
loadSnapshot()
Deprecated:
use loadSnapshot from the 'tldraw' package instead.
loadSnapshot(snapshot: StoreSnapshot<R>): voidParameters
| Name | Description | 
|---|---|
  | 
Returns
voidloadStoreSnapshot()
Load a serialized snapshot.
const snapshot = store.getStoreSnapshot()
store.loadStoreSnapshot(snapshot)loadStoreSnapshot(snapshot: StoreSnapshot<R>): voidParameters
| Name | Description | 
|---|---|
  | The snapshot to load.  | 
Returns
voidmergeRemoteChanges()
Merge changes from a remote source without triggering listeners.
mergeRemoteChanges(fn: () => void): voidParameters
| Name | Description | 
|---|---|
  | A function that merges the external changes.  | 
Returns
voidmigrateSnapshot()
Migrate a serialized snapshot of the store and its schema.
const snapshot = store.getSnapshot()
store.migrateSnapshot(snapshot)migrateSnapshot(snapshot: StoreSnapshot<R>): StoreSnapshot<R>Parameters
| Name | Description | 
|---|---|
  | The snapshot to load.  | 
Returns
put()
Add some records to the store. It's an error if they already exist.
put(records: R[], phaseOverride?: 'initialize'): voidParameters
| Name | Description | 
|---|---|
  | The records to add.  | 
  | The phase override.  | 
Returns
voidremove()
Remove some records from the store via their ids.
remove(ids: IdOf<R>[]): voidParameters
| Name | Description | 
|---|---|
  | The ids of the records to remove.  | 
Returns
voidserialize()
Creates a JSON payload from the record store.
serialize(scope?: 'all' | RecordScope): SerializedStore<R>Parameters
| Name | Description | 
|---|---|
  | The scope of records to serialize. Defaults to 'document'.  | 
Returns
The record store snapshot as a JSON payload.
unsafeGetWithoutCapture()
Get the value of a store record by its id without updating its epoch.
unsafeGetWithoutCapture<K extends IdOf<R>>(id: K): RecordFromId<K> | undefinedParameters
| Name | Description | 
|---|---|
  | The id of the record to get.  | 
Returns
RecordFromId<K> | undefinedupdate()
Update a record. To update multiple records at once, use the update method of the
TypedStore class.
update<K extends IdOf<R>>(
  id: K,
  updater: (record: RecordFromId<K>) => RecordFromId<K>
): voidParameters
| Name | Description | 
|---|---|
  | The id of the record to update.  | 
  | A function that updates the record.  | 
Returns
voidvalidate()
validate(
  phase: 'createRecord' | 'initialize' | 'tests' | 'updateRecord'
): voidParameters
| Name | Description | 
|---|---|
  |  | 
Returns
void