-
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.
feat(store-sync): additional table definitions
- Loading branch information
Showing
8 changed files
with
175 additions
and
75 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
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,45 +1,34 @@ | ||
import { StoreConfig } from "@latticexyz/store"; | ||
import { SchemaAbiType } from "@latticexyz/schema-type"; | ||
import { StoreConfig, resolveUserTypes } from "@latticexyz/store"; | ||
import { resourceToHex } from "@latticexyz/common"; | ||
import { World, defineComponent, Type } from "@latticexyz/recs"; | ||
import { World } from "@latticexyz/recs"; | ||
import { ConfigToRecsComponents } from "./common"; | ||
import { schemaAbiTypeToRecsType } from "./schemaAbiTypeToRecsType"; | ||
import { tableToRecsComponent } from "./tableToRecsComponent"; | ||
import { KeySchema } from "@latticexyz/protocol-parser"; | ||
|
||
export function configToRecsComponents<TConfig extends StoreConfig>( | ||
world: World, | ||
config: TConfig | ||
): ConfigToRecsComponents<TConfig> { | ||
const userTypes = { | ||
...config.userTypes, | ||
...Object.fromEntries(Object.entries(config.enums).map(([key]) => [key, { internalType: "uint8" }] as const)), | ||
}; | ||
|
||
return Object.fromEntries( | ||
Object.entries(config.tables).map(([tableName, table]) => [ | ||
tableName, | ||
defineComponent( | ||
world, | ||
{ | ||
...Object.fromEntries( | ||
Object.entries(table.valueSchema).map(([fieldName, schemaAbiType]) => [ | ||
fieldName, | ||
schemaAbiTypeToRecsType[schemaAbiType as SchemaAbiType], | ||
]) | ||
), | ||
__staticData: Type.OptionalString, | ||
__encodedLengths: Type.OptionalString, | ||
__dynamicData: Type.OptionalString, | ||
}, | ||
{ | ||
// TODO: support table namespaces https://github.com/latticexyz/mud/issues/994 | ||
id: resourceToHex({ | ||
type: table.offchainOnly ? "offchainTable" : "table", | ||
namespace: config.namespace, | ||
name: tableName, | ||
}), | ||
metadata: { | ||
componentName: tableName, | ||
tableName: `${config.namespace}:${tableName}`, | ||
keySchema: table.keySchema, | ||
valueSchema: table.valueSchema, | ||
}, | ||
} | ||
), | ||
tableToRecsComponent(world, { | ||
tableId: resourceToHex({ | ||
type: table.offchainOnly ? "offchainTable" : "table", | ||
namespace: config.namespace, | ||
name: table.name, // using the parsed config's `table.name` here rather than `tableName` key because that's what's used by codegen | ||
}), | ||
namespace: config.namespace, | ||
name: tableName, | ||
// TODO: refine to static ABI types so we don't have to cast | ||
keySchema: resolveUserTypes(table.keySchema, userTypes) as KeySchema, | ||
valueSchema: resolveUserTypes(table.valueSchema, userTypes), | ||
}), | ||
]) | ||
) as ConfigToRecsComponents<TConfig>; | ||
} |
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,34 @@ | ||
import { Type, World, defineComponent } from "@latticexyz/recs"; | ||
import { Table } from "../common"; | ||
import { TableToRecsComponent } from "./common"; | ||
import { schemaAbiTypeToRecsType } from "./schemaAbiTypeToRecsType"; | ||
import { SchemaAbiType } from "@latticexyz/schema-type"; | ||
|
||
export function tableToRecsComponent<table extends Omit<Table, "address">>( | ||
world: World, | ||
table: table | ||
): TableToRecsComponent<table> { | ||
return defineComponent( | ||
world, | ||
{ | ||
...Object.fromEntries( | ||
Object.entries(table.valueSchema).map(([fieldName, schemaAbiType]) => [ | ||
fieldName, | ||
schemaAbiTypeToRecsType[schemaAbiType as SchemaAbiType], | ||
]) | ||
), | ||
__staticData: Type.OptionalString, | ||
__encodedLengths: Type.OptionalString, | ||
__dynamicData: Type.OptionalString, | ||
}, | ||
{ | ||
id: table.tableId, | ||
metadata: { | ||
componentName: table.name, | ||
tableName: `${table.namespace}:${table.name}`, | ||
keySchema: table.keySchema, | ||
valueSchema: table.valueSchema, | ||
}, | ||
} | ||
) as TableToRecsComponent<table>; | ||
} |
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,13 @@ | ||
import { World } from "@latticexyz/recs"; | ||
import { Table } from "../common"; | ||
import { TablesToRecsComponents } from "./common"; | ||
import { tableToRecsComponent } from "./tableToRecsComponent"; | ||
|
||
export function tablesToRecsComponents<tables extends Record<string, Omit<Table, "address">>>( | ||
world: World, | ||
tables: tables | ||
): TablesToRecsComponents<tables> { | ||
return Object.fromEntries( | ||
Object.entries(tables).map(([tableName, table]) => [tableName, tableToRecsComponent(world, table)]) | ||
) as TablesToRecsComponents<tables>; | ||
} |