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(store-sync): sync to zustand #1843

Merged
merged 23 commits into from
Nov 2, 2023
Merged

feat(store-sync): sync to zustand #1843

merged 23 commits into from
Nov 2, 2023

Conversation

holic
Copy link
Member

@holic holic commented Oct 30, 2023

see changeset

@changeset-bot
Copy link

changeset-bot bot commented Oct 30, 2023

🦋 Changeset detected

Latest commit: fbda87f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 30 packages
Name Type
@latticexyz/store-sync Major
@latticexyz/dev-tools Major
@latticexyz/store-indexer Major
@latticexyz/abi-ts Major
@latticexyz/block-logs-stream Major
@latticexyz/cli Major
@latticexyz/common Major
@latticexyz/config Major
create-mud Major
@latticexyz/ecs-browser Major
@latticexyz/faucet Major
@latticexyz/gas-report Major
@latticexyz/network Major
@latticexyz/noise Major
@latticexyz/phaserx Major
@latticexyz/protocol-parser Major
@latticexyz/react Major
@latticexyz/recs Major
@latticexyz/schema-type Major
@latticexyz/services Major
@latticexyz/solecs Major
solhint-config-mud Major
solhint-plugin-mud Major
@latticexyz/std-client Major
@latticexyz/std-contracts Major
@latticexyz/store-cache Major
@latticexyz/store Major
@latticexyz/utils Major
@latticexyz/world-modules Major
@latticexyz/world Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@holic holic changed the title WIP sync to zustand feat(store-sync): sync to zustand Nov 1, 2023
tables: {},
rawRecords: {},
records: {},
getRecords: <table extends Table>(table: table): TableRecords<table> => {
Copy link
Member

Choose a reason for hiding this comment

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

ok maybe i'm slowly getting used to table extends Table

Comment on lines 59 to 65
const records = Object.values(get().records);
const expectedKeyTuple = encodeKey(flattenSchema(table.keySchema), key);
const record = records.find(
(record) =>
record.table.tableId === table.tableId && concatHex([...record.keyTuple]) === concatHex(expectedKeyTuple)
);
return record as TableRecord<table> | undefined;
Copy link
Member

Choose a reason for hiding this comment

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

i'm a little bit scared of performance of finding records via linear search if the table has a lot of entries (maybe not the use case of the zustand store? but still).

Couldn't we make the id just the concatenated tableId + key to be able to look it up immediately? What are we solving for by adding the address? (which i think is the reason why we can't just look up the record by id here correct?) If we had records from multiple worlds in here, then I think the current approach would just return the first record independent of the address part of its id?

Copy link
Member Author

Choose a reason for hiding this comment

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

I considered this, but all of the existing sync adapters support an optional address (i.e. either one world or all worlds), so this was here to keep compatibility/predictability. We could make address a required param for syncToZustand and then get rid of the address as part of the ID/object key and that would make look ups faster.

Now that I am writing this, I am realizing that even though we key on address, we don't actually return it anywhere or allow filtering on it in getRecord, getRecords, etc. So maybe it is best if we make this a required param for now, and figure out how to make it optional/multi-world later.

Another thing on my mind is potentially keeping the top-level keys (records, tables, etc.) "out of view" of downstream consumers and forcing them to use things like getRecord and getRecords so we can modify the underlying state representation.

alvrs
alvrs previously approved these changes Nov 1, 2023
Copy link
Member

@alvrs alvrs left a comment

Choose a reason for hiding this comment

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

comments are not blocking but i'm curious about the id/getRecord approach

@holic holic merged commit fa77635 into main Nov 2, 2023
9 checks passed
@holic holic deleted the holic/sync-to-zustand branch November 2, 2023 11:57
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