diff --git a/.changeset/tricky-frogs-beam.md b/.changeset/tricky-frogs-beam.md new file mode 100644 index 0000000000..cbff4e5c78 --- /dev/null +++ b/.changeset/tricky-frogs-beam.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/network": patch +--- + +Remove devEmit function when sending network events from SyncWorker because they can't be serialized across the web worker boundary. diff --git a/packages/network/src/types.ts b/packages/network/src/types.ts index a256058ce3..4b51ed418f 100644 --- a/packages/network/src/types.ts +++ b/packages/network/src/types.ts @@ -92,6 +92,7 @@ export type NetworkComponentUpdate = { partialValue?: Partial>>; initialValue?: ComponentValue>; ephemeral?: boolean; + devEmit?: () => void; }; }[keyof C] & { entity: Entity; diff --git a/packages/network/src/v2/ecsEventFromLog.ts b/packages/network/src/v2/ecsEventFromLog.ts index e650795898..7862cfebbf 100644 --- a/packages/network/src/v2/ecsEventFromLog.ts +++ b/packages/network/src/v2/ecsEventFromLog.ts @@ -16,7 +16,7 @@ export const ecsEventFromLog = async ( log: Log, parsedLog: LogDescription, lastEventInTx: boolean -): Promise<(NetworkComponentUpdate & { devEmit: () => void }) | undefined> => { +): Promise => { const { blockNumber, transactionHash, logIndex } = log; const { args, name } = parsedLog; diff --git a/packages/network/src/v2/fetchStoreEvents.ts b/packages/network/src/v2/fetchStoreEvents.ts index ee3d262dc0..7fd4ab9daa 100644 --- a/packages/network/src/v2/fetchStoreEvents.ts +++ b/packages/network/src/v2/fetchStoreEvents.ts @@ -42,7 +42,7 @@ export async function fetchStoreEvents( // We defer the emissions of dev events because `ecsEventFromLog` is async and emitting them // from within that function causes them to arrive out of order. It's better if our emitter // can guarantee ordering for now. - events.forEach((event) => event.devEmit()); + events.forEach((event) => event?.devEmit && event.devEmit()); return events; } diff --git a/packages/network/src/workers/SyncWorker.ts b/packages/network/src/workers/SyncWorker.ts index c79501c82b..82cd1cb79e 100644 --- a/packages/network/src/workers/SyncWorker.ts +++ b/packages/network/src/workers/SyncWorker.ts @@ -162,7 +162,14 @@ export class SyncWorker implements DoWork); + const networkEvent = event as NetworkEvent; + if (isNetworkComponentUpdateEvent(networkEvent)) { + // Remove devEmit from event before passing it to the main thread + // because it is not serializable + delete networkEvent.devEmit; + } + + this.output$.next(networkEvent); }); const streamStartBlockNumberPromise = awaitStreamValue(blockNumber$);