Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow callback function after getEvents #332

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Comment on lines +202 to +203
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve callback execution with optional chaining and error handling

The current implementation has two areas for improvement:

  1. Use optional chaining for better readability
  2. Add error handling around the callback execution

Apply this diff to improve the implementation:

-    callback && callback();
+    try {
+        callback?.();
+    } catch (error) {
+        console.warn('Error executing callback after getEvents:', error);
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
callback && callback();
try {
callback?.();
} catch (error) {
console.warn('Error executing callback after getEvents:', error);
}
🧰 Tools
🪛 Biome

[error] 203-203: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

};

/**
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
Loading