Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(store-sync): track changed records together in zustand #2387

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
always track touched IDs
  • Loading branch information
holic committed Mar 7, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 755086d3d17741e2b7fecdc624a979ad8e837e47
22 changes: 13 additions & 9 deletions packages/store-sync/src/zustand/createStorageAdapter.ts
Original file line number Diff line number Diff line change
@@ -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 };

@@ -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,
@@ -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,
@@ -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,
@@ -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) => {
Loading