-
Notifications
You must be signed in to change notification settings - Fork 197
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): add react provider and hook (#3451)
- Loading branch information
Showing
14 changed files
with
274 additions
and
65 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,31 @@ | ||
--- | ||
"@latticexyz/store-sync": patch | ||
--- | ||
|
||
Added an experimental `@latticexyz/store-sync/react` export with a `SyncProvider` and `useSync` hook. This allows for easier syncing MUD data to React apps. | ||
|
||
Note that this is currently only usable with Stash and assumes you are also using Wagmi in your React app. | ||
|
||
```tsx | ||
import { WagmiProvider } from "wagmi"; | ||
import { QueryClientProvider } from "@tanstack/react-query"; | ||
import { SyncProvider } from "@latticexyz/store-sync/react"; | ||
import { createSyncAdapter } from "@latticexyz/store-sync/internal"; | ||
|
||
export function App() { | ||
return ( | ||
<WagmiProvider config={wagmiConfig}> | ||
<QueryClientProvider client={queryClient}> | ||
<SyncProvider | ||
chainId={chainId} | ||
address={worldAddress} | ||
startBlock={startBlock} | ||
adapter={createSyncAdapter({ stash })} | ||
> | ||
{children} | ||
</SyncProvider> | ||
</QueryClientProvider> | ||
</WagmiProvider> | ||
); | ||
} | ||
``` |
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 |
---|---|---|
@@ -1,2 +1,8 @@ | ||
// SQL | ||
export * from "../sql"; | ||
export * from "../stash"; | ||
|
||
// Stash | ||
export * from "../stash/common"; | ||
export * from "../stash/createStorageAdapter"; | ||
export * from "../stash/createSyncAdapter"; | ||
export * from "../stash/syncToStash"; |
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,2 @@ | ||
export * from "../react/SyncProvider"; | ||
export * from "../react/useSync"; |
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,57 @@ | ||
import { ReactNode, createContext, useContext, useEffect } from "react"; | ||
import { useConfig } from "wagmi"; | ||
import { getClient } from "wagmi/actions"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { SyncAdapter, SyncOptions, SyncResult } from "../common"; | ||
|
||
/** @internal */ | ||
export const SyncContext = createContext<{ | ||
sync?: SyncResult; | ||
} | null>(null); | ||
|
||
export type Props = Omit<SyncOptions, "publicClient"> & { | ||
chainId: number; | ||
adapter: SyncAdapter; | ||
children: ReactNode; | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
export function SyncProvider({ chainId, adapter, children, ...syncOptions }: Props) { | ||
const existingValue = useContext(SyncContext); | ||
if (existingValue != null) { | ||
throw new Error("A `SyncProvider` cannot be nested inside another."); | ||
} | ||
|
||
const config = useConfig(); | ||
|
||
const { data: sync, error: syncError } = useQuery({ | ||
queryKey: ["sync", chainId], | ||
queryFn: async () => { | ||
const client = getClient(config, { chainId }); | ||
if (!client) { | ||
throw new Error(`Unable to retrieve Viem client for chain ${chainId}.`); | ||
} | ||
|
||
return adapter({ publicClient: client, ...syncOptions }); | ||
}, | ||
staleTime: Infinity, | ||
refetchOnMount: false, | ||
refetchOnWindowFocus: false, | ||
refetchOnReconnect: false, | ||
}); | ||
if (syncError) throw syncError; | ||
|
||
useEffect(() => { | ||
if (!sync) return; | ||
|
||
const sub = sync.storedBlockLogs$.subscribe({ | ||
error: (error) => console.error("got sync error", error), | ||
}); | ||
|
||
return (): void => { | ||
sub.unsubscribe(); | ||
}; | ||
}, [sync]); | ||
|
||
return <SyncContext.Provider value={{ sync }}>{children}</SyncContext.Provider>; | ||
} |
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,12 @@ | ||
import { useContext } from "react"; | ||
import { SyncContext } from "./SyncProvider"; | ||
import { SyncResult } from "../common"; | ||
|
||
export function useSync(): Partial<SyncResult> { | ||
const value = useContext(SyncContext); | ||
if (value == null) { | ||
throw new Error("`useSync` must be used inside a `SyncProvider`."); | ||
} | ||
const { sync } = value; | ||
return sync ?? {}; | ||
} |
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,24 @@ | ||
import { defineTable } from "@latticexyz/store/internal"; | ||
import { SyncStep } from "../SyncStep"; | ||
import { getSchemaPrimitives, getValueSchema } from "@latticexyz/protocol-parser/internal"; | ||
|
||
export const SyncProgress = defineTable({ | ||
namespaceLabel: "syncToStash", | ||
label: "SyncProgress", | ||
schema: { | ||
step: "string", | ||
percentage: "uint32", | ||
latestBlockNumber: "uint256", | ||
lastBlockNumberProcessed: "uint256", | ||
message: "string", | ||
}, | ||
key: [], | ||
}); | ||
|
||
export const initialProgress = { | ||
step: SyncStep.INITIALIZE, | ||
percentage: 0, | ||
latestBlockNumber: 0n, | ||
lastBlockNumberProcessed: 0n, | ||
message: "Connecting", | ||
} satisfies getSchemaPrimitives<getValueSchema<typeof SyncProgress>>; |
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,30 @@ | ||
import { getRecord, setRecord, registerTable, Stash } from "@latticexyz/stash/internal"; | ||
import { createStorageAdapter } from "./createStorageAdapter"; | ||
import { SyncStep } from "../SyncStep"; | ||
import { SyncAdapter } from "../common"; | ||
import { createStoreSync } from "../createStoreSync"; | ||
import { SyncProgress } from "./common"; | ||
|
||
export type CreateSyncAdapterOptions = { stash: Stash }; | ||
|
||
export function createSyncAdapter({ stash }: CreateSyncAdapterOptions): SyncAdapter { | ||
return (opts) => { | ||
// TODO: clear stash? | ||
|
||
registerTable({ stash, table: SyncProgress }); | ||
|
||
const storageAdapter = createStorageAdapter({ stash }); | ||
|
||
return createStoreSync({ | ||
...opts, | ||
storageAdapter, | ||
onProgress: (nextValue) => { | ||
const currentValue = getRecord({ stash, table: SyncProgress, key: {} }); | ||
// update sync progress until we're caught up and live | ||
if (currentValue?.step !== SyncStep.LIVE) { | ||
setRecord({ stash, table: SyncProgress, key: {}, value: nextValue }); | ||
} | ||
}, | ||
}); | ||
}; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist" | ||
"outDir": "dist", | ||
"jsx": "react-jsx", | ||
"jsxImportSource": "react" | ||
} | ||
} |
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
Oops, something went wrong.