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

feat: sync data to store cache, decode keys #808

Merged
merged 47 commits into from
May 15, 2023
Merged

Conversation

alvrs
Copy link
Member

@alvrs alvrs commented May 12, 2023

@@ -42,6 +42,7 @@
"@latticexyz/recs": "workspace:*",
"@latticexyz/solecs": "workspace:*",
"@latticexyz/store": "workspace:*",
"@latticexyz/store-cache": "workspace:^",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what command are you using to add this? wondering if we should just make them all ^

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this happens when i do pnpm add @latticexyz/store-cache

@@ -7,17 +7,19 @@ import { world } from "./world";
import { Contract, Signer, utils } from "ethers";
import { JsonRpcProvider } from "@ethersproject/providers";
import { IWorld__factory } from "contracts/types/ethers-contracts/factories/IWorld__factory";
import storeConfig from "../../../contracts/mud.config";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might break vercel deployments because it's outside the client directory. Is there an easy workaround?

Copy link
Member

@frolic frolic May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you import storeConfig from "contracts/mud.config"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that worked!

@alvrs alvrs marked this pull request as ready for review May 15, 2023 16:36
Comment on lines +20 to +23
useEffect(() => {
storeCache.tables.Inventory.subscribe((update) => console.log("got update from inventory table", update));
}, [storeCache]);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

planning to add a better example for this in a follow up once we have a scan support to get multiple rows of a table

@alvrs alvrs changed the title feat: WIP sync data to store cache feat: sync data to store cache, decode keys May 15, 2023
@alvrs alvrs requested a review from frolic May 15, 2023 17:07

export default mudConfig({
namespace: "",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this the default?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it is, good catch! added this in some intermediate version where importing mudConfig didn't work yet

primaryKeys: {
user: "address",
item: "bytes32",
variant: "uint32",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is variant?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a random example of a uint32 key tuple (though of a variant of some item, like "red cup" vs "green cup", lemme know if you can think of a better example haha)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh, in that case, itemVariant might be a little more descriptive

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gonna revisit this in an immediate followup! (the example itself is not very expressive yet)

@@ -6,7 +6,7 @@ import { IStore } from "@latticexyz/store/types/ethers-contracts/IStore.sol/ISto

// worldAddress:tableId => schema
// TODO: add chain ID to namespace?
const schemaCache: Partial<Record<`${string}:${string}`, Promise<TableSchema>>> = {};
const schemaCache: Record<`${string}:${string}`, Promise<TableSchema>> = {};
Copy link
Member

@frolic frolic May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason this changed?

I tend to use Partial<Record<...>> for any object that I'm filling in over time, otherwise the types don't reflect that a key can sometimes not have a value (missing from object)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep agree this should be Partial!

// defined in the local mud config)
// (see https://github.com/latticexyz/mud/issues/824)

// `Object.getOwnPropertyNames` guarantees key order, `Object.keys` does not
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL

frolic
frolic previously approved these changes May 15, 2023
@alvrs alvrs merged commit 872fc73 into main May 15, 2023
@frolic frolic deleted the alvrs/sync-store-cache branch June 23, 2023 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants