Skip to content

Commit

Permalink
update stash storage adapter to apply updates once
Browse files Browse the repository at this point in the history
  • Loading branch information
frolic committed Nov 11, 2024
1 parent ff56a0e commit 35f1941
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/stash/src/actions/applyUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { encodeKey } from "./encodeKey";
import { Table } from "@latticexyz/config";
import { registerTable } from "./registerTable";

export type Update<table extends Table = Table> = {
export type StashUpdate<table extends Table = Table> = {
table: table;
key: Key<table>;
value: undefined | Partial<TableRecord<table>>;
};

export type ApplyUpdatesArgs = {
stash: Stash;
updates: Update[];
updates: StashUpdate[];
};

const pendingUpdates = new Map<Stash, StoreUpdates>();
Expand Down
30 changes: 22 additions & 8 deletions packages/store-sync/src/stash/createStorageAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Stash, deleteRecord, getRecord, setRecord } from "@latticexyz/stash/internal";
import { Stash, StashUpdate, TableRecord, applyUpdates, getRecord } from "@latticexyz/stash/internal";
import {
decodeKey,
decodeValueArgs,
Expand All @@ -8,7 +8,7 @@ import {
getValueSchema,
} from "@latticexyz/protocol-parser/internal";
import { spliceHex } from "@latticexyz/common";
import { size } from "viem";
import { Hex, concatHex, size } from "viem";
import { Table } from "@latticexyz/config";
import { StorageAdapter, StorageAdapterBlock, emptyValueArgs } from "../common";

Expand All @@ -23,20 +23,30 @@ export function createStorageAdapter({ stash }: CreateStorageAdapter): StorageAd
.map((table) => [table.tableId, table]),
);

function getUpdateId(tableId: Hex, keyTuple: readonly Hex[]): string {
return `${tableId}:${concatHex(keyTuple)}`;
}

return async function storageAdapter({ logs }: StorageAdapterBlock): Promise<void> {
const updates: Record<string, StashUpdate> = {};

for (const log of logs) {
const table = tablesById[log.args.tableId];
if (!table) continue;

const id = getUpdateId(log.args.tableId, log.args.keyTuple);

const valueSchema = getSchemaTypes(getValueSchema(table));
const keySchema = getSchemaTypes(getKeySchema(table));
const key = decodeKey(keySchema, log.args.keyTuple);

if (log.eventName === "Store_SetRecord") {
const value = decodeValueArgs(valueSchema, log.args);
setRecord({ stash, table, key, value });
updates[id] = { table, key, value };
} else if (log.eventName === "Store_SpliceStaticData") {
const previousValue = getRecord({ stash, table, key });
const previousValue = updates[id]
? ({ ...updates[id].key, ...updates[id].value } as TableRecord)
: getRecord({ stash, table, key });

const {
staticData: previousStaticData,
Expand All @@ -51,9 +61,11 @@ export function createStorageAdapter({ stash }: CreateStorageAdapter): StorageAd
dynamicData,
});

setRecord({ stash, table, key, value });
updates[id] = { table, key, value };
} else if (log.eventName === "Store_SpliceDynamicData") {
const previousValue = getRecord({ stash, table, key });
const previousValue = updates[id]
? ({ ...updates[id].key, ...updates[id].value } as TableRecord)
: getRecord({ stash, table, key });

const { staticData, dynamicData: previousDynamicData } = previousValue
? encodeValueArgs(valueSchema, previousValue)
Expand All @@ -66,10 +78,12 @@ export function createStorageAdapter({ stash }: CreateStorageAdapter): StorageAd
dynamicData,
});

setRecord({ stash, table, key, value });
updates[id] = { table, key, value };
} else if (log.eventName === "Store_DeleteRecord") {
deleteRecord({ stash, table, key });
updates[id] = { table, key, value: undefined };
}
}

applyUpdates({ stash, updates: Object.values(updates) });
};
}

0 comments on commit 35f1941

Please sign in to comment.