Skip to content

Commit

Permalink
batch queries
Browse files Browse the repository at this point in the history
  • Loading branch information
bob0005 committed Dec 28, 2024
1 parent 26df692 commit 5756cdb
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions landing/src/hooks/helpers/useDonkeyArrivals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { GET_ETERNUM_ENTITY_OWNERS } from "../query/entityOwners";
import { GET_ENTITY_DISTANCE } from "../query/position";
import { GET_ENTITIES_RESOURCES } from "../query/resources";

const BATCH_SIZE = 5;

export function useDonkeyArrivals(realmEntityIds: ID[]) {
const {
data: entityPositions,
Expand All @@ -29,11 +31,20 @@ export function useDonkeyArrivals(realmEntityIds: ID[]) {
// refetchInterval: 10_000,
// });

const batchedRealmIds = useMemo(() => {
const batches = [];
for (let i = 0; i < realmEntityIds.length; i += BATCH_SIZE) {
batches.push(realmEntityIds.slice(i, i + BATCH_SIZE));
}
return batches;
}, [realmEntityIds]);


const donkeyQueriesResults = useQueries({
queries: realmEntityIds.map((realmId) => ({
queryKey: ["donkeyEntityIds", realmId],
queryFn: () => execute(GET_ETERNUM_ENTITY_OWNERS, { entityOwnerIds: [realmId] }),
enabled: !!realmId,
queries: batchedRealmIds.map((realmIds) => ({
queryKey: ["donkeyEntityIds", realmIds],
queryFn: () => execute(GET_ETERNUM_ENTITY_OWNERS, { entityOwnerIds: realmIds }),
enabled: !!realmIds,
staleTime: 5 * 60 * 1000,
})),
});
Expand Down

0 comments on commit 5756cdb

Please sign in to comment.