1
- const EventEmitter = require ( "events" ) ;
1
+ import { EventEmitter } from "events" ;
2
+
3
+ export type SocketId = string ;
4
+ export type Room = string ;
5
+
6
+ export interface BroadcastFlags {
7
+ volatile ?: boolean ;
8
+ compress ?: boolean ;
9
+ local ?: boolean ;
10
+ broadcast ?: boolean ;
11
+ binary ?: boolean ;
12
+ }
13
+
14
+ export interface BroadcastOptions {
15
+ rooms : Set < Room > ;
16
+ except ?: Set < SocketId > ;
17
+ flags ?: BroadcastFlags ;
18
+ }
19
+
20
+ export class Adapter extends EventEmitter {
21
+ protected rooms : Map < Room , Set < SocketId > > = new Map ( ) ;
22
+ protected sids : Map < SocketId , Set < Room > > = new Map ( ) ;
23
+ private readonly encoder ;
2
24
3
- class Adapter extends EventEmitter {
4
25
/**
5
26
* In-memory adapter constructor.
6
27
*
7
28
* @param {Namespace } nsp
8
- * @public
9
29
*/
10
- constructor ( nsp ) {
30
+ constructor ( readonly nsp : any ) {
11
31
super ( ) ;
12
- this . nsp = nsp ;
13
- this . rooms = new Map ( ) ; // Map<Room, Set<SocketId>>
14
- this . sids = new Map ( ) ; // Map<SocketId, Set<Room>>
15
32
this . encoder = nsp . server . encoder ;
16
33
}
17
34
18
35
/**
19
36
* Adds a socket to a list of room.
20
37
*
21
- * @param {string } id the socket id
22
- * @param {Set<string > } rooms a set of rooms
38
+ * @param {SocketId } id the socket id
39
+ * @param {Set<Room > } rooms a set of rooms
23
40
* @public
24
41
*/
25
- addAll ( id , rooms ) {
42
+ public addAll ( id : SocketId , rooms : Set < Room > ) : void {
26
43
for ( const room of rooms ) {
27
44
if ( ! this . sids . has ( id ) ) {
28
45
this . sids . set ( id , new Set ( ) ) ;
@@ -39,11 +56,10 @@ class Adapter extends EventEmitter {
39
56
/**
40
57
* Removes a socket from a room.
41
58
*
42
- * @param {string } id the socket id
43
- * @param {string } room the room name
44
- * @public
59
+ * @param {SocketId } id the socket id
60
+ * @param {Room } room the room name
45
61
*/
46
- del ( id , room ) {
62
+ public del ( id : SocketId , room : Room ) : void {
47
63
if ( this . sids . has ( id ) ) {
48
64
this . sids . get ( id ) . delete ( room ) ;
49
65
}
@@ -57,10 +73,9 @@ class Adapter extends EventEmitter {
57
73
/**
58
74
* Removes a socket from all rooms it's joined.
59
75
*
60
- * @param {string } id the socket id
61
- * @public
76
+ * @param {SocketId } id the socket id
62
77
*/
63
- delAll ( id ) {
78
+ public delAll ( id : SocketId ) : void {
64
79
if ( ! this . sids . has ( id ) ) {
65
80
return ;
66
81
}
@@ -87,9 +102,9 @@ class Adapter extends EventEmitter {
87
102
* @param {Object } opts the options
88
103
* @public
89
104
*/
90
- broadcast ( packet , opts ) {
105
+ public broadcast ( packet : any , opts : BroadcastOptions ) : void {
91
106
const rooms = opts . rooms ;
92
- const except = opts . except || [ ] ;
107
+ const except = opts . except || new Set ( ) ;
93
108
const flags = opts . flags || { } ;
94
109
const packetOpts = {
95
110
preEncoded : true ,
@@ -105,7 +120,7 @@ class Adapter extends EventEmitter {
105
120
if ( ! this . rooms . has ( room ) ) continue ;
106
121
107
122
for ( const id of this . rooms . get ( room ) ) {
108
- if ( ids . has ( id ) || ~ except . indexOf ( id ) ) continue ;
123
+ if ( ids . has ( id ) || except . has ( id ) ) continue ;
109
124
const socket = this . nsp . connected . get ( id ) ;
110
125
if ( socket ) {
111
126
socket . packet ( encodedPackets , packetOpts ) ;
@@ -115,7 +130,7 @@ class Adapter extends EventEmitter {
115
130
}
116
131
} else {
117
132
for ( const [ id ] of this . sids ) {
118
- if ( ~ except . indexOf ( id ) ) continue ;
133
+ if ( except . has ( id ) ) continue ;
119
134
const socket = this . nsp . connected . get ( id ) ;
120
135
if ( socket ) socket . packet ( encodedPackets , packetOpts ) ;
121
136
}
@@ -126,11 +141,10 @@ class Adapter extends EventEmitter {
126
141
/**
127
142
* Gets a list of sockets by sid.
128
143
*
129
- * @param {Set<string> } rooms the explicit set of rooms to check.
130
- * @public
144
+ * @param {Set<Room> } rooms the explicit set of rooms to check.
131
145
*/
132
- sockets ( rooms ) {
133
- const sids = new Set ( ) ;
146
+ public sockets ( rooms : Set < Room > ) : Promise < Set < SocketId > > {
147
+ const sids = new Set < SocketId > ( ) ;
134
148
135
149
if ( rooms . size ) {
136
150
for ( const room of rooms ) {
@@ -148,18 +162,15 @@ class Adapter extends EventEmitter {
148
162
}
149
163
}
150
164
151
- return sids ;
165
+ return Promise . resolve ( sids ) ;
152
166
}
153
167
154
168
/**
155
169
* Gets the list of rooms a given socket has joined.
156
170
*
157
- * @param {String } id the socket id
158
- * @public
171
+ * @param {SocketId } id the socket id
159
172
*/
160
- socketRooms ( id ) {
173
+ public socketRooms ( id : SocketId ) : Set < Room > | undefined {
161
174
return this . sids . get ( id ) ;
162
175
}
163
176
}
164
-
165
- module . exports = Adapter ;
0 commit comments