[fix] mutating snapshot
in migrateStoreSnapshot
(#1663)
This PR fixes an issue that I introduced where the `snapshot` argument would be mutated by the `Store.migrateStoreSnapshot` method. ### Change Type - [x] `patch` ### Release Notes - [@tldraw/store] Fixed a bug that would cause `Store.migrateStoreSnapshot` to mutate its `snapshot` argument.
This commit is contained in:
parent
a63b536782
commit
3e07f70440
1 changed files with 9 additions and 7 deletions
|
@ -189,9 +189,11 @@ export class StoreSchema<R extends UnknownRecord, P = unknown> {
|
||||||
}
|
}
|
||||||
|
|
||||||
migrateStoreSnapshot(snapshot: StoreSnapshot<R>): MigrationResult<SerializedStore<R>> {
|
migrateStoreSnapshot(snapshot: StoreSnapshot<R>): MigrationResult<SerializedStore<R>> {
|
||||||
|
let { store } = snapshot
|
||||||
|
|
||||||
const migrations = this.options.snapshotMigrations
|
const migrations = this.options.snapshotMigrations
|
||||||
if (!migrations) {
|
if (!migrations) {
|
||||||
return { type: 'success', value: snapshot.store }
|
return { type: 'success', value: store }
|
||||||
}
|
}
|
||||||
// apply store migrations first
|
// apply store migrations first
|
||||||
const ourStoreVersion = migrations.currentVersion
|
const ourStoreVersion = migrations.currentVersion
|
||||||
|
@ -203,7 +205,7 @@ export class StoreSchema<R extends UnknownRecord, P = unknown> {
|
||||||
|
|
||||||
if (ourStoreVersion > persistedStoreVersion) {
|
if (ourStoreVersion > persistedStoreVersion) {
|
||||||
const result = migrate<SerializedStore<R>>({
|
const result = migrate<SerializedStore<R>>({
|
||||||
value: snapshot.store,
|
value: store,
|
||||||
migrations,
|
migrations,
|
||||||
fromVersion: persistedStoreVersion,
|
fromVersion: persistedStoreVersion,
|
||||||
toVersion: ourStoreVersion,
|
toVersion: ourStoreVersion,
|
||||||
|
@ -212,11 +214,11 @@ export class StoreSchema<R extends UnknownRecord, P = unknown> {
|
||||||
if (result.type === 'error') {
|
if (result.type === 'error') {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
snapshot.store = result.value
|
store = result.value
|
||||||
}
|
}
|
||||||
|
|
||||||
const updated: R[] = []
|
const updated: R[] = []
|
||||||
for (const r of objectMapValues(snapshot.store)) {
|
for (const r of objectMapValues(store)) {
|
||||||
const result = this.migratePersistedRecord(r, snapshot.schema)
|
const result = this.migratePersistedRecord(r, snapshot.schema)
|
||||||
if (result.type === 'error') {
|
if (result.type === 'error') {
|
||||||
return result
|
return result
|
||||||
|
@ -225,12 +227,12 @@ export class StoreSchema<R extends UnknownRecord, P = unknown> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (updated.length) {
|
if (updated.length) {
|
||||||
snapshot.store = { ...snapshot.store }
|
store = { ...store }
|
||||||
for (const r of updated) {
|
for (const r of updated) {
|
||||||
snapshot.store[r.id as IdOf<R>] = r
|
store[r.id as IdOf<R>] = r
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return { type: 'success', value: snapshot.store }
|
return { type: 'success', value: store }
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @internal */
|
/** @internal */
|
||||||
|
|
Loading…
Reference in a new issue