|
1 | 1 | import type * as FN from '@soketi/impl/types'; |
| 2 | +import { Router as BaseRouter } from '../router'; |
2 | 3 |
|
3 | | -export class Router { |
4 | | - static protocols = new Map<string, { handler: Function; }>(); |
5 | | - |
| 4 | +export class Router extends BaseRouter { |
6 | 5 | static ON_NEW_CONNECTION = 'onNewConnection'; |
7 | 6 | static ON_CONNECTION_CLOSED = 'onConnectionClosed'; |
8 | 7 | static ON_MESSAGE = 'onMessage'; |
9 | 8 | static ON_ERROR = 'onError'; |
10 | 9 |
|
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 { |
16 | 11 | this.registerHandler(this.ON_NEW_CONNECTION, cb); |
17 | 12 | } |
18 | 13 |
|
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 { |
20 | 15 | this.registerHandler(this.ON_CONNECTION_CLOSED, cb); |
21 | 16 | } |
22 | 17 |
|
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 { |
24 | 19 | this.registerHandler(this.ON_MESSAGE, cb); |
25 | 20 | } |
26 | 21 |
|
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 { |
28 | 23 | this.registerHandler(this.ON_ERROR, cb); |
29 | 24 | } |
30 | 25 |
|
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); |
37 | 28 | } |
38 | 29 |
|
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); |
41 | 32 | } |
42 | 33 |
|
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); |
45 | 36 | } |
46 | 37 |
|
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); |
49 | 40 | } |
50 | 41 | } |
0 commit comments