Skip to content

Commit

Permalink
Bug fixed with event listeners | 2.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
WarstekHUN committed Dec 24, 2023
1 parent d696794 commit 3ff6771
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/package",
"name": "zilaws-server",
"version": "2.0.1",
"version": "2.0.2",
"description": "ZilaWS is a blazingly fast and very lightweight library that provides an extremely easy-to-use way to transmit data via websockets between client-side and server-side using eventhandlers and async waiters.",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
41 changes: 21 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ interface IServerSettings {
) => ZilaClient;
}

interface IServerEvents {
interface IServerEvents<T extends ZilaClient> {
/**
* Runs every time a client connects.
* @param socket
* @param req
* @returns
*/
onClientConnect: (socket: ZilaClient) => void;
onClientConnect: (socket: T) => void;

/**
* Runs every time a client disconnects
Expand All @@ -97,7 +97,7 @@ interface IServerEvents {
* @param reason
* @returns
*/
onClientDisconnect: (socket: ZilaClient, code: number, reason: string) => void;
onClientDisconnect: (socket: T, code: number, reason: string) => void;

/**
* Runs every time after a the server processes a message from the client.
Expand All @@ -106,7 +106,7 @@ interface IServerEvents {
* @param message If the message object is instance of T, this param will be T, undefined if not.
* @returns
*/
onClientMessage: <T>(socket: ZilaClient, eventHandlerName: string, messageDataObject: T | undefined) => void;
onClientMessage: <T>(socket: T, eventHandlerName: string, messageDataObject: T | undefined) => void;

/**
* Runs every time a server recieves a message from the client before any registered callback could run
Expand All @@ -115,10 +115,10 @@ interface IServerEvents {
* @param message If the message object is instance of T, this param will be T, undefined if not.
* @returns
*/
onClientMessageBeforeCallback: <T>(
socket: ZilaClient,
onClientMessageBeforeCallback: <U>(
socket: T,
eventHandlerName: string,
messageDataObject: T | undefined
messageDataObject: U | undefined
) => void;

/**
Expand All @@ -127,15 +127,15 @@ interface IServerEvents {
* @param rawMessage Not processed, raw message from the client. (Hopefully JSON)
* @returns
*/
onClientRawMessageBeforeCallback: (socket: ZilaClient, rawMessage: string) => void;
onClientRawMessageBeforeCallback: (socket: T, rawMessage: string) => void;

/**
* Runs when a client calls `syncCookies`.
* @param socket socket.cookies contain the newly synced cookies.
* @param cookiesBeforeSync The cookies before syncing
* @returns
*/
onCookieSync: (socket: ZilaClient, cookiesBeforeSync: Map<string, string>) => void;
onCookieSync: (socket: T, cookiesBeforeSync: Map<string, string>) => void;
}

export class ZilaServer<T extends ZilaClient = ZilaClient> {
Expand All @@ -147,7 +147,7 @@ export class ZilaServer<T extends ZilaClient = ZilaClient> {
private clientClass: new (
socket: WebSocketClient,
ip: string | undefined,
server: ZilaServer,
server: ZilaServer<T>,
isBrowser: boolean,
headers: { [name: string]: string },
cookies?: Map<string, string>
Expand All @@ -158,7 +158,7 @@ export class ZilaServer<T extends ZilaClient = ZilaClient> {
private baseServer: ServerHTTP | ServerHTTPS;

private serverEvents: {
[K in keyof IServerEvents]?: Array<IServerEvents[K]> | undefined;
[K in keyof IServerEvents<T>]?: Array<IServerEvents<T>[K]> | undefined;
} = {};

private readonly callbacks: { [id: string]: ZilaWSCallback<ZilaClient> | undefined } = {};
Expand Down Expand Up @@ -213,6 +213,7 @@ export class ZilaServer<T extends ZilaClient = ZilaClient> {
public constructor(settings: IServerSettings) {
this.settings = settings;
this.hasrequested = false;
// @ts-ignore
this.clientClass = settings.clientClass ?? ZilaClient;
if (settings.maxWaiterTime) this.maxWaiterTime = settings.maxWaiterTime;

Expand Down Expand Up @@ -332,15 +333,15 @@ export class ZilaServer<T extends ZilaClient = ZilaClient> {

if (this.serverEvents.onClientConnect) {
for (const cb of this.serverEvents.onClientConnect) {
cb(zilaSocket);
cb(zilaSocket as T);
}
}

zilaSocket.socket.addListener("message", (data) => {
const datastring = data.toString();
if (this.serverEvents.onClientRawMessageBeforeCallback) {
for (const cb of this.serverEvents.onClientRawMessageBeforeCallback) {
cb(zilaSocket, datastring);
cb(zilaSocket as T, datastring);
}
}

Expand All @@ -351,7 +352,7 @@ export class ZilaServer<T extends ZilaClient = ZilaClient> {
zilaSocket.socket.addEventListener("close", (event) => {
if (this.serverEvents.onClientDisconnect) {
for (const cb of this.serverEvents.onClientDisconnect) {
cb(zilaSocket, event.code, event.reason);
cb(zilaSocket as T, event.code, event.reason);
}
}

Expand Down Expand Up @@ -381,7 +382,7 @@ export class ZilaServer<T extends ZilaClient = ZilaClient> {
* @param eventType
* @param callback
*/
public addEventListener<K extends keyof IServerEvents>(eventType: K, callback: IServerEvents[K]) {
public addEventListener<K extends keyof IServerEvents<T>>(eventType: K, callback: IServerEvents<T>[K]) {
let arr = this.serverEvents[eventType];
if (!arr) {
arr = [];
Expand All @@ -399,7 +400,7 @@ export class ZilaServer<T extends ZilaClient = ZilaClient> {
* @param callback
* @returns
*/
public removeEventListener<K extends keyof IServerEvents>(eventType: K, callback: IServerEvents[K]) {
public removeEventListener<K extends keyof IServerEvents<T>>(eventType: K, callback: IServerEvents<T>[K]) {
let arr = this.serverEvents[eventType];
if (!arr) return;

Expand All @@ -417,7 +418,7 @@ export class ZilaServer<T extends ZilaClient = ZilaClient> {
* @param eventType
* @param callback
*/
public onceEventListener<K extends keyof IServerEvents>(eventType: K, callback: IServerEvents[K]) {
public onceEventListener<K extends keyof IServerEvents<T>>(eventType: K, callback: IServerEvents<T>[K]) {
const that = this;

function onceCallback(...args: any[]) {
Expand Down Expand Up @@ -673,7 +674,7 @@ export class ZilaServer<T extends ZilaClient = ZilaClient> {

if (beforeCookies) {
for (const cb of this.serverEvents.onCookieSync!) {
cb(socket, beforeCookies);
cb(socket as T, beforeCookies);
}
}
} catch {
Expand All @@ -689,13 +690,13 @@ export class ZilaServer<T extends ZilaClient = ZilaClient> {

if (this.serverEvents.onClientRawMessageBeforeCallback) {
for (const cb of this.serverEvents.onClientRawMessageBeforeCallback) {
cb(socket, msg);
cb(socket as T, msg);
}
}

if (this.serverEvents.onClientMessageBeforeCallback) {
for (const cb of this.serverEvents.onClientMessageBeforeCallback) {
cb(socket, msgObj.identifier, msgObj.message);
cb(socket as T, msgObj.identifier, msgObj.message);
}
}

Expand Down

0 comments on commit 3ff6771

Please sign in to comment.