Skip to content

Commit b52b4ef

Browse files
refactor: migrate to TypeScript
1 parent 130f28a commit b52b4ef

File tree

7 files changed

+80
-533
lines changed

7 files changed

+80
-533
lines changed

.eslintrc.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
.nyc_output/
3+
dist/

index.js renamed to lib/index.ts

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
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;
224

3-
class Adapter extends EventEmitter {
425
/**
526
* In-memory adapter constructor.
627
*
728
* @param {Namespace} nsp
8-
* @public
929
*/
10-
constructor(nsp) {
30+
constructor(readonly nsp: any) {
1131
super();
12-
this.nsp = nsp;
13-
this.rooms = new Map(); // Map<Room, Set<SocketId>>
14-
this.sids = new Map(); // Map<SocketId, Set<Room>>
1532
this.encoder = nsp.server.encoder;
1633
}
1734

1835
/**
1936
* Adds a socket to a list of room.
2037
*
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
2340
* @public
2441
*/
25-
addAll(id, rooms) {
42+
public addAll(id: SocketId, rooms: Set<Room>): void {
2643
for (const room of rooms) {
2744
if (!this.sids.has(id)) {
2845
this.sids.set(id, new Set());
@@ -39,11 +56,10 @@ class Adapter extends EventEmitter {
3956
/**
4057
* Removes a socket from a room.
4158
*
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
4561
*/
46-
del(id, room) {
62+
public del(id: SocketId, room: Room): void {
4763
if (this.sids.has(id)) {
4864
this.sids.get(id).delete(room);
4965
}
@@ -57,10 +73,9 @@ class Adapter extends EventEmitter {
5773
/**
5874
* Removes a socket from all rooms it's joined.
5975
*
60-
* @param {string} id the socket id
61-
* @public
76+
* @param {SocketId} id the socket id
6277
*/
63-
delAll(id) {
78+
public delAll(id: SocketId): void {
6479
if (!this.sids.has(id)) {
6580
return;
6681
}
@@ -87,9 +102,9 @@ class Adapter extends EventEmitter {
87102
* @param {Object} opts the options
88103
* @public
89104
*/
90-
broadcast(packet, opts) {
105+
public broadcast(packet: any, opts: BroadcastOptions): void {
91106
const rooms = opts.rooms;
92-
const except = opts.except || [];
107+
const except = opts.except || new Set();
93108
const flags = opts.flags || {};
94109
const packetOpts = {
95110
preEncoded: true,
@@ -105,7 +120,7 @@ class Adapter extends EventEmitter {
105120
if (!this.rooms.has(room)) continue;
106121

107122
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;
109124
const socket = this.nsp.connected.get(id);
110125
if (socket) {
111126
socket.packet(encodedPackets, packetOpts);
@@ -115,7 +130,7 @@ class Adapter extends EventEmitter {
115130
}
116131
} else {
117132
for (const [id] of this.sids) {
118-
if (~except.indexOf(id)) continue;
133+
if (except.has(id)) continue;
119134
const socket = this.nsp.connected.get(id);
120135
if (socket) socket.packet(encodedPackets, packetOpts);
121136
}
@@ -126,11 +141,10 @@ class Adapter extends EventEmitter {
126141
/**
127142
* Gets a list of sockets by sid.
128143
*
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.
131145
*/
132-
sockets(rooms) {
133-
const sids = new Set();
146+
public sockets(rooms: Set<Room>): Promise<Set<SocketId>> {
147+
const sids = new Set<SocketId>();
134148

135149
if (rooms.size) {
136150
for (const room of rooms) {
@@ -148,18 +162,15 @@ class Adapter extends EventEmitter {
148162
}
149163
}
150164

151-
return sids;
165+
return Promise.resolve(sids);
152166
}
153167

154168
/**
155169
* Gets the list of rooms a given socket has joined.
156170
*
157-
* @param {String} id the socket id
158-
* @public
171+
* @param {SocketId} id the socket id
159172
*/
160-
socketRooms(id) {
173+
public socketRooms(id: SocketId): Set<Room> | undefined {
161174
return this.sids.get(id);
162175
}
163176
}
164-
165-
module.exports = Adapter;

0 commit comments

Comments
 (0)