Skip to content

Commit

Permalink
remove webworker
Browse files Browse the repository at this point in the history
  • Loading branch information
ponderingdemocritus committed Dec 19, 2024
1 parent c6e5221 commit ebf790a
Showing 1 changed file with 59 additions and 14 deletions.
73 changes: 59 additions & 14 deletions client/src/dojo/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReturnType<typeof setup>>;

export const configManager = ClientConfigManager.instance();

export const syncEntitiesDebounced = async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
entityKeyClause: EntityKeysClause[],
logging: boolean = true,
historical: boolean = false,
) => {
if (logging) console.log("Starting syncEntities");

let entityBatch: Record<string, any> = {};

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);
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit ebf790a

Please sign in to comment.