Skip to content

Commit 1e9047e

Browse files
authored
fix(store-sync): handle pending deleted dynamic value in stash storage adapter (#3714)
1 parent 5f0b4d1 commit 1e9047e

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

.changeset/heavy-pumas-repeat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@latticexyz/store-sync": patch
3+
---
4+
5+
Fixed a bug that caused the stash storage adapter to crash when deleting a dynamic field and writing to it again in the same block.

packages/store-sync/src/stash/createStorageAdapter.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Stash, PendingStashUpdate, TableRecord, applyUpdates, getRecord } from "@latticexyz/stash/internal";
1+
import { Stash, PendingStashUpdate, TableRecord, applyUpdates, getRecord, Key } from "@latticexyz/stash/internal";
22
import {
33
decodeKey,
44
decodeValueArgs,
@@ -31,6 +31,14 @@ export function createStorageAdapter({ stash }: CreateStorageAdapter): StorageAd
3131
const pendingRecords: Record<string, PendingStashUpdate> = {};
3232
const updates: PendingStashUpdate[] = [];
3333

34+
function getPendingRecord(id: string, table: Table, key: Key<Table>): TableRecord | undefined {
35+
return pendingRecords[id]
36+
? pendingRecords[id].value
37+
? ({ ...pendingRecords[id].key, ...pendingRecords[id].value } as TableRecord)
38+
: undefined
39+
: getRecord({ stash, table, key });
40+
}
41+
3442
for (const log of logs) {
3543
const table = tablesById[log.args.tableId];
3644
if (!table) continue;
@@ -45,12 +53,7 @@ export function createStorageAdapter({ stash }: CreateStorageAdapter): StorageAd
4553
const value = decodeValueArgs(valueSchema, log.args);
4654
updates.push((pendingRecords[id] = { table, key, value }));
4755
} else if (log.eventName === "Store_SpliceStaticData") {
48-
const previousValue = pendingRecords[id]
49-
? pendingRecords[id].value
50-
? ({ ...pendingRecords[id].key, ...pendingRecords[id].value } as TableRecord)
51-
: undefined
52-
: getRecord({ stash, table, key });
53-
56+
const previousValue = getPendingRecord(id, table, key);
5457
const {
5558
staticData: previousStaticData,
5659
encodedLengths,
@@ -66,10 +69,7 @@ export function createStorageAdapter({ stash }: CreateStorageAdapter): StorageAd
6669

6770
updates.push((pendingRecords[id] = { table, key, value }));
6871
} else if (log.eventName === "Store_SpliceDynamicData") {
69-
const previousValue = pendingRecords[id]
70-
? ({ ...pendingRecords[id].key, ...pendingRecords[id].value } as TableRecord)
71-
: getRecord({ stash, table, key });
72-
72+
const previousValue = getPendingRecord(id, table, key);
7373
const { staticData, dynamicData: previousDynamicData } = previousValue
7474
? encodeValueArgs(valueSchema, previousValue)
7575
: emptyValueArgs;

0 commit comments

Comments
 (0)