Skip to content

Commit 45a1dd4

Browse files
authored
refactor: add event type for use() (#4138)
1 parent 3a07207 commit 45a1dd4

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

lib/socket.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ export interface Handshake {
108108
auth: { [key: string]: any };
109109
}
110110

111+
type Event = [eventName: string, ...args: any[]];
112+
111113
export class Socket<
112114
ListenEvents extends EventsMap = DefaultEventsMap,
113115
EmitEvents extends EventsMap = ListenEvents,
@@ -130,8 +132,7 @@ export class Socket<
130132
private readonly server: Server<ListenEvents, EmitEvents, ServerSideEvents>;
131133
private readonly adapter: Adapter;
132134
private acks: Map<number, () => void> = new Map();
133-
private fns: Array<(event: Array<any>, next: (err?: Error) => void) => void> =
134-
[];
135+
private fns: Array<(event: Event, next: (err?: Error) => void) => void> = [];
135136
private flags: BroadcastFlags = {};
136137
private _anyListeners?: Array<(...args: any[]) => void>;
137138

@@ -569,7 +570,7 @@ export class Socket<
569570
* @param {Array} event - event that will get emitted
570571
* @private
571572
*/
572-
private dispatch(event: [eventName: string, ...args: any[]]): void {
573+
private dispatch(event: Event): void {
573574
debug("dispatching an event %j", event);
574575
this.run(event, (err) => {
575576
process.nextTick(() => {
@@ -592,9 +593,7 @@ export class Socket<
592593
* @return {Socket} self
593594
* @public
594595
*/
595-
public use(
596-
fn: (event: Array<any>, next: (err?: Error) => void) => void
597-
): this {
596+
public use(fn: (event: Event, next: (err?: Error) => void) => void): this {
598597
this.fns.push(fn);
599598
return this;
600599
}
@@ -606,10 +605,7 @@ export class Socket<
606605
* @param {Function} fn - last fn call in the middleware
607606
* @private
608607
*/
609-
private run(
610-
event: [eventName: string, ...args: any[]],
611-
fn: (err: Error | null) => void
612-
): void {
608+
private run(event: Event, fn: (err: Error | null) => void): void {
613609
const fns = this.fns.slice(0);
614610
if (!fns.length) return fn(null);
615611

0 commit comments

Comments
 (0)