@tldraw/tlstore
Advanced tools
Comparing version 2.0.0-canary.03595bc88dd5 to 2.0.0-canary.0375dbf8c092
@@ -499,2 +499,7 @@ import { Atom } from 'signia'; | ||
/** | ||
* The same as `serialize`, but only serializes records with a scope of `document`. | ||
* @returns The record store snapshot as a JSON payload. | ||
*/ | ||
serializeDocumentState: () => StoreSnapshot<R>; | ||
/** | ||
* Opposite of `serialize`. Replace the store's current records with records as defined by a | ||
@@ -569,2 +574,3 @@ * simple JSON structure into the stores. | ||
createSelectedComputedCache: <T, J, V extends R = R>(name: string, selector: (record: V) => T | undefined, derive: (input: T) => J | undefined) => ComputedCache<J, V>; | ||
private _integrityChecker?; | ||
/* Excluded from this release type: ensureStoreIsUsable */ | ||
@@ -688,3 +694,3 @@ private _isPossiblyCorrupted; | ||
migrateStoreSnapshot(storeSnapshot: StoreSnapshot<R>, persistedSchema: SerializedSchema): MigrationResult<StoreSnapshot<R>>; | ||
/* Excluded from this release type: ensureStoreIsUsable */ | ||
/* Excluded from this release type: createIntegrityChecker */ | ||
/* Excluded from this release type: derivePresenceState */ | ||
@@ -707,3 +713,3 @@ serialize(): SerializedSchema; | ||
}) => R; | ||
/* Excluded from this release type: ensureStoreIsUsable */ | ||
/* Excluded from this release type: createIntegrityChecker */ | ||
/* Excluded from this release type: derivePresenceState */ | ||
@@ -710,0 +716,0 @@ }; |
@@ -26,2 +26,3 @@ "use strict"; | ||
module.exports = __toCommonJS(Store_exports); | ||
var import_utils = require("@tldraw/utils"); | ||
var import_signia = require("signia"); | ||
@@ -96,3 +97,3 @@ var import_Cache = require("./Cache"); | ||
}, | ||
{ scheduleEffect: (cb) => requestAnimationFrame(cb) } | ||
{ scheduleEffect: (cb) => (0, import_utils.throttledRaf)(cb) } | ||
); | ||
@@ -299,2 +300,12 @@ } | ||
/** | ||
* The same as `serialize`, but only serializes records with a scope of `document`. | ||
* @returns The record store snapshot as a JSON payload. | ||
*/ | ||
serializeDocumentState = () => { | ||
return this.serialize((r) => { | ||
const type = this.schema.types[r.typeName]; | ||
return type.scope === "document"; | ||
}); | ||
}; | ||
/** | ||
* Opposite of `serialize`. Replace the store's current records with records as defined by a | ||
@@ -469,5 +480,7 @@ * simple JSON structure into the stores. | ||
}; | ||
_integrityChecker; | ||
/** @internal */ | ||
ensureStoreIsUsable() { | ||
this.schema.ensureStoreIsUsable(this); | ||
this._integrityChecker ??= this.schema.createIntegrityChecker(this); | ||
this._integrityChecker?.(); | ||
} | ||
@@ -474,0 +487,0 @@ _isPossiblyCorrupted = false; |
@@ -150,4 +150,4 @@ "use strict"; | ||
/** @internal */ | ||
ensureStoreIsUsable(store) { | ||
this.options.ensureStoreIsUsable?.(store); | ||
createIntegrityChecker(store) { | ||
return this.options.createIntegrityChecker?.(store) ?? void 0; | ||
} | ||
@@ -154,0 +154,0 @@ /** @internal */ |
{ | ||
"name": "@tldraw/tlstore", | ||
"description": "A tiny little drawing app (store).", | ||
"version": "2.0.0-canary.03595bc88dd5", | ||
"version": "2.0.0-canary.0375dbf8c092", | ||
"packageManager": "yarn@3.5.0", | ||
@@ -37,3 +37,3 @@ "author": { | ||
"test-coverage": "lazy inherit", | ||
"build-package": "yarn run -T tsx ../../scripts/build-package.ts", | ||
"build": "yarn run -T tsx ../../scripts/build-package.ts", | ||
"build-api": "yarn run -T tsx ../../scripts/build-api.ts", | ||
@@ -46,3 +46,3 @@ "prepack": "yarn run -T tsx ../../scripts/prepack.ts", | ||
"dependencies": { | ||
"@tldraw/utils": "2.0.0-canary.03595bc88dd5", | ||
"@tldraw/utils": "2.0.0-canary.0375dbf8c092", | ||
"lodash.isequal": "^4.5.0", | ||
@@ -49,0 +49,0 @@ "nanoid": "4.0.2" |
@@ -0,1 +1,2 @@ | ||
import { throttledRaf } from '@tldraw/utils' | ||
import { atom, Atom, computed, Computed, Reactor, reactor, transact } from 'signia' | ||
@@ -5,2 +6,3 @@ import { BaseRecord, ID } from './BaseRecord' | ||
import { devFreeze } from './devFreeze' | ||
import { RecordType } from './RecordType' | ||
import { StoreQueries } from './StoreQueries' | ||
@@ -176,3 +178,3 @@ import { StoreSchema } from './StoreSchema' | ||
}, | ||
{ scheduleEffect: (cb) => requestAnimationFrame(cb) } | ||
{ scheduleEffect: (cb) => throttledRaf(cb) } | ||
) | ||
@@ -434,2 +436,13 @@ } | ||
/** | ||
* The same as `serialize`, but only serializes records with a scope of `document`. | ||
* @returns The record store snapshot as a JSON payload. | ||
*/ | ||
serializeDocumentState = (): StoreSnapshot<R> => { | ||
return this.serialize((r) => { | ||
const type = this.schema.types[r.typeName as R['typeName']] as RecordType<any, any> | ||
return type.scope === 'document' | ||
}) | ||
} | ||
/** | ||
* Opposite of `serialize`. Replace the store's current records with records as defined by a | ||
@@ -627,5 +640,8 @@ * simple JSON structure into the stores. | ||
private _integrityChecker?: () => void | undefined | ||
/** @internal */ | ||
ensureStoreIsUsable() { | ||
this.schema.ensureStoreIsUsable(this) | ||
this._integrityChecker ??= this.schema.createIntegrityChecker(this) | ||
this._integrityChecker?.() | ||
} | ||
@@ -632,0 +648,0 @@ |
@@ -51,3 +51,3 @@ import { getOwnProperty, objectMapValues } from '@tldraw/utils' | ||
/** @internal */ | ||
ensureStoreIsUsable?: (store: Store<R, P>) => void | ||
createIntegrityChecker?: (store: Store<R, P>) => void | ||
/** @internal */ | ||
@@ -244,4 +244,4 @@ derivePresenceState?: (store: Store<R, P>) => Signal<R | null> | ||
/** @internal */ | ||
ensureStoreIsUsable(store: Store<R, P>): void { | ||
this.options.ensureStoreIsUsable?.(store) | ||
createIntegrityChecker(store: Store<R, P>): (() => void) | undefined { | ||
return this.options.createIntegrityChecker?.(store) ?? undefined | ||
} | ||
@@ -248,0 +248,0 @@ |
@@ -472,22 +472,29 @@ import { Computed, react, RESET_VALUE, transact } from 'signia' | ||
it('flushes history before attaching listeners', async () => { | ||
store.put([Author.create({ name: 'J.R.R Tolkein', id: Author.createCustomId('tolkein') })]) | ||
const firstListener = jest.fn() | ||
store.listen(firstListener) | ||
expect(firstListener).toHaveBeenCalledTimes(0) | ||
try { | ||
// @ts-expect-error | ||
globalThis.__FORCE_RAF_IN_TESTS__ = true | ||
store.put([Author.create({ name: 'J.R.R Tolkein', id: Author.createCustomId('tolkein') })]) | ||
const firstListener = jest.fn() | ||
store.listen(firstListener) | ||
expect(firstListener).toHaveBeenCalledTimes(0) | ||
store.put([Author.create({ name: 'Chips McCoy', id: Author.createCustomId('chips') })]) | ||
store.put([Author.create({ name: 'Chips McCoy', id: Author.createCustomId('chips') })]) | ||
expect(firstListener).toHaveBeenCalledTimes(0) | ||
expect(firstListener).toHaveBeenCalledTimes(0) | ||
const secondListener = jest.fn() | ||
const secondListener = jest.fn() | ||
store.listen(secondListener) | ||
store.listen(secondListener) | ||
expect(firstListener).toHaveBeenCalledTimes(1) | ||
expect(secondListener).toHaveBeenCalledTimes(0) | ||
expect(firstListener).toHaveBeenCalledTimes(1) | ||
expect(secondListener).toHaveBeenCalledTimes(0) | ||
await new Promise((resolve) => requestAnimationFrame(resolve)) | ||
await new Promise((resolve) => requestAnimationFrame(resolve)) | ||
expect(firstListener).toHaveBeenCalledTimes(1) | ||
expect(secondListener).toHaveBeenCalledTimes(0) | ||
expect(firstListener).toHaveBeenCalledTimes(1) | ||
expect(secondListener).toHaveBeenCalledTimes(0) | ||
} finally { | ||
// @ts-expect-error | ||
globalThis.__FORCE_RAF_IN_TESTS__ = false | ||
} | ||
}) | ||
@@ -494,0 +501,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
536257
9203
+ Added@tldraw/utils@2.0.0-canary.0375dbf8c092(transitive)
- Removed@tldraw/utils@2.0.0-canary.03595bc88dd5(transitive)