Skip to content

Commit ef57d5d

Browse files
committed
Implemented global router
Added extra args for handlers in WS
1 parent c2020a8 commit ef57d5d

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

src/router.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export class Router {
2+
static protocols = new Map<string, { handler: Function; }>();
3+
4+
static registerHandler(protocol: string, handler: Function): void {
5+
this.protocols.set(protocol, { handler });
6+
}
7+
8+
static getProtocol(protocol: string): { handler: Function; } {
9+
return this.protocols.get(protocol);
10+
}
11+
12+
static async handle(protocol: string, ...args): Promise<void> {
13+
await this.getProtocol(protocol).handler(...args);
14+
}
15+
}

src/ws/router.ts

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,41 @@
11
import type * as FN from '@soketi/impl/types';
2+
import { Router as BaseRouter } from '../router';
23

3-
export class Router {
4-
static protocols = new Map<string, { handler: Function; }>();
5-
4+
export class Router extends BaseRouter {
65
static ON_NEW_CONNECTION = 'onNewConnection';
76
static ON_CONNECTION_CLOSED = 'onConnectionClosed';
87
static ON_MESSAGE = 'onMessage';
98
static ON_ERROR = 'onError';
109

11-
static registerHandler(protocol: string, handler: Function): void {
12-
this.protocols.set(protocol, { handler });
13-
}
14-
15-
static onNewConnection(cb: (connection: FN.WS.Connection) => any): void {
10+
static onNewConnection(cb: (connection: FN.WS.Connection, ...args) => any): void {
1611
this.registerHandler(this.ON_NEW_CONNECTION, cb);
1712
}
1813

19-
static onConnectionClosed(cb: (connection: FN.WS.Connection, code?: number, message?: any) => any): void {
14+
static onConnectionClosed(cb: (connection: FN.WS.Connection, code?: number, message?: any, ...args) => any): void {
2015
this.registerHandler(this.ON_CONNECTION_CLOSED, cb);
2116
}
2217

23-
static onMessage(cb: (connection: FN.WS.Connection, message: any) => any): void {
18+
static onMessage(cb: (connection: FN.WS.Connection, message: any, ...args) => any): void {
2419
this.registerHandler(this.ON_MESSAGE, cb);
2520
}
2621

27-
static onError(cb: (connection: FN.WS.Connection, error: any) => any): void {
22+
static onError(cb: (connection: FN.WS.Connection, error: any, ...args) => any): void {
2823
this.registerHandler(this.ON_ERROR, cb);
2924
}
3025

31-
static async handleNewConnection(connection: FN.WS.Connection): Promise<void> {
32-
await this.getProtocol(this.ON_NEW_CONNECTION).handler(connection);
33-
}
34-
35-
static async handleConnectionClosed(connection: FN.WS.Connection, code?: number, message?: any): Promise<void> {
36-
await this.getProtocol(this.ON_CONNECTION_CLOSED).handler(connection, code, message);
26+
static async handleNewConnection(connection: FN.WS.Connection, ...args): Promise<void> {
27+
await this.handle(this.ON_NEW_CONNECTION, connection, ...args);
3728
}
3829

39-
static async handleMessage(connection: FN.WS.Connection, message: any): Promise<void> {
40-
await this.getProtocol(this.ON_MESSAGE).handler(connection, message);
30+
static async handleConnectionClosed(connection: FN.WS.Connection, code?: number, message?: any, ...args): Promise<void> {
31+
await this.handle(this.ON_CONNECTION_CLOSED, connection, code, message, ...args);
4132
}
4233

43-
static async handleError(connection: FN.WS.Connection, error: any): Promise<void> {
44-
await this.getProtocol(this.ON_ERROR).handler(connection, error);
34+
static async handleMessage(connection: FN.WS.Connection, message: any, ...args): Promise<void> {
35+
await this.handle(this.ON_MESSAGE, connection, message, ...args);
4536
}
4637

47-
static getProtocol(protocol: string): { handler: Function; } {
48-
return this.protocols.get(protocol);
38+
static async handleError(connection: FN.WS.Connection, error: any, ...args): Promise<void> {
39+
await this.handle(this.ON_ERROR, connection, error, ...args);
4940
}
5041
}

0 commit comments

Comments
 (0)