-
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.
refactor(store-sync): use config namespaces for tables (#2963)
Co-authored-by: alvrs <[email protected]>
- Loading branch information
Showing
29 changed files
with
205 additions
and
197 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,20 @@ | ||
--- | ||
"@latticexyz/store-sync": patch | ||
--- | ||
|
||
Refactored `syncToRecs` and `syncToZustand` to use tables from config namespaces output. This is a precursor for supporting multiple namespaces. | ||
|
||
Note for library authors: If you were using `createStorageAdapter` from `@latticexyz/store-sync/recs`, this helper no longer appends MUD's built-in tables from Store and World packages. This behavior was moved into `syncToRecs` for consistency with `syncToZustand` and makes `createStorageAdapter` less opinionated. | ||
|
||
You can achieve the previous behavior with: | ||
|
||
```diff | ||
import { createStorageAdapter } from "@latticexyz/store-sync/recs"; | ||
+import { mudTables } from "@latticexyz/store-sync"; | ||
|
||
createStorageAdapter({ | ||
- tables, | ||
+ tables: { ...tables, ...mudTables }, | ||
... | ||
}); | ||
``` |
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,12 @@ | ||
import { describe, it } from "vitest"; | ||
import { attest } from "@arktype/attest"; | ||
import { isDisjoint } from "@arktype/util"; | ||
import storeConfig from "@latticexyz/store/mud.config"; | ||
import worldConfig from "@latticexyz/world/mud.config"; | ||
import { configToTables } from "./configToTables"; | ||
|
||
describe("mudTables", () => { | ||
it("has no overlapping table labels", () => { | ||
attest<true, isDisjoint<keyof configToTables<typeof storeConfig>, keyof configToTables<typeof worldConfig>>>(); | ||
}); | ||
}); |
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,44 @@ | ||
import { describe, it } from "vitest"; | ||
import { configToTables } from "./configToTables"; | ||
import { defineWorld } from "@latticexyz/world"; | ||
import { resourceToHex } from "@latticexyz/common"; | ||
import { attest } from "@arktype/attest"; | ||
|
||
describe("configToTables", () => { | ||
it("flattens tables from single namespace", async () => { | ||
const config = defineWorld({ | ||
namespace: "app", | ||
tables: { | ||
ExceedsResourceNameSizeLimit: "bytes32", | ||
Table2: "address", | ||
}, | ||
}); | ||
|
||
const tables = configToTables(config); | ||
|
||
attest<"ExceedsResourceNameSizeLimit" | "Table2", keyof typeof tables>(); | ||
|
||
attest(tables.ExceedsResourceNameSizeLimit.tableId).equals( | ||
resourceToHex({ | ||
type: "table", | ||
namespace: "app", | ||
name: "ExceedsResourceN", | ||
}), | ||
); | ||
attest(tables.ExceedsResourceNameSizeLimit.label).equals("ExceedsResourceNameSizeLimit"); | ||
attest(tables.ExceedsResourceNameSizeLimit.name).equals("ExceedsResourceN"); | ||
|
||
attest(tables.Table2.tableId).equals( | ||
resourceToHex({ | ||
type: "table", | ||
namespace: "app", | ||
name: "Table2", | ||
}), | ||
); | ||
attest(tables.Table2.label).equals("Table2"); | ||
attest(tables.Table2.name).equals("Table2"); | ||
}); | ||
|
||
// TODO: add test with multiple namespaces | ||
// TODO: add test where the label is the same for two tables in different namespaces to make sure TS + runtime agree on which takes precedence | ||
}); |
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,25 @@ | ||
import { show } from "@arktype/util"; | ||
import { Store } from "@latticexyz/store"; | ||
|
||
type flattenedTableKeys<config extends Store> = config extends { readonly namespaces: infer namespaces } | ||
? { | ||
[namespaceLabel in keyof namespaces]: namespaces[namespaceLabel] extends { readonly tables: infer tables } | ||
? `${namespaceLabel & string}__${keyof tables & string}` | ||
: never; | ||
}[keyof namespaces] | ||
: never; | ||
|
||
// TODO: figure out how TS handles overlapping table labels so we can make runtime match | ||
|
||
export type configToTables<config extends Store> = { | ||
readonly [key in flattenedTableKeys<config> as key extends `${string}__${infer tableLabel}` | ||
? tableLabel | ||
: never]: key extends `${infer namespaceLabel}__${infer tableLabel}` | ||
? config["namespaces"][namespaceLabel]["tables"][tableLabel] | ||
: never; | ||
}; | ||
|
||
export function configToTables<config extends Store>(config: config): show<configToTables<config>> { | ||
const tables = Object.values(config.namespaces).flatMap((namespace) => Object.values(namespace.tables)); | ||
return Object.fromEntries(tables.map((table) => [table.label, table])) as never; | ||
} |
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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.