Skip to content

Commit

Permalink
feat: add init() and close() methods
Browse files Browse the repository at this point in the history
These extension points may be used by another adapter, in order to open
or close a connection to a database for example.

In Socket.IO v2, the join() method did accept a callback:

```js
socket.join("room1", () => {
  io.to("room1").emit("hello");
});
```

Depending on the adapter, it may now return a promise:

```js
await socket.join("room1");
io.to("room1").emit("hello");
```

Related: socketio/socket.io#3662
  • Loading branch information
darrachequesne committed Oct 20, 2020
1 parent 5f417d2 commit 2e023bf
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,24 @@ export class Adapter extends EventEmitter {
this.encoder = nsp.server.encoder;
}

/**
* To be overridden
*/
public init(): Promise<void> | void {}

/**
* To be overridden
*/
public close(): Promise<void> | void {}

/**
* Adds a socket to a list of room.
*
* @param {SocketId} id the socket id
* @param {Set<Room>} rooms a set of rooms
* @public
*/
public addAll(id: SocketId, rooms: Set<Room>): void {
public addAll(id: SocketId, rooms: Set<Room>): Promise<void> | void {
for (const room of rooms) {
if (!this.sids.has(id)) {
this.sids.set(id, new Set());
Expand All @@ -59,7 +69,7 @@ export class Adapter extends EventEmitter {
* @param {SocketId} id the socket id
* @param {Room} room the room name
*/
public del(id: SocketId, room: Room): void {
public del(id: SocketId, room: Room): Promise<void> | void {
if (this.sids.has(id)) {
this.sids.get(id).delete(room);
}
Expand Down

0 comments on commit 2e023bf

Please sign in to comment.