1- import { Tables } from "@latticexyz/config" ;
1+ import { Table , Tables } from "@latticexyz/config" ;
22import { debug } from "./debug" ;
3- import { World as RecsWorld , getComponentValue , hasComponent , removeComponent , setComponent } from "@latticexyz/recs" ;
4- import { defineInternalComponents } from "./defineInternalComponents" ;
5- import { getTableEntity } from "./getTableEntity" ;
3+ import { World as RecsWorld , getComponentValue , removeComponent , setComponent } from "@latticexyz/recs" ;
64import { hexToResource , resourceToLabel , spliceHex } from "@latticexyz/common" ;
7- import { decodeValueArgs } from "@latticexyz/protocol-parser/internal" ;
5+ import { decodeValueArgs , getSchemaTypes , getValueSchema } from "@latticexyz/protocol-parser/internal" ;
86import { Hex , size } from "viem" ;
9- import { isTableRegistrationLog } from "../isTableRegistrationLog" ;
10- import { logToTable } from "../logToTable" ;
117import { hexKeyTupleToEntity } from "./hexKeyTupleToEntity" ;
128import { StorageAdapter , StorageAdapterBlock } from "../common" ;
139import { singletonEntity } from "./singletonEntity" ;
1410import { tablesToComponents } from "./tablesToComponents" ;
15- import { merge } from "@ark/util" ;
1611
17- export type CreateStorageAdapterOptions < tables extends Tables > = {
12+ export type CreateStorageAdapterOptions < tables extends Tables = { } > = {
1813 world : RecsWorld ;
14+ /** @deprecated Use `const components = tablesToComponents(world, tables)` instead. */
1915 tables : tables ;
2016 shouldSkipUpdateStream ?: ( ) => boolean ;
2117} ;
2218
23- export type CreateStorageAdapterResult < tables extends Tables > = {
19+ export type CreateStorageAdapterResult < tables extends Tables = { } > = {
2420 storageAdapter : StorageAdapter ;
25- components : merge < tablesToComponents < tables > , ReturnType < typeof defineInternalComponents > > ;
21+ /** @deprecated Use `const components = tablesToComponents(world, tables)` instead. */
22+ components : tablesToComponents < tables > ;
2623} ;
2724
28- export function createStorageAdapter < tables extends Tables > ( {
25+ export function createStorageAdapter < tables extends Tables = { } > ( {
2926 world,
30- tables,
27+ tables = { } as tables ,
3128 shouldSkipUpdateStream,
3229} : CreateStorageAdapterOptions < tables > ) : CreateStorageAdapterResult < tables > {
3330 world . registerEntity ( { id : singletonEntity } ) ;
3431
35- const components = {
36- ...tablesToComponents ( world , tables ) ,
37- ...defineInternalComponents ( world ) ,
38- } as CreateStorageAdapterResult < tables > [ "components" ] ;
39-
40- async function recsStorageAdapter ( { logs } : StorageAdapterBlock ) : Promise < void > {
41- const newTables = logs . filter ( isTableRegistrationLog ) . map ( logToTable ) ;
42- for ( const newTable of newTables ) {
43- const tableEntity = getTableEntity ( newTable ) ;
44- if ( hasComponent ( components . RegisteredTables , tableEntity ) ) {
45- console . warn ( "table already registered, ignoring" , {
46- newTable,
47- existingTable : getComponentValue ( components . RegisteredTables , tableEntity ) ?. table ,
48- } ) ;
49- } else {
50- setComponent (
51- components . RegisteredTables ,
52- tableEntity ,
53- { table : newTable } ,
54- { skipUpdateStream : shouldSkipUpdateStream ?.( ) } ,
55- ) ;
56- }
57- }
32+ // kept for backwards compat
33+ const components = tablesToComponents ( world , tables ) as CreateStorageAdapterResult < tables > [ "components" ] ;
5834
35+ async function storageAdapter ( { logs } : StorageAdapterBlock ) : Promise < void > {
5936 for ( const log of logs ) {
60- const { namespace, name } = hexToResource ( log . args . tableId ) ;
61- const table = getComponentValue (
62- components . RegisteredTables ,
63- getTableEntity ( { address : log . address , namespace, name } ) ,
64- ) ?. table ;
65- if ( ! table ) {
66- debug ( `skipping update for unknown table: ${ resourceToLabel ( { namespace, name } ) } at ${ log . address } ` ) ;
67- continue ;
68- }
69-
70- const component = world . components . find ( ( c ) => c . id === table . tableId ) ;
37+ const tableId = log . args . tableId ;
38+ const component = world . components . find ( ( c ) => c . id === tableId ) ;
7139 if ( ! component ) {
7240 debug (
73- `skipping update for unknown component: ${ table . tableId } (${ resourceToLabel ( {
74- namespace,
75- name,
76- } ) } ). Available components: ${ Object . keys ( components ) } `,
41+ `skipping update for unknown component: ${ tableId } (${ resourceToLabel ( hexToResource ( tableId ) ) } ). Available components: ${ Object . keys ( components ) } ` ,
7742 ) ;
7843 continue ;
7944 }
45+ const table = component . metadata ?. table as Table | undefined ;
46+ if ( ! table ) {
47+ debug ( `skipping update for unknown table: ${ resourceToLabel ( hexToResource ( tableId ) ) } at ${ log . address } ` ) ;
48+ continue ;
49+ }
8050
51+ const valueSchema = getSchemaTypes ( getValueSchema ( table ) ) ;
8152 const entity = hexKeyTupleToEntity ( log . args . keyTuple ) ;
8253
8354 if ( log . eventName === "Store_SetRecord" ) {
84- const value = decodeValueArgs ( table . valueSchema , log . args ) ;
55+ const value = decodeValueArgs ( valueSchema , log . args ) ;
8556 debug ( "setting component" , {
8657 namespace : table . namespace ,
8758 name : table . name ,
@@ -104,7 +75,7 @@ export function createStorageAdapter<tables extends Tables>({
10475 const previousValue = getComponentValue ( component , entity ) ;
10576 const previousStaticData = ( previousValue ?. __staticData as Hex ) ?? "0x" ;
10677 const newStaticData = spliceHex ( previousStaticData , log . args . start , size ( log . args . data ) , log . args . data ) ;
107- const newValue = decodeValueArgs ( table . valueSchema , {
78+ const newValue = decodeValueArgs ( valueSchema , {
10879 staticData : newStaticData ,
10980 encodedLengths : ( previousValue ?. __encodedLengths as Hex ) ?? "0x" ,
11081 dynamicData : ( previousValue ?. __dynamicData as Hex ) ?? "0x" ,
@@ -132,7 +103,7 @@ export function createStorageAdapter<tables extends Tables>({
132103 const previousValue = getComponentValue ( component , entity ) ;
133104 const previousDynamicData = ( previousValue ?. __dynamicData as Hex ) ?? "0x" ;
134105 const newDynamicData = spliceHex ( previousDynamicData , log . args . start , log . args . deleteCount , log . args . data ) ;
135- const newValue = decodeValueArgs ( table . valueSchema , {
106+ const newValue = decodeValueArgs ( valueSchema , {
136107 staticData : ( previousValue ?. __staticData as Hex ) ?? "0x" ,
137108 // TODO: handle unchanged encoded lengths
138109 encodedLengths : log . args . encodedLengths ,
@@ -168,5 +139,9 @@ export function createStorageAdapter<tables extends Tables>({
168139 }
169140 }
170141
171- return { storageAdapter : recsStorageAdapter , components } ;
142+ return {
143+ storageAdapter,
144+ /** @deprecated Use `const components = tablesToComponents(world, tables)` instead. */
145+ components,
146+ } ;
172147}
0 commit comments