@@ -6,6 +6,7 @@ import type {
6
6
DefaultEventsMap ,
7
7
EventNamesWithoutAck ,
8
8
} from "./typed-events" ;
9
+ import { Adapter } from "socket.io-adapter" ;
9
10
import type { BroadcastOptions } from "socket.io-adapter" ;
10
11
import debugModule from "debug" ;
11
12
@@ -33,7 +34,7 @@ export class ParentNamespace<
33
34
SocketData = any
34
35
> extends Namespace < ListenEvents , EmitEvents , ServerSideEvents , SocketData > {
35
36
private static count : number = 0 ;
36
- private children : Set <
37
+ private readonly children : Set <
37
38
Namespace < ListenEvents , EmitEvents , ServerSideEvents , SocketData >
38
39
> = new Set ( ) ;
39
40
@@ -47,13 +48,7 @@ export class ParentNamespace<
47
48
* @private
48
49
*/
49
50
_initAdapter ( ) : void {
50
- const broadcast = ( packet : any , opts : BroadcastOptions ) => {
51
- this . children . forEach ( ( nsp ) => {
52
- nsp . adapter . broadcast ( packet , opts ) ;
53
- } ) ;
54
- } ;
55
- // @ts -ignore FIXME is there a way to declare an inner class in TypeScript?
56
- this . adapter = { broadcast } ;
51
+ this . adapter = new ParentBroadcastAdapter ( this , this . children ) ;
57
52
}
58
53
59
54
public emit < Ev extends EventNamesWithoutAck < EmitEvents > > (
@@ -112,3 +107,19 @@ export class ParentNamespace<
112
107
throw new Error ( "fetchSockets() is not supported on parent namespaces" ) ;
113
108
}
114
109
}
110
+
111
+ /**
112
+ * A dummy adapter that only supports broadcasting to child (concrete) namespaces.
113
+ * @private file
114
+ */
115
+ class ParentBroadcastAdapter extends Adapter {
116
+ constructor ( parentNsp : any , private readonly children : Set < Namespace > ) {
117
+ super ( parentNsp ) ;
118
+ }
119
+
120
+ broadcast ( packet : any , opts : BroadcastOptions ) {
121
+ this . children . forEach ( ( nsp ) => {
122
+ nsp . adapter . broadcast ( packet , opts ) ;
123
+ } ) ;
124
+ }
125
+ }
0 commit comments