-
Notifications
You must be signed in to change notification settings - Fork 41
feat: broadcastWithOptions ACTR-136 #939
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
base: 04-20-feat_ai-agent_example
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -36,6 +36,16 @@ export interface SaveStateOptions { | |||||
immediate?: boolean; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Options for the `_broadcastWithOptions` method. | ||||||
*/ | ||||||
export interface BroadcastInstanceOptions { | ||||||
/** | ||||||
* The connection IDs to be excluded from the broadcast. | ||||||
*/ | ||||||
exclude?: string[]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The type for
Suggested change
Spotted by Diamond |
||||||
} | ||||||
|
||||||
/** Actor type alias with all `any` types. Used for `extends` in classes referencing this actor. */ | ||||||
// biome-ignore lint/suspicious/noExplicitAny: Needs to be used in `extends` | ||||||
export type AnyActorInstance = ActorInstance<any, any, any, any>; | ||||||
|
@@ -1013,6 +1023,16 @@ export class ActorInstance<S, CP, CS, V> { | |||||
* @param args - The arguments to send with the event. | ||||||
*/ | ||||||
_broadcast<Args extends Array<unknown>>(name: string, ...args: Args) { | ||||||
return this._broadcastWithOptions({}, name, ...args); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Broadcasts an event to all connected clients with options. | ||||||
* @param opts - Options for the broadcast. | ||||||
* @param name - The name of the event. | ||||||
* @param args - The arguments to send with the event. | ||||||
*/ | ||||||
_broadcastWithOptions<Args extends Array<unknown>>(opts: BroadcastInstanceOptions, name: string, ...args: Args) { | ||||||
this.#assertReady(); | ||||||
|
||||||
// Send to all connected clients | ||||||
|
@@ -1028,8 +1048,14 @@ export class ActorInstance<S, CP, CS, V> { | |||||
}, | ||||||
}); | ||||||
|
||||||
const excludeList = opts.exclude ?? []; | ||||||
|
||||||
// Send message to clients | ||||||
for (const connection of subscriptions) { | ||||||
if (excludeList.includes(connection.id)) { | ||||||
continue; | ||||||
} | ||||||
|
||||||
connection._sendMessage(toClientSerializer); | ||||||
} | ||||||
} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential Issue: This code mutates the
exclude
array that was passed in throughopts
. If the caller reuses this array elsewhere, they might encounter unexpected behavior. Consider creating a copy of the array instead:This ensures the original array remains unchanged while still allowing the function to add the current connection ID when
excludeSelf
is true.Spotted by Diamond
Is this helpful? React 👍 or 👎 to let us know.