Skip to content

Commit

Permalink
feat: allow callback function after getEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
edisontim committed Nov 15, 2024
1 parent b47c945 commit 856497d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
20 changes: 14 additions & 6 deletions packages/state/src/recs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const getSyncEntities = async <S extends Schema>(
* @param limit - The maximum number of events to fetch per request (default: 100).
* @param logging - Whether to log debug information (default: false).
* @param historical - Whether to fetch and subscribe to historical events (default: false).
* @param callback - An optional callback function to be called after fetching events.
* @returns A promise that resolves to a subscription for event updates.
*
* @example
Expand Down Expand Up @@ -91,17 +92,20 @@ export const getSyncEvents = async <S extends Schema>(
entityKeyClause: EntityKeysClause[],
limit: number = 100,
logging: boolean = false,
historical: boolean = true
historical: boolean = true,
callback?: () => void
) => {
if (logging) console.log("Starting getSyncEvents");
await getEvents(client, components, limit, clause, logging, historical);
return await syncEvents(
await getEvents(
client,
components,
entityKeyClause,
limit,
clause,
logging,
historical
historical,
callback
);
return await syncEvents(client, components, entityKeyClause, logging);
};

/**
Expand Down Expand Up @@ -159,14 +163,16 @@ export const getEntities = async <S extends Schema>(
* @param clause - An optional clause to filter event messages.
* @param logging - Whether to log debug information (default: false).
* @param historical - Whether to fetch historical events (default: false).
* @param callback - An optional callback function to be called after fetching events.
*/
export const getEvents = async <S extends Schema>(
client: ToriiClient,
components: Component<S, Metadata, undefined>[],
limit: number = 100,
clause: Clause | undefined,
logging: boolean = false,
historical: boolean = false
historical: boolean = false,
callback?: () => void
) => {
if (logging) console.log("Starting getEvents");
let offset = 0;
Expand All @@ -193,6 +199,8 @@ export const getEvents = async <S extends Schema>(
offset += limit;
}
}

callback && callback();
};

/**
Expand Down
3 changes: 0 additions & 3 deletions packages/state/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ function handleStringArray(value: any) {
try {
return BigInt(a.value);
} catch (error) {
console.warn(
`Failed to convert ${a.value} to BigInt. Using string value instead.`
);
return a.value;
}
});
Expand Down

0 comments on commit 856497d

Please sign in to comment.