Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/Allow async events add url mutation #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
dist
coverage

.idea
46 changes: 28 additions & 18 deletions src/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -72,6 +72,10 @@ export class Websocket {
return this._url;
}

set url(url: string) {
this._url = url;
}

/**
* Getter for the protocols.
*
Expand Down Expand Up @@ -349,25 +353,31 @@ export class Websocket {
* @param type of the event to dispatch.
* @param event to dispatch.
*/
private dispatchEvent<K extends WebsocketEvent>(
private async dispatchEvent<K extends WebsocketEvent>(
type: K,
event: WebsocketEventMap[K],
) {
const eventListeners: WebsocketEventListeners[K] =
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
}
Expand All @@ -378,13 +388,13 @@ export class Websocket {
* @param type of the event to handle.
* @param event to handle.
*/
private handleEvent<K extends WebsocketEvent>(
private async handleEvent<K extends WebsocketEvent>(
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;

Expand All @@ -399,22 +409,22 @@ export class Websocket {
new CustomEvent<ReconnectEventDetail>(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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down