-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a47469
commit 8d06296
Showing
7 changed files
with
223 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { debounce } from "lodash"; | ||
|
||
let toriiClient = null; | ||
let entityBatch = {}; | ||
let logging = false; | ||
const DEBOUNCE_DELAY = 1000; | ||
|
||
let debouncedSendBatch = debounce(() => { | ||
if (Object.keys(entityBatch).length > 0) { | ||
console.log("Worker: Sending batch", entityBatch); | ||
self.postMessage({ updatedEntities: entityBatch }); | ||
entityBatch = {}; | ||
} | ||
}, DEBOUNCE_DELAY); | ||
|
||
self.onmessage = async (e) => { | ||
const { type, entities, logging: logFlag } = e.data; | ||
if (type === "update") { | ||
logging = logFlag; | ||
if (logging) console.log("Worker: Received entities update"); | ||
handleUpdate(entities.fetchedEntities, entities.data); | ||
} | ||
}; | ||
|
||
function handleUpdate(fetchedEntities, data) { | ||
entityBatch[fetchedEntities] = { | ||
...entityBatch[fetchedEntities], | ||
...data, | ||
}; | ||
debouncedSendBatch(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Component, Metadata, Schema } from "@dojoengine/recs"; | ||
import { setEntities } from "@dojoengine/state"; | ||
import { EntityKeysClause, createClient } from "@dojoengine/torii-wasm"; | ||
|
||
export async function setupWorker( | ||
config: { rpcUrl: string; toriiUrl: string; relayUrl: string; worldAddress: string }, | ||
components: Component<Schema, Metadata, undefined>[], | ||
entityKeyClause: EntityKeysClause[], | ||
historical: boolean, | ||
logging = false, | ||
) { | ||
if (logging) console.log("Starting syncEntities"); | ||
|
||
const worker = new Worker(new URL("./entityWorker.js", import.meta.url), { type: "module" }); | ||
|
||
// Create the client in the main thread | ||
const toriiClient = await createClient({ | ||
rpcUrl: config.rpcUrl, | ||
toriiUrl: config.toriiUrl, | ||
relayUrl: config.relayUrl, | ||
worldAddress: config.worldAddress, | ||
}); | ||
|
||
// Listen for batches from the Worker | ||
worker.onmessage = (event) => { | ||
const { updatedEntities } = event.data; | ||
setEntities(updatedEntities, components, logging); | ||
}; | ||
|
||
// Set up subscriptions in the main thread | ||
await toriiClient.onEntityUpdated(entityKeyClause, (fetchedEntities: any, data: any) => { | ||
if (logging) console.log("Main: Entity updated", fetchedEntities); | ||
// Send updates to worker for processing | ||
worker.postMessage({ | ||
type: "update", | ||
entities: { fetchedEntities, data }, | ||
logging, | ||
}); | ||
}); | ||
|
||
await toriiClient.onEventMessageUpdated(entityKeyClause, historical, (fetchedEntities: any, data: any) => { | ||
if (logging) console.log("Main: Event message updated", fetchedEntities); | ||
// Send updates to worker for processing | ||
worker.postMessage({ | ||
type: "update", | ||
entities: { fetchedEntities, data }, | ||
logging, | ||
}); | ||
}); | ||
|
||
return { | ||
cancel: () => { | ||
worker.terminate(); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.