Skip to content

Commit

Permalink
fix(store-sync): track changed records together in zustand (latticexy…
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Mar 7, 2024
1 parent d66e2bc commit 3f5d33a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-students-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/store-sync": patch
---

Fixes an issue with Zustand store sync where multiple updates to a record for a key in the same block did not get tracked and applied properly.
22 changes: 13 additions & 9 deletions packages/store-sync/src/zustand/createStorageAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export function createStorageAdapter<tables extends Tables>({
return async function zustandStorageAdapter({ blockNumber, logs }) {
// TODO: clean this up so that we do one store write per block

const updatedIds: string[] = [];
const deletedIds: string[] = [];
// record id => is deleted
const touchedIds: Map<string, boolean> = new Map();

const rawRecords = { ...store.getState().rawRecords };

Expand Down Expand Up @@ -54,7 +54,7 @@ export function createStorageAdapter<tables extends Tables>({
encodedLengths: log.args.encodedLengths,
dynamicData: log.args.dynamicData,
};
updatedIds.push(id);
touchedIds.set(id, false);
} else if (log.eventName === "Store_SpliceStaticData") {
debug("splicing static data", {
namespace: table.namespace,
Expand All @@ -75,7 +75,7 @@ export function createStorageAdapter<tables extends Tables>({
...previousRecord,
staticData,
};
updatedIds.push(id);
touchedIds.set(id, false);
} else if (log.eventName === "Store_SpliceDynamicData") {
debug("splicing dynamic data", {
namespace: table.namespace,
Expand All @@ -98,7 +98,7 @@ export function createStorageAdapter<tables extends Tables>({
encodedLengths,
dynamicData,
};
updatedIds.push(id);
touchedIds.set(id, false);
} else if (log.eventName === "Store_DeleteRecord") {
debug("deleting record", {
namespace: table.namespace,
Expand All @@ -107,14 +107,18 @@ export function createStorageAdapter<tables extends Tables>({
log,
});
delete rawRecords[id];
deletedIds.push(id);
touchedIds.set(id, true);
}
}

if (!updatedIds.length && !deletedIds.length) return;
if (!touchedIds.size) return;

const records = {
...Object.fromEntries(Object.entries(store.getState().records).filter(([id]) => !deletedIds.includes(id))),
const updatedIds = Array.from(touchedIds.keys()).filter((id) => touchedIds.get(id) === false);
const deletedIds = Array.from(touchedIds.keys()).filter((id) => touchedIds.get(id) === true);

const previousRecords = store.getState().records;
const records: typeof previousRecords = {
...Object.fromEntries(Object.entries(previousRecords).filter(([id]) => !deletedIds.includes(id))),
...Object.fromEntries(
updatedIds
.map((id) => {
Expand Down

0 comments on commit 3f5d33a

Please sign in to comment.