diff --git a/.gitignore b/.gitignore index 7227482..534b9e5 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules dist coverage +.idea \ No newline at end of file diff --git a/src/websocket.ts b/src/websocket.ts index 343dca9..2e4a9ba 100644 --- a/src/websocket.ts +++ b/src/websocket.ts @@ -16,7 +16,7 @@ import { WebsocketOptions } from "./websocket_options"; * A websocket wrapper that can be configured to reconnect automatically and buffer messages when the websocket is not connected. */ export class Websocket { - private readonly _url: string; // the url to connect to + private _url: string; // the url to connect to private readonly _protocols?: string | string[]; // the protocols to use private _closedByUser: boolean = false; // whether the websocket was closed by the user @@ -72,6 +72,10 @@ export class Websocket { return this._url; } + set url(url: string) { + this._url = url; + } + /** * Getter for the protocols. * @@ -349,7 +353,7 @@ export class Websocket { * @param type of the event to dispatch. * @param event to dispatch. */ - private dispatchEvent( + private async dispatchEvent( type: K, event: WebsocketEventMap[K], ) { @@ -357,17 +361,23 @@ export class Websocket { this._options.listeners[type]; const newEventListeners: WebsocketEventListeners[K] = []; - eventListeners.forEach(({ listener, options }) => { - listener(this, event); // invoke listener with event + await Promise.all( + eventListeners.map(async ({ listener, options }) => { + if (listener.constructor.name === "AsyncFunction") { + await listener(this, event); // invoke listener with event + } else { + listener(this, event); // invoke listener with event + } - if ( - options === undefined || - options.once === undefined || - !options.once - ) { - newEventListeners.push({ listener, options }); // only keep listener if it isn't a once-listener - } - }); + if ( + options === undefined || + options.once === undefined || + !options.once + ) { + newEventListeners.push({ listener, options }); // only keep listener if it isn't a once-listener + } + }), + ); this._options.listeners[type] = newEventListeners; // replace old listeners with new listeners that don't include once-listeners } @@ -378,13 +388,13 @@ export class Websocket { * @param type of the event to handle. * @param event to handle. */ - private handleEvent( + private async handleEvent( type: K, event: WebsocketEventMap[K], ) { switch (type) { case WebsocketEvent.close: - this.dispatchEvent(type, event); + await this.dispatchEvent(type, event); this.scheduleConnectionRetryIfNeeded(); // schedule a new connection retry if the websocket was closed by the server break; @@ -399,22 +409,22 @@ export class Websocket { new CustomEvent(WebsocketEvent.reconnect, { detail, }); - this.dispatchEvent(WebsocketEvent.reconnect, event); + await this.dispatchEvent(WebsocketEvent.reconnect, event); this.backoff.reset(); } this._lastConnection = new Date(); - this.dispatchEvent(type, event); // dispatch open event and send buffered data + await this.dispatchEvent(type, event); // dispatch open event and send buffered data this.sendBufferedData(); break; case WebsocketEvent.retry: - this.dispatchEvent(type, event); // dispatch retry event and try to connect + await this.dispatchEvent(type, event); // dispatch retry event and try to connect this.clearWebsocket(); // clear the old websocket this.tryConnect(); break; default: - this.dispatchEvent(type, event); // dispatch event to all listeners of the given event-type + await this.dispatchEvent(type, event); // dispatch event to all listeners of the given event-type break; } } diff --git a/tsconfig.cjs.json b/tsconfig.cjs.json index 24b38f9..0543f5f 100644 --- a/tsconfig.cjs.json +++ b/tsconfig.cjs.json @@ -4,7 +4,7 @@ /* Basic Options */ // "incremental": true, /* Enable incremental compilation */ - "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ + "target": "es2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ // "lib": [], /* Specify library files to be included in the compilation. */ // "allowJs": true, /* Allow javascript files to be compiled. */