Skip to content

Commit

Permalink
feat: add type information to socket.data (#4159)
Browse files Browse the repository at this point in the history
Usage:

```js
interface SocketData {
  name: string;
  age: number;
}

const io = new Server<ClientToServerEvents, ServerToClientEvents, InterServerEvents, SocketData>();

io.on("connection", (socket) => {
  socket.data.name = "john";
  socket.data.age = 42;
});
```
  • Loading branch information
backmeupplz authored Nov 8, 2021
1 parent ed8483d commit fe8730c
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 39 deletions.
22 changes: 15 additions & 7 deletions lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,27 @@ interface WriteOptions {
export class Client<
ListenEvents extends EventsMap,
EmitEvents extends EventsMap,
ServerSideEvents extends EventsMap
ServerSideEvents extends EventsMap,
SocketData = any
> {
public readonly conn: RawSocket;

private readonly id: string;
private readonly server: Server<ListenEvents, EmitEvents, ServerSideEvents>;
private readonly server: Server<
ListenEvents,
EmitEvents,
ServerSideEvents,
SocketData
>;
private readonly encoder: Encoder;
private readonly decoder: Decoder;
private sockets: Map<
SocketId,
Socket<ListenEvents, EmitEvents, ServerSideEvents>
Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>
> = new Map();
private nsps: Map<
string,
Socket<ListenEvents, EmitEvents, ServerSideEvents>
Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>
> = new Map();
private connectTimeout?: NodeJS.Timeout;

Expand All @@ -47,7 +53,7 @@ export class Client<
* @package
*/
constructor(
server: Server<ListenEvents, EmitEvents, ServerSideEvents>,
server: Server<ListenEvents, EmitEvents, ServerSideEvents, SocketData>,
conn: any
) {
this.server = server;
Expand Down Expand Up @@ -112,7 +118,7 @@ export class Client<
auth,
(
dynamicNspName:
| Namespace<ListenEvents, EmitEvents, ServerSideEvents>
| Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData>
| false
) => {
if (dynamicNspName) {
Expand Down Expand Up @@ -171,7 +177,9 @@ export class Client<
*
* @private
*/
_remove(socket: Socket<ListenEvents, EmitEvents, ServerSideEvents>): void {
_remove(
socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>
): void {
if (this.sockets.has(socket.id)) {
const nsp = this.sockets.get(socket.id)!.nsp.name;
this.sockets.delete(socket.id);
Expand Down
19 changes: 14 additions & 5 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,23 @@ interface ServerOptions extends EngineOptions, AttachOptions {
export class Server<
ListenEvents extends EventsMap = DefaultEventsMap,
EmitEvents extends EventsMap = ListenEvents,
ServerSideEvents extends EventsMap = DefaultEventsMap
ServerSideEvents extends EventsMap = DefaultEventsMap,
SocketData = any
> extends StrictEventEmitter<
ServerSideEvents,
EmitEvents,
ServerReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents>
ServerReservedEventsMap<
ListenEvents,
EmitEvents,
ServerSideEvents,
SocketData
>
> {
public readonly sockets: Namespace<
ListenEvents,
EmitEvents,
ServerSideEvents
ServerSideEvents,
SocketData
>;
/**
* A reference to the underlying Engine.IO server.
Expand Down Expand Up @@ -504,7 +511,9 @@ export class Server<
*/
public of(
name: string | RegExp | ParentNspNameMatchFn,
fn?: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents>) => void
fn?: (
socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>
) => void
): Namespace<ListenEvents, EmitEvents, ServerSideEvents> {
if (typeof name === "function" || name instanceof RegExp) {
const parentNsp = new ParentNamespace(this);
Expand Down Expand Up @@ -568,7 +577,7 @@ export class Server<
*/
public use(
fn: (
socket: Socket<ListenEvents, EmitEvents, ServerSideEvents>,
socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>,
next: (err?: ExtendedError) => void
) => void
): this {
Expand Down
52 changes: 35 additions & 17 deletions lib/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,56 +21,72 @@ export interface ExtendedError extends Error {
export interface NamespaceReservedEventsMap<
ListenEvents extends EventsMap,
EmitEvents extends EventsMap,
ServerSideEvents extends EventsMap
ServerSideEvents extends EventsMap,
SocketData
> {
connect: (socket: Socket<ListenEvents, EmitEvents, ServerSideEvents>) => void;
connect: (
socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>
) => void;
connection: (
socket: Socket<ListenEvents, EmitEvents, ServerSideEvents>
socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>
) => void;
}

export interface ServerReservedEventsMap<
ListenEvents,
EmitEvents,
ServerSideEvents
ServerSideEvents,
SocketData
> extends NamespaceReservedEventsMap<
ListenEvents,
EmitEvents,
ServerSideEvents
ServerSideEvents,
SocketData
> {
new_namespace: (
namespace: Namespace<ListenEvents, EmitEvents, ServerSideEvents>
namespace: Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData>
) => void;
}

export const RESERVED_EVENTS: ReadonlySet<string | Symbol> = new Set<
keyof ServerReservedEventsMap<never, never, never>
keyof ServerReservedEventsMap<never, never, never, never>
>(<const>["connect", "connection", "new_namespace"]);

export class Namespace<
ListenEvents extends EventsMap = DefaultEventsMap,
EmitEvents extends EventsMap = ListenEvents,
ServerSideEvents extends EventsMap = DefaultEventsMap
ServerSideEvents extends EventsMap = DefaultEventsMap,
SocketData = any
> extends StrictEventEmitter<
ServerSideEvents,
EmitEvents,
NamespaceReservedEventsMap<ListenEvents, EmitEvents, ServerSideEvents>
NamespaceReservedEventsMap<
ListenEvents,
EmitEvents,
ServerSideEvents,
SocketData
>
> {
public readonly name: string;
public readonly sockets: Map<
SocketId,
Socket<ListenEvents, EmitEvents, ServerSideEvents>
Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>
> = new Map();

public adapter: Adapter;

/** @private */
readonly server: Server<ListenEvents, EmitEvents, ServerSideEvents>;
readonly server: Server<
ListenEvents,
EmitEvents,
ServerSideEvents,
SocketData
>;

/** @private */
_fns: Array<
(
socket: Socket<ListenEvents, EmitEvents, ServerSideEvents>,
socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>,
next: (err?: ExtendedError) => void
) => void
> = [];
Expand All @@ -85,7 +101,7 @@ export class Namespace<
* @param name
*/
constructor(
server: Server<ListenEvents, EmitEvents, ServerSideEvents>,
server: Server<ListenEvents, EmitEvents, ServerSideEvents, SocketData>,
name: string
) {
super();
Expand Down Expand Up @@ -114,7 +130,7 @@ export class Namespace<
*/
public use(
fn: (
socket: Socket<ListenEvents, EmitEvents, ServerSideEvents>,
socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>,
next: (err?: ExtendedError) => void
) => void
): this {
Expand All @@ -130,7 +146,7 @@ export class Namespace<
* @private
*/
private run(
socket: Socket<ListenEvents, EmitEvents, ServerSideEvents>,
socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>,
fn: (err: ExtendedError | null) => void
) {
const fns = this._fns.slice(0);
Expand Down Expand Up @@ -195,7 +211,7 @@ export class Namespace<
client: Client<ListenEvents, EmitEvents, ServerSideEvents>,
query,
fn?: () => void
): Socket<ListenEvents, EmitEvents, ServerSideEvents> {
): Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData> {
debug("adding socket to nsp %s", this.name);
const socket = new Socket(this, client, query);
this.run(socket, (err) => {
Expand Down Expand Up @@ -238,7 +254,9 @@ export class Namespace<
*
* @private
*/
_remove(socket: Socket<ListenEvents, EmitEvents, ServerSideEvents>): void {
_remove(
socket: Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>
): void {
if (this.sockets.has(socket.id)) {
this.sockets.delete(socket.id);
} else {
Expand Down
16 changes: 10 additions & 6 deletions lib/parent-namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ import type { BroadcastOptions } from "socket.io-adapter";
export class ParentNamespace<
ListenEvents extends EventsMap = DefaultEventsMap,
EmitEvents extends EventsMap = ListenEvents,
ServerSideEvents extends EventsMap = DefaultEventsMap
> extends Namespace<ListenEvents, EmitEvents, ServerSideEvents> {
ServerSideEvents extends EventsMap = DefaultEventsMap,
SocketData = any
> extends Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData> {
private static count: number = 0;
private children: Set<Namespace<ListenEvents, EmitEvents, ServerSideEvents>> =
new Set();
private children: Set<
Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData>
> = new Set();

constructor(server: Server<ListenEvents, EmitEvents, ServerSideEvents>) {
constructor(
server: Server<ListenEvents, EmitEvents, ServerSideEvents, SocketData>
) {
super(server, "/_" + ParentNamespace.count++);
}

Expand Down Expand Up @@ -47,7 +51,7 @@ export class ParentNamespace<

createChild(
name: string
): Namespace<ListenEvents, EmitEvents, ServerSideEvents> {
): Namespace<ListenEvents, EmitEvents, ServerSideEvents, SocketData> {
const namespace = new Namespace(this.server, name);
namespace._fns = this._fns.slice(0);
this.listeners("connect").forEach((listener) =>
Expand Down
14 changes: 10 additions & 4 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export interface EventEmitterReservedEventsMap {

export const RESERVED_EVENTS: ReadonlySet<string | Symbol> = new Set<
| ClientReservedEvents
| keyof NamespaceReservedEventsMap<never, never, never>
| keyof NamespaceReservedEventsMap<never, never, never, never>
| keyof SocketReservedEventsMap
| keyof EventEmitterReservedEventsMap
>(<const>[
Expand Down Expand Up @@ -113,7 +113,8 @@ type Event = [eventName: string, ...args: any[]];
export class Socket<
ListenEvents extends EventsMap = DefaultEventsMap,
EmitEvents extends EventsMap = ListenEvents,
ServerSideEvents extends EventsMap = DefaultEventsMap
ServerSideEvents extends EventsMap = DefaultEventsMap,
SocketData = any
> extends StrictEventEmitter<
ListenEvents,
EmitEvents,
Expand All @@ -124,12 +125,17 @@ export class Socket<
/**
* Additional information that can be attached to the Socket instance and which will be used in the fetchSockets method
*/
public data: any = {};
public data: Partial<SocketData> = {};

public connected: boolean;
public disconnected: boolean;

private readonly server: Server<ListenEvents, EmitEvents, ServerSideEvents>;
private readonly server: Server<
ListenEvents,
EmitEvents,
ServerSideEvents,
SocketData
>;
private readonly adapter: Adapter;
private acks: Map<number, () => void> = new Map();
private fns: Array<(event: Event, next: (err?: Error) => void) => void> = [];
Expand Down

0 comments on commit fe8730c

Please sign in to comment.