From ebf790a4be34d832a721cc08da9a789ee36b32c5 Mon Sep 17 00:00:00 2001 From: ponderingdemocritus Date: Thu, 19 Dec 2024 15:41:51 +1100 Subject: [PATCH] remove webworker --- client/src/dojo/setup.ts | 73 ++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 14 deletions(-) diff --git a/client/src/dojo/setup.ts b/client/src/dojo/setup.ts index 29acc548d..55768ea65 100644 --- a/client/src/dojo/setup.ts +++ b/client/src/dojo/setup.ts @@ -4,18 +4,73 @@ import { WORLD_CONFIG_ID, } from "@bibliothecadao/eternum"; import { DojoConfig } from "@dojoengine/core"; -import { getEntities, getEvents } from "@dojoengine/state"; -import { Clause } from "@dojoengine/torii-client"; +import { Component, Metadata, Schema } from "@dojoengine/recs"; +import { getEntities, getEvents, setEntities } from "@dojoengine/state"; +import { Clause, EntityKeysClause, ToriiClient } from "@dojoengine/torii-client"; +import { debounce } from "lodash"; import { createClientComponents } from "./createClientComponents"; import { createSystemCalls } from "./createSystemCalls"; import { ClientConfigManager } from "./modelManager/ConfigManager"; import { setupNetwork } from "./setupNetwork"; -import { setupWorker } from "./worker"; export type SetupResult = Awaited>; export const configManager = ClientConfigManager.instance(); +export const syncEntitiesDebounced = async ( + client: ToriiClient, + components: Component[], + entityKeyClause: EntityKeysClause[], + logging: boolean = true, + historical: boolean = false, +) => { + if (logging) console.log("Starting syncEntities"); + + let entityBatch: Record = {}; + + const debouncedSetEntities = debounce(() => { + if (Object.keys(entityBatch).length > 0) { + // console.log("Applying batch update", entityBatch); + setEntities(entityBatch, components, logging); + entityBatch = {}; // Clear the batch after applying + } + }, 200); // Increased debounce time to 1 second for larger batches + + // Handle entity updates + const entitySub = await client.onEntityUpdated(entityKeyClause, (fetchedEntities: any, data: any) => { + if (logging) console.log("Entity updated", fetchedEntities); + // Merge new data with existing data for this entity + entityBatch[fetchedEntities] = { + ...entityBatch[fetchedEntities], + ...data, + }; + debouncedSetEntities(); + }); + + // Handle event message updates + const eventSub = await client.onEventMessageUpdated( + entityKeyClause, + historical, + (fetchedEntities: any, data: any) => { + if (logging) console.log("Event message updated", fetchedEntities); + // Merge new data with existing data for this entity + entityBatch[fetchedEntities] = { + ...entityBatch[fetchedEntities], + ...data, + }; + debouncedSetEntities(); + }, + ); + + // Return combined subscription that can cancel both + return { + cancel: () => { + entitySub.cancel(); + eventSub.cancel(); + }, + }; +}; + export async function setup({ ...config }: DojoConfig) { const network = await setupNetwork(config); const components = createClientComponents(network); @@ -112,17 +167,7 @@ export async function setup({ ...config }: DojoConfig) { false, ); - const sync = await setupWorker( - { - rpcUrl: config.rpcUrl, - toriiUrl: config.toriiUrl, - relayUrl: config.relayUrl, - worldAddress: config.manifest.world.address || "", - }, - network.contractComponents as any, - [], - false, - ); + const sync = await syncEntitiesDebounced(network.toriiClient, network.contractComponents as any, [], false); configManager.setDojo(components);