-
Notifications
You must be signed in to change notification settings - Fork 51
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
ebf790a
commit 2adc56d
Showing
4 changed files
with
131 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { Component, Metadata, Schema } from "@dojoengine/recs"; | ||
import { ToriiClient } from "@dojoengine/torii-client"; | ||
import debounce from "lodash/debounce"; | ||
import { | ||
addMarketSubscription, | ||
addToSubscription, | ||
addToSubscriptionOneKeyModelbyRealmEntityId, | ||
addToSubscriptionTwoKeyModelbyRealmEntityId, | ||
syncPosition, | ||
} from "./queries"; | ||
|
||
// Queue class to manage requests | ||
class RequestQueue { | ||
private queue: Array<() => Promise<void>> = []; | ||
private processing = false; | ||
private batchSize = 3; // Number of concurrent requests | ||
private batchDelayMs = 100; // Delay between batches | ||
|
||
async add(request: () => Promise<void>) { | ||
this.queue.push(request); | ||
if (!this.processing) { | ||
this.processing = true; | ||
this.processQueue(); | ||
} | ||
} | ||
|
||
private async processQueue() { | ||
while (this.queue.length > 0) { | ||
const batch = this.queue.splice(0, this.batchSize); | ||
|
||
try { | ||
await Promise.all(batch.map((request) => request())); | ||
} catch (error) { | ||
console.error("Error processing request batch:", error); | ||
} | ||
|
||
if (this.queue.length > 0) { | ||
// Add delay between batches to prevent overwhelming the server | ||
await new Promise((resolve) => setTimeout(resolve, this.batchDelayMs)); | ||
} | ||
} | ||
this.processing = false; | ||
} | ||
|
||
clear() { | ||
this.queue = []; | ||
} | ||
} | ||
|
||
// Create separate queues for different types of requests | ||
const positionQueue = new RequestQueue(); | ||
const subscriptionQueue = new RequestQueue(); | ||
const marketQueue = new RequestQueue(); | ||
|
||
// Debounced functions that add to queues | ||
export const debouncedSyncPosition = debounce( | ||
async <S extends Schema>(client: ToriiClient, components: Component<S, Metadata, undefined>[], entityID: string) => { | ||
await positionQueue.add(() => syncPosition(client, components, entityID)); | ||
}, | ||
100, | ||
{ leading: true }, // Add leading: true to execute immediately on first call | ||
); | ||
|
||
export const debouncedAddToSubscriptionTwoKey = debounce( | ||
async <S extends Schema>( | ||
client: ToriiClient, | ||
components: Component<S, Metadata, undefined>[], | ||
entityID: string[], | ||
) => { | ||
await subscriptionQueue.add(() => addToSubscriptionTwoKeyModelbyRealmEntityId(client, components, entityID)); | ||
}, | ||
250, | ||
{ leading: true }, | ||
); | ||
|
||
export const debouncedAddToSubscriptionOneKey = debounce( | ||
async <S extends Schema>( | ||
client: ToriiClient, | ||
components: Component<S, Metadata, undefined>[], | ||
entityID: string[], | ||
) => { | ||
await subscriptionQueue.add(() => addToSubscriptionOneKeyModelbyRealmEntityId(client, components, entityID)); | ||
}, | ||
250, | ||
{ leading: true }, | ||
); | ||
|
||
export const debouncedAddToSubscription = debounce( | ||
async <S extends Schema>( | ||
client: ToriiClient, | ||
components: Component<S, Metadata, undefined>[], | ||
entityID: string[], | ||
position?: { x: number; y: number }[], | ||
) => { | ||
await subscriptionQueue.add(() => addToSubscription(client, components, entityID, position)); | ||
}, | ||
250, | ||
{ leading: true }, | ||
); | ||
|
||
export const debouncedAddMarketSubscription = debounce( | ||
async <S extends Schema>(client: ToriiClient, components: Component<S, Metadata, undefined>[]) => { | ||
await marketQueue.add(() => addMarketSubscription(client, components)); | ||
}, | ||
500, | ||
{ leading: true }, | ||
); | ||
|
||
// Utility function to clear all queues if needed | ||
export const clearAllQueues = () => { | ||
positionQueue.clear(); | ||
subscriptionQueue.clear(); | ||
marketQueue.clear(); | ||
}; |
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
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