-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store-sync): sync to zustand (#1843)
- Loading branch information
Showing
29 changed files
with
649 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
"@latticexyz/store-sync": minor | ||
--- | ||
|
||
Added a Zustand storage adapter and corresponding `syncToZustand` method for use in vanilla and React apps. It's used much like the other sync methods, except it returns a bound store and set of typed tables. | ||
|
||
```ts | ||
import { syncToZustand } from "@latticexyz/store-sync/zustand"; | ||
import config from "contracts/mud.config"; | ||
|
||
const { tables, useStore, latestBlock$, storedBlockLogs$, waitForTransaction } = await syncToZustand({ | ||
config, | ||
... | ||
}); | ||
|
||
// in vanilla apps | ||
const positions = useStore.getState().getRecords(tables.Position); | ||
|
||
// in React apps | ||
const positions = useStore((state) => state.getRecords(tables.Position)); | ||
``` | ||
|
||
This change will be shortly followed by an update to our templates that uses Zustand as the default client data store and sync method. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
examples/minimal/packages/client-vanilla/src/mud/createClientComponents.ts
This file was deleted.
Oops, something went wrong.
10 changes: 2 additions & 8 deletions
10
examples/minimal/packages/client-vanilla/src/mud/createSystemCalls.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,13 @@ | ||
import { createClientComponents } from "./createClientComponents"; | ||
import { createSystemCalls } from "./createSystemCalls"; | ||
import { setupNetwork } from "./setupNetwork"; | ||
|
||
export type SetupResult = Awaited<ReturnType<typeof setup>>; | ||
|
||
export async function setup() { | ||
const network = await setupNetwork(); | ||
const components = createClientComponents(network); | ||
const systemCalls = createSystemCalls(network, components); | ||
const systemCalls = createSystemCalls(network); | ||
return { | ||
network, | ||
components, | ||
systemCalls, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { mapObject } from "@latticexyz/common/utils"; | ||
import { ValueSchema } from "@latticexyz/store"; | ||
|
||
export function flattenSchema<schema extends ValueSchema>( | ||
schema: schema | ||
): { readonly [k in keyof schema]: schema[k]["type"] } { | ||
return mapObject(schema, (value) => value.type); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { StorageAdapterLog, schemasTableId } from "./common"; | ||
import { StorageAdapterLog, storeTables } from "./common"; | ||
|
||
export function isTableRegistrationLog( | ||
log: StorageAdapterLog | ||
): log is StorageAdapterLog & { eventName: "Store_SetRecord" } { | ||
return log.eventName === "Store_SetRecord" && log.args.tableId === schemasTableId; | ||
return log.eventName === "Store_SetRecord" && log.args.tableId === storeTables.Tables.tableId; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Table, SchemaToPrimitives } from "@latticexyz/store"; | ||
import { Hex } from "viem"; | ||
|
||
export type RawRecord = { | ||
/** Internal unique ID */ | ||
readonly id: string; | ||
readonly tableId: Hex; | ||
readonly keyTuple: readonly Hex[]; | ||
readonly staticData: Hex; | ||
readonly encodedLengths: Hex; | ||
readonly dynamicData: Hex; | ||
}; | ||
|
||
export type TableRecord<table extends Table> = { | ||
/** Internal unique ID */ | ||
readonly id: string; | ||
readonly table: table; | ||
readonly keyTuple: readonly Hex[]; | ||
readonly key: SchemaToPrimitives<table["keySchema"]>; | ||
readonly value: SchemaToPrimitives<table["valueSchema"]>; | ||
}; |
71 changes: 71 additions & 0 deletions
71
packages/store-sync/src/zustand/createStorageAdapter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import mudConfig from "../../../../e2e/packages/contracts/mud.config"; | ||
import worldRpcLogs from "../../../../test-data/world-logs.json"; | ||
import { groupLogsByBlockNumber } from "@latticexyz/block-logs-stream"; | ||
import { StoreEventsLog } from "../common"; | ||
import { RpcLog, formatLog, decodeEventLog, Hex } from "viem"; | ||
import { resolveConfig, storeEventsAbi } from "@latticexyz/store"; | ||
import { createStorageAdapter } from "./createStorageAdapter"; | ||
import { createStore } from "./createStore"; | ||
|
||
const tables = resolveConfig(mudConfig).tables; | ||
|
||
// TODO: make test-data a proper package and export this | ||
const blocks = groupLogsByBlockNumber( | ||
worldRpcLogs.map((log) => { | ||
const { eventName, args } = decodeEventLog({ | ||
abi: storeEventsAbi, | ||
data: log.data as Hex, | ||
topics: log.topics as [Hex, ...Hex[]], | ||
strict: true, | ||
}); | ||
return formatLog(log as unknown as RpcLog, { args, eventName: eventName as string }) as StoreEventsLog; | ||
}) | ||
); | ||
|
||
describe("createStorageAdapter", () => { | ||
it("sets component values from logs", async () => { | ||
const useStore = createStore({ tables }); | ||
const storageAdapter = createStorageAdapter({ store: useStore }); | ||
|
||
for (const block of blocks) { | ||
await storageAdapter(block); | ||
} | ||
|
||
expect(useStore.getState().getRecords(tables.NumberList)).toMatchInlineSnapshot(` | ||
{ | ||
"0x746200000000000000000000000000004e756d6265724c697374000000000000:0x": { | ||
"id": "0x746200000000000000000000000000004e756d6265724c697374000000000000:0x", | ||
"key": {}, | ||
"keyTuple": [], | ||
"table": { | ||
"keySchema": {}, | ||
"name": "NumberList", | ||
"namespace": "", | ||
"tableId": "0x746200000000000000000000000000004e756d6265724c697374000000000000", | ||
"valueSchema": { | ||
"value": { | ||
"type": "uint32[]", | ||
}, | ||
}, | ||
}, | ||
"value": { | ||
"value": [ | ||
420, | ||
69, | ||
], | ||
}, | ||
}, | ||
} | ||
`); | ||
|
||
expect(useStore.getState().getValue(tables.NumberList, {})).toMatchInlineSnapshot(` | ||
{ | ||
"value": [ | ||
420, | ||
69, | ||
], | ||
} | ||
`); | ||
}); | ||
}); |
Oops, something went wrong.