diff --git a/packages/editor/src/lib/utils/sync/TLLocalSyncClient.ts b/packages/editor/src/lib/utils/sync/TLLocalSyncClient.ts index b1b199fa9..bfccf3c84 100644 --- a/packages/editor/src/lib/utils/sync/TLLocalSyncClient.ts +++ b/packages/editor/src/lib/utils/sync/TLLocalSyncClient.ts @@ -181,10 +181,10 @@ export class TLLocalSyncClient { const documentSnapshot = Object.fromEntries(data.records.map((r) => [r.id, r])) const sessionStateSnapshot = data.sessionStateSnapshot ?? extractSessionStateFromLegacySnapshot(documentSnapshot) - const migrationResult = this.store.schema.migrateStoreSnapshot( - documentSnapshot, - data.schema ?? this.store.schema.serializeEarliestVersion() - ) + const migrationResult = this.store.schema.migrateStoreSnapshot({ + store: documentSnapshot, + schema: data.schema ?? this.store.schema.serializeEarliestVersion(), + }) if (migrationResult.type === 'error') { console.error('failed to migrate store', migrationResult) diff --git a/packages/file-format/src/lib/file.ts b/packages/file-format/src/lib/file.ts index 57d5b0e29..b3fcecc6c 100644 --- a/packages/file-format/src/lib/file.ts +++ b/packages/file-format/src/lib/file.ts @@ -122,7 +122,7 @@ export function parseTldrawJsonFile({ let migrationResult: MigrationResult> try { const storeSnapshot = Object.fromEntries(data.records.map((r) => [r.id, r as TLRecord])) - migrationResult = schema.migrateStoreSnapshot(storeSnapshot, data.schema) + migrationResult = schema.migrateStoreSnapshot({ store: storeSnapshot, schema: data.schema }) } catch (e) { // junk data in the migration return Result.err({ type: 'invalidRecords', cause: e }) diff --git a/packages/store/api-report.md b/packages/store/api-report.md index 329cab88c..efcbfb7b3 100644 --- a/packages/store/api-report.md +++ b/packages/store/api-report.md @@ -304,7 +304,7 @@ export class StoreSchema { // (undocumented) migratePersistedRecord(record: R, persistedSchema: SerializedSchema, direction?: 'down' | 'up'): MigrationResult; // (undocumented) - migrateStoreSnapshot(storeSnapshot: SerializedStore, persistedSchema: SerializedSchema): MigrationResult>; + migrateStoreSnapshot(snapshot: StoreSnapshot): MigrationResult>; // (undocumented) serialize(): SerializedSchema; // (undocumented) diff --git a/packages/store/src/lib/Store.ts b/packages/store/src/lib/Store.ts index 4ed3f427a..ec3467b86 100644 --- a/packages/store/src/lib/Store.ts +++ b/packages/store/src/lib/Store.ts @@ -551,7 +551,7 @@ export class Store { * @public */ loadSnapshot(snapshot: StoreSnapshot): void { - const migrationResult = this.schema.migrateStoreSnapshot(snapshot.store, snapshot.schema) + const migrationResult = this.schema.migrateStoreSnapshot(snapshot) if (migrationResult.type === 'error') { throw new Error(`Failed to migrate snapshot: ${migrationResult.reason}`) diff --git a/packages/store/src/lib/StoreSchema.ts b/packages/store/src/lib/StoreSchema.ts index 390a61274..49bc882ec 100644 --- a/packages/store/src/lib/StoreSchema.ts +++ b/packages/store/src/lib/StoreSchema.ts @@ -1,7 +1,7 @@ import { getOwnProperty, objectMapValues } from '@tldraw/utils' import { IdOf, UnknownRecord } from './BaseRecord' import { RecordType } from './RecordType' -import { SerializedStore, Store } from './Store' +import { SerializedStore, Store, StoreSnapshot } from './Store' import { MigrationFailureReason, MigrationResult, @@ -188,17 +188,14 @@ export class StoreSchema { return { type: 'success', value: result.value } } - migrateStoreSnapshot( - storeSnapshot: SerializedStore, - persistedSchema: SerializedSchema - ): MigrationResult> { + migrateStoreSnapshot(snapshot: StoreSnapshot): MigrationResult> { const migrations = this.options.snapshotMigrations if (!migrations) { - return { type: 'success', value: storeSnapshot } + return { type: 'success', value: snapshot.store } } // apply store migrations first const ourStoreVersion = migrations.currentVersion - const persistedStoreVersion = persistedSchema.storeVersion ?? 0 + const persistedStoreVersion = snapshot.schema.storeVersion ?? 0 if (ourStoreVersion < persistedStoreVersion) { return { type: 'error', reason: MigrationFailureReason.TargetVersionTooOld } @@ -206,7 +203,7 @@ export class StoreSchema { if (ourStoreVersion > persistedStoreVersion) { const result = migrate>({ - value: storeSnapshot, + value: snapshot.store, migrations, fromVersion: persistedStoreVersion, toVersion: ourStoreVersion, @@ -215,12 +212,12 @@ export class StoreSchema { if (result.type === 'error') { return result } - storeSnapshot = result.value + snapshot.store = result.value } const updated: R[] = [] - for (const r of objectMapValues(storeSnapshot)) { - const result = this.migratePersistedRecord(r, persistedSchema) + for (const r of objectMapValues(snapshot.store)) { + const result = this.migratePersistedRecord(r, snapshot.schema) if (result.type === 'error') { return result } else if (result.value && result.value !== r) { @@ -228,12 +225,12 @@ export class StoreSchema { } } if (updated.length) { - storeSnapshot = { ...storeSnapshot } + snapshot.store = { ...snapshot.store } for (const r of updated) { - storeSnapshot[r.id as IdOf] = r + snapshot.store[r.id as IdOf] = r } } - return { type: 'success', value: storeSnapshot } + return { type: 'success', value: snapshot.store } } /** @internal */ diff --git a/packages/store/src/lib/test/migrate.test.ts b/packages/store/src/lib/test/migrate.test.ts index 500ef34fe..2095ff8a9 100644 --- a/packages/store/src/lib/test/migrate.test.ts +++ b/packages/store/src/lib/test/migrate.test.ts @@ -305,7 +305,7 @@ test('subtype versions in the future fail', () => { }) test('migrating a whole store snapshot works', () => { - const snapshot: SerializedStore = { + const serializedStore: SerializedStore = { 'user-1': { id: 'user-1', typeName: 'user', @@ -329,7 +329,10 @@ test('migrating a whole store snapshot works', () => { }, } - const result = testSchemaV1.migrateStoreSnapshot(snapshot, serializedV0Schenma) + const result = testSchemaV1.migrateStoreSnapshot({ + store: serializedStore, + schema: serializedV0Schenma, + }) if (result.type !== 'success') { console.error(result)