-
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.
- Loading branch information
Showing
6 changed files
with
58 additions
and
27 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
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,30 @@ | ||
import { satisfy, show } from "@arktype/util"; | ||
import { Tables } from "@latticexyz/config"; | ||
import { Store } from "@latticexyz/store"; | ||
|
||
type flattenedTables<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 | ||
// TODO: move satisfy to type test | ||
|
||
export type configToTables<config extends Store> = satisfy< | ||
Tables, | ||
{ | ||
readonly [key in flattenedTables<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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,31 @@ | ||
import { Store as StoreConfig } from "@latticexyz/store"; | ||
import { Tables } from "@latticexyz/config"; | ||
import { tablesByLabel } from "./tablesByLabel"; | ||
import { mergeRight } from "./mergeRight"; | ||
import storeConfig from "@latticexyz/store/mud.config"; | ||
import worldConfig from "@latticexyz/world/mud.config"; | ||
import { configToTables } from "./configToTables"; | ||
import { satisfy, show } from "@arktype/util"; | ||
|
||
const storeTables = storeConfig.tables; | ||
type storeTables = typeof storeTables; | ||
type mudTables = mergeRight<configToTables<typeof storeConfig>, configToTables<typeof worldConfig>>; | ||
const mudTables = { | ||
...configToTables(storeConfig), | ||
...configToTables(worldConfig), | ||
}; | ||
|
||
const worldTables = worldConfig.tables; | ||
type worldTables = typeof worldTables; | ||
|
||
export type getAllTables<config extends StoreConfig, extraTables extends Tables> = tablesByLabel< | ||
mergeRight<config["tables"], mergeRight<extraTables, mergeRight<storeTables, worldTables>>> | ||
// TODO: validate that extraTables keys correspond to table labels? | ||
// TODO: move satisfy to type test | ||
export type getAllTables<config extends StoreConfig, extraTables extends Tables> = satisfy< | ||
Tables, | ||
mergeRight<configToTables<config>, mergeRight<extraTables, mudTables>> | ||
>; | ||
|
||
export function getAllTables<config extends StoreConfig, extraTables extends Tables>( | ||
config: config, | ||
extraTables: extraTables, | ||
): getAllTables<config, extraTables> { | ||
return tablesByLabel({ | ||
...config.tables, | ||
): show<getAllTables<config, extraTables>> { | ||
return { | ||
...configToTables(config), | ||
...extraTables, | ||
...storeTables, | ||
...worldTables, | ||
}) as never; | ||
...mudTables, | ||
} as never; | ||
} |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.