Skip to content

Commit

Permalink
Implemented global router
Browse files Browse the repository at this point in the history
Added extra args for handlers in WS
  • Loading branch information
rennokki committed Jun 27, 2023
1 parent c2020a8 commit ef57d5d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
15 changes: 15 additions & 0 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export class Router {
static protocols = new Map<string, { handler: Function; }>();

static registerHandler(protocol: string, handler: Function): void {
this.protocols.set(protocol, { handler });
}

static getProtocol(protocol: string): { handler: Function; } {
return this.protocols.get(protocol);
}

static async handle(protocol: string, ...args): Promise<void> {
await this.getProtocol(protocol).handler(...args);
}
}
37 changes: 14 additions & 23 deletions src/ws/router.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,41 @@
import type * as FN from '@soketi/impl/types';
import { Router as BaseRouter } from '../router';

export class Router {
static protocols = new Map<string, { handler: Function; }>();

export class Router extends BaseRouter {
static ON_NEW_CONNECTION = 'onNewConnection';
static ON_CONNECTION_CLOSED = 'onConnectionClosed';
static ON_MESSAGE = 'onMessage';
static ON_ERROR = 'onError';

static registerHandler(protocol: string, handler: Function): void {
this.protocols.set(protocol, { handler });
}

static onNewConnection(cb: (connection: FN.WS.Connection) => any): void {
static onNewConnection(cb: (connection: FN.WS.Connection, ...args) => any): void {
this.registerHandler(this.ON_NEW_CONNECTION, cb);
}

static onConnectionClosed(cb: (connection: FN.WS.Connection, code?: number, message?: any) => any): void {
static onConnectionClosed(cb: (connection: FN.WS.Connection, code?: number, message?: any, ...args) => any): void {
this.registerHandler(this.ON_CONNECTION_CLOSED, cb);
}

static onMessage(cb: (connection: FN.WS.Connection, message: any) => any): void {
static onMessage(cb: (connection: FN.WS.Connection, message: any, ...args) => any): void {
this.registerHandler(this.ON_MESSAGE, cb);
}

static onError(cb: (connection: FN.WS.Connection, error: any) => any): void {
static onError(cb: (connection: FN.WS.Connection, error: any, ...args) => any): void {
this.registerHandler(this.ON_ERROR, cb);
}

static async handleNewConnection(connection: FN.WS.Connection): Promise<void> {
await this.getProtocol(this.ON_NEW_CONNECTION).handler(connection);
}

static async handleConnectionClosed(connection: FN.WS.Connection, code?: number, message?: any): Promise<void> {
await this.getProtocol(this.ON_CONNECTION_CLOSED).handler(connection, code, message);
static async handleNewConnection(connection: FN.WS.Connection, ...args): Promise<void> {
await this.handle(this.ON_NEW_CONNECTION, connection, ...args);
}

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

static async handleError(connection: FN.WS.Connection, error: any): Promise<void> {
await this.getProtocol(this.ON_ERROR).handler(connection, error);
static async handleMessage(connection: FN.WS.Connection, message: any, ...args): Promise<void> {
await this.handle(this.ON_MESSAGE, connection, message, ...args);
}

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

0 comments on commit ef57d5d

Please sign in to comment.