Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove webworker #2562

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading