-
Notifications
You must be signed in to change notification settings - Fork 196
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
feat(dev-tools): use new sync stack #1284
Changes from 8 commits
9270b98
ded937a
bc7f7a6
663dab5
c7334e0
199b792
71b6e5f
7f0eed5
3bc236d
29fb6f0
5de6df4
0c67371
3afbb96
d4ec24c
253e08a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,8 @@ import { getNetworkConfig } from "./getNetworkConfig"; | |
import { world } from "./world"; | ||
import { IWorld__factory } from "contracts/types/ethers-contracts/factories/IWorld__factory"; | ||
import storeConfig from "contracts/mud.config"; | ||
import { createBurnerAccount, createContract, transportObserver } from "@latticexyz/common"; | ||
import { ContractWrite, createBurnerAccount, createContract, transportObserver } from "@latticexyz/common"; | ||
import { Subject, share } from "rxjs"; | ||
|
||
export type SetupNetworkResult = Awaited<ReturnType<typeof setupNetwork>>; | ||
|
||
|
@@ -26,6 +27,15 @@ export async function setupNetwork() { | |
account: burnerAccount, | ||
}); | ||
|
||
const write$ = new Subject<ContractWrite>(); | ||
const worldContract = createContract({ | ||
address: networkConfig.worldAddress as Hex, | ||
abi: IWorld__factory.abi, | ||
publicClient, | ||
walletClient: burnerWalletClient, | ||
onWrite: (write) => write$.next(write), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wondering if we wanna export something like downside is that common will need rxjs as a dep, which is quite heavy There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i like the lightweight |
||
}); | ||
|
||
const { components, latestBlock$, blockStorageOperations$, waitForTransaction } = await syncToRecs({ | ||
world, | ||
config: storeConfig, | ||
|
@@ -67,11 +77,7 @@ export async function setupNetwork() { | |
latestBlock$, | ||
blockStorageOperations$, | ||
waitForTransaction, | ||
worldContract: createContract({ | ||
address: networkConfig.worldAddress as Hex, | ||
abi: IWorld__factory.abi, | ||
publicClient, | ||
walletClient: burnerWalletClient, | ||
}), | ||
worldContract, | ||
write$: write$.asObservable().pipe(share()), | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { createContext, ReactNode, useContext, useEffect, useState } from "react"; | ||
import { DevToolsOptions } from "./common"; | ||
import { ContractWrite } from "@latticexyz/common"; | ||
import { StorageOperation } from "@latticexyz/store-sync"; | ||
import { StoreConfig } from "@latticexyz/store"; | ||
|
||
type DevToolsContextValue = DevToolsOptions & { | ||
writes: ContractWrite[]; | ||
storageOperations: StorageOperation<StoreConfig>[]; | ||
}; | ||
|
||
const DevToolsContext = createContext<DevToolsContextValue | null>(null); | ||
|
||
type Props = { | ||
children: ReactNode; | ||
value: DevToolsOptions; | ||
}; | ||
|
||
export const DevToolsProvider = ({ children, value }: Props) => { | ||
const currentValue = useContext(DevToolsContext); | ||
if (currentValue) throw new Error("DevToolsProvider can only be used once"); | ||
|
||
const [writes, setWrites] = useState<ContractWrite[]>([]); | ||
useEffect(() => { | ||
const sub = value.write$.subscribe((write) => { | ||
setWrites((val) => [...val, write]); | ||
}); | ||
return () => sub.unsubscribe(); | ||
}, [value.write$]); | ||
|
||
const [storageOperations, setStorageOperations] = useState<StorageOperation<StoreConfig>[]>([]); | ||
useEffect(() => { | ||
const sub = value.blockStorageOperations$.subscribe(({ operations }) => { | ||
setStorageOperations((val) => [...val, ...operations]); | ||
}); | ||
return () => sub.unsubscribe(); | ||
}, [value.blockStorageOperations$]); | ||
|
||
return ( | ||
<DevToolsContext.Provider | ||
value={{ | ||
...value, | ||
writes, | ||
storageOperations, | ||
}} | ||
> | ||
{children} | ||
</DevToolsContext.Provider> | ||
); | ||
}; | ||
|
||
export const useDevToolsContext = () => { | ||
const value = useContext(DevToolsContext); | ||
if (!value) throw new Error("Must be used within a DevToolsProvider"); | ||
return value; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alvrs thoughts on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks fine, maybe add a comment linking to https://vitejs.dev/guide/env-and-mode.html near
import.meta.env.DEV
?