Skip to content

Commit 7bf1236

Browse files
committed
feat: broadcastWithOptions ACTR-136
1 parent 317f869 commit 7bf1236

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

packages/actor-core/src/actor/action.ts

+36
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ import type { SaveStateOptions } from "./instance";
88
import { Actions } from "./config";
99
import { ActorContext } from "./context";
1010

11+
/**
12+
* Options for the `_broadcast` method.
13+
*/
14+
interface BroadcastOptions {
15+
/**
16+
* The connection IDs to be excluded from the broadcast.
17+
*/
18+
exclude?: ConnId[];
19+
/**
20+
* Excludes the current connection from the broadcast.
21+
*/
22+
excludeSelf?: boolean;
23+
}
24+
1125
/**
1226
* Context for a remote procedure call.
1327
*
@@ -50,6 +64,28 @@ export class ActionContext<S, CP, CS, V> {
5064
this.#actorContext.broadcast(name, ...args);
5165
}
5266

67+
/**
68+
* Broadcasts an event to all connected clients with options.
69+
*/
70+
broadcastWithOptions<Args extends Array<unknown>>(opts: BroadcastOptions, name: string, ...args: Args) {
71+
const exclude = opts.exclude ?? [];
72+
73+
if (opts.excludeSelf) {
74+
exclude.push(this.conn.id);
75+
}
76+
77+
// @ts-ignore - Access protected method
78+
this.#actorContext.broadcastWithOptions({ exclude }, name, ...args);
79+
return;
80+
}
81+
82+
/**
83+
* Alias for `broadcastWithOptions`
84+
*/
85+
broadcastWith<Args extends Array<unknown>>(opts: BroadcastOptions, name: string, ...args: Args) {
86+
return this.broadcastWithOptions(opts, name, ...args);
87+
}
88+
5389
/**
5490
* Gets the logger instance.
5591
*/

packages/actor-core/src/actor/context.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Logger } from "@/common/log";
22
import { Actions } from "./config";
3-
import { ActorInstance, SaveStateOptions } from "./instance";
3+
import { ActorInstance, BroadcastInstanceOptions, SaveStateOptions } from "./instance";
44
import { Conn, ConnId } from "./connection";
55
import { ActorTags } from "@/common/utils";
66
import { Schedule } from "./schedule";
@@ -41,6 +41,18 @@ export class ActorContext<S, CP, CS, V> {
4141
return;
4242
}
4343

44+
/**
45+
* Broadcasts an event to all connected clients with options.
46+
* @param opts - Options for the broadcast.
47+
* @param name - The name of the event.
48+
* @param args - The arguments to send with the event.
49+
*/
50+
broadcastWithOptions<Args extends Array<unknown>>(opts: BroadcastInstanceOptions, name: string, ...args: Args) {
51+
// @ts-ignore - Access protected method
52+
this.#actor._broadcastWithOptions(opts, name, ...args);
53+
return;
54+
}
55+
4456
/**
4557
* Gets the logger instance.
4658
*/

packages/actor-core/src/actor/instance.ts

+26
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ export interface SaveStateOptions {
3636
immediate?: boolean;
3737
}
3838

39+
/**
40+
* Options for the `_broadcastWithOptions` method.
41+
*/
42+
export interface BroadcastInstanceOptions {
43+
/**
44+
* The connection IDs to be excluded from the broadcast.
45+
*/
46+
exclude?: string[];
47+
}
48+
3949
/** Actor type alias with all `any` types. Used for `extends` in classes referencing this actor. */
4050
// biome-ignore lint/suspicious/noExplicitAny: Needs to be used in `extends`
4151
export type AnyActorInstance = ActorInstance<any, any, any, any>;
@@ -1013,6 +1023,16 @@ export class ActorInstance<S, CP, CS, V> {
10131023
* @param args - The arguments to send with the event.
10141024
*/
10151025
_broadcast<Args extends Array<unknown>>(name: string, ...args: Args) {
1026+
return this._broadcastWithOptions({}, name, ...args);
1027+
}
1028+
1029+
/**
1030+
* Broadcasts an event to all connected clients with options.
1031+
* @param opts - Options for the broadcast.
1032+
* @param name - The name of the event.
1033+
* @param args - The arguments to send with the event.
1034+
*/
1035+
_broadcastWithOptions<Args extends Array<unknown>>(opts: BroadcastInstanceOptions, name: string, ...args: Args) {
10161036
this.#assertReady();
10171037

10181038
// Send to all connected clients
@@ -1028,8 +1048,14 @@ export class ActorInstance<S, CP, CS, V> {
10281048
},
10291049
});
10301050

1051+
const excludeList = opts.exclude ?? [];
1052+
10311053
// Send message to clients
10321054
for (const connection of subscriptions) {
1055+
if (excludeList.includes(connection.id)) {
1056+
continue;
1057+
}
1058+
10331059
connection._sendMessage(toClientSerializer);
10341060
}
10351061
}

0 commit comments

Comments
 (0)