Skip to content

Commit c8eef83

Browse files
feat: add in continue fetching
1 parent 71d62ea commit c8eef83

File tree

4 files changed

+33
-125
lines changed

4 files changed

+33
-125
lines changed

packages/sdk/src/getEntities.ts

+30-20
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,41 @@ import * as torii from "@dojoengine/torii-client";
55
export async function getEntities<T extends SchemaType, K extends keyof T>(
66
client: torii.ToriiClient,
77
query: { [P in K]?: Partial<T[P]> },
8-
callback: (response: {
9-
entities?: torii.Entities;
10-
data: torii.Entities;
11-
error?: Error;
12-
}) => void,
8+
callback: (response: { entities?: torii.Entities; error?: Error }) => void,
139
limit: number = 100, // Default limit
1410
offset: number = 0 // Default offset
1511
): Promise<torii.Entities> {
1612
const clauses = convertQueryToClause(query);
17-
const toriiQuery: torii.Query = {
18-
limit: limit,
19-
offset: offset,
20-
clause: {
21-
Composite: {
22-
operator: "And",
23-
clauses: [clauses],
13+
let cursor = offset;
14+
let continueFetching = true;
15+
let allEntities: torii.Entities = {};
16+
17+
while (continueFetching) {
18+
const toriiQuery: torii.Query = {
19+
limit: limit,
20+
offset: cursor,
21+
clause: {
22+
Composite: {
23+
operator: "And",
24+
clauses: [clauses],
25+
},
2426
},
25-
},
26-
};
27+
};
28+
29+
try {
30+
const entities = await client.getEntities(toriiQuery);
31+
Object.assign(allEntities, entities);
32+
callback({ entities });
2733

28-
try {
29-
const entities = await client.getEntities(toriiQuery);
30-
callback({ data: entities });
31-
return entities;
32-
} catch (error) {
33-
throw error;
34+
if (Object.keys(entities).length < limit) {
35+
continueFetching = false;
36+
} else {
37+
cursor += limit;
38+
}
39+
} catch (error) {
40+
throw error;
41+
}
3442
}
43+
44+
return allEntities;
3545
}

packages/sdk/src/subscribeQuery.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@ import { SchemaType } from "./types";
55
export async function subscribeQuery<T extends SchemaType, K extends keyof T>(
66
client: torii.ToriiClient,
77
query: { [P in K]?: Partial<T[P]> },
8-
callback: (response: {
9-
entities?: torii.Entities;
10-
data: torii.Entities;
11-
error?: Error;
12-
}) => void
8+
callback: (response: { entities?: torii.Entities; error?: Error }) => void
139
): Promise<torii.Subscription> {
1410
const clauses = convertQueryToClauses(query);
1511
return client.onEntityUpdated(
1612
clauses,
17-
(entities: torii.Entities, data: torii.Entities) => {
18-
callback({ entities, data });
13+
(entities: string, data: torii.Entities) => {
14+
callback({ [entities]: data });
1915
}
2016
);
2117
}

packages/torii-client/src/index.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
export * from "@dojoengine/torii-wasm";
2-
3-
export * from "./utils";

packages/torii-client/src/utils.ts

-96
This file was deleted.

0 commit comments

Comments
 (0)