Skip to content

Commit

Permalink
fix: remove devEmit when sending events from SyncWorker (#1109)
Browse files Browse the repository at this point in the history
* fix: remove devEmit when sending events from SyncWorker

* Create tricky-frogs-beam.md

* Update tricky-frogs-beam.md

---------

Co-authored-by: Kevin Ingersoll <[email protected]>
  • Loading branch information
Kooshaba and holic authored Jul 6, 2023
1 parent 904fd7d commit e019c77
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-frogs-beam.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions packages/network/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export type NetworkComponentUpdate<C extends Components = Components> = {
partialValue?: Partial<ComponentValue<SchemaOf<C[key]>>>;
initialValue?: ComponentValue<SchemaOf<C[key]>>;
ephemeral?: boolean;
devEmit?: () => void;
};
}[keyof C] & {
entity: Entity;
Expand Down
2 changes: 1 addition & 1 deletion packages/network/src/v2/ecsEventFromLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const ecsEventFromLog = async (
log: Log,
parsedLog: LogDescription,
lastEventInTx: boolean
): Promise<(NetworkComponentUpdate & { devEmit: () => void }) | undefined> => {
): Promise<NetworkComponentUpdate | undefined> => {
const { blockNumber, transactionHash, logIndex } = log;
const { args, name } = parsedLog;

Expand Down
2 changes: 1 addition & 1 deletion packages/network/src/v2/fetchStoreEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
9 changes: 8 additions & 1 deletion packages/network/src/workers/SyncWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,14 @@ export class SyncWorker<C extends Components> implements DoWork<Input, NetworkEv
}
}

this.output$.next(event as NetworkEvent<C>);
const networkEvent = event as NetworkEvent<C>;
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$);

Expand Down

0 comments on commit e019c77

Please sign in to comment.