Skip to content

Commit c9b52f1

Browse files
fix: error on getEntitiesQuery
1 parent f68e2f9 commit c9b52f1

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

packages/state/src/recs/index.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export const getEvents = async <S extends Schema>(
160160
};
161161

162162
/**
163-
* Fetches entities and their components from the client based on specified criteria.
163+
* Fetches entities and their components from the client based on specified criteria, helping to reduce the loading time when the entities are fetched.
164164
* @param client - The client instance for API communication.
165165
* @param components - An array of component definitions to fetch.
166166
* @param entityKeyClause - An optional EntityKeysClause to filter entities by their keys.
@@ -170,20 +170,25 @@ export const getEvents = async <S extends Schema>(
170170
* @example
171171
* const components = createClientComponents({ contractComponents });
172172
* await getEntitiesQuery(client, components, undefined, "FixedLen", 1000);
173+
* return await syncEntities(toriiClient, components as any, []);
173174
*
174175
* @example
175176
* const components = createClientComponents({ contractComponents });
176177
* await getEntitiesQuery(client, components, { Keys: { keys: ["0x1"], models: ["Position"] } }, "FixedLen", 1000);
178+
* return await syncEntities(toriiClient, components as any, []);
177179
*
178180
* @example
179181
* const components = createClientComponents({ contractComponents });
180182
* await getEntitiesQuery(client, components, { HashedKeys: ["0x1"] }, "FixedLen", 1000);
183+
* return await syncEntities(toriiClient, components as any, []);
181184
*
182185
* This function performs paginated queries to fetch all matching entities and their
183186
* components. It uses the provided EntityKeysClause (if any) to filter entities and
184187
* the specified components to determine which data to retrieve. The function continues
185188
* fetching until all matching entities have been retrieved, using the 'limit' parameter
186189
* to control the batch size of each request.
190+
*
191+
* Note: Make sure to synchronize the entities by calling the syncEntities method
187192
*/
188193
export const getEntitiesQuery = async <S extends Schema>(
189194
client: ToriiClient,
@@ -195,22 +200,24 @@ export const getEntitiesQuery = async <S extends Schema>(
195200
let cursor = 0;
196201
let continueFetching = true;
197202

198-
while (continueFetching) {
199-
const clause: Clause | null = entityKeyClause
200-
? {
201-
Keys: {
202-
keys:
203-
"HashedKeys" in entityKeyClause
204-
? entityKeyClause.HashedKeys
205-
: entityKeyClause.Keys.keys,
206-
pattern_matching: patternMatching,
207-
models: [
208-
...components.map((c) => c.metadata?.name as string),
209-
],
210-
},
211-
}
212-
: null;
203+
const componentArray = Object.values(components);
213204

205+
const clause: Clause | null = entityKeyClause
206+
? {
207+
Keys: {
208+
keys:
209+
"HashedKeys" in entityKeyClause
210+
? entityKeyClause.HashedKeys
211+
: entityKeyClause.Keys.keys,
212+
pattern_matching: patternMatching,
213+
models: [
214+
...componentArray.map((c) => c.metadata?.name as string),
215+
],
216+
},
217+
}
218+
: null;
219+
220+
while (continueFetching) {
214221
const fetchedEntities = await client.getEntities({
215222
limit,
216223
offset: cursor,

0 commit comments

Comments
 (0)