Skip to content

Commit

Permalink
feat: add logger option (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain authored May 13, 2024
1 parent a8493d0 commit 4abec32
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions reconnecting-websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export type Event = Events.Event;
export type ErrorEvent = Events.ErrorEvent;
export type CloseEvent = Events.CloseEvent;

export type IRWLogger = {
log: (message: string, ...args: any[]) => void;
error: (message: string, ...args: any[]) => void;
};

export type Options = {
WebSocket?: any;
WebSocketOptions?: any | {};
Expand All @@ -36,6 +41,7 @@ export type Options = {
maxEnqueuedMessages?: number;
startClosed?: boolean;
debug?: boolean;
logger?: IRWLogger;
};

const DEFAULT = {
Expand Down Expand Up @@ -82,13 +88,21 @@ export default class ReconnectingWebSocket {
private readonly _protocols?: string | string[];
private readonly _options: Options;

private readonly _logger: IRWLogger;

constructor(url: UrlProvider, protocols?: string | string[], options: Options = {}) {
this._url = url;
this._protocols = protocols;
this._options = options;
if (this._options.startClosed) {
this._shouldReconnect = false;
}
if (options.logger) {
this._logger = options.logger;
} else {
this._logger = console;
}

this._connect();
}

Expand Down Expand Up @@ -301,11 +315,10 @@ export default class ReconnectingWebSocket {
}

private _debug(...args: any[]) {
if (this._options.debug) {
// not using spread because compiled version uses Symbols
// tslint:disable-next-line
console.log.apply(console, ['RWS>', ...args]);
if (!this._debug) {
return;
}
this._logger.log('RWS>', ...args);
}

private _getNextDelay() {
Expand Down Expand Up @@ -383,8 +396,8 @@ export default class ReconnectingWebSocket {
}
this._debug('connect', {url, protocols: this._protocols, options: this._options});
this._ws = this._protocols
? new WebSocket(url, this._protocols, this._options.WebSocketOptions)
: new WebSocket(url, this._options.WebSocketOptions);
? new WebSocket(url, this._protocols, this._options.WebSocketOptions)
: new WebSocket(url, this._options.WebSocketOptions);
this._ws!.binaryType = this._binaryType;
this._connectLock = false;
this._addListeners();
Expand All @@ -393,8 +406,8 @@ export default class ReconnectingWebSocket {
})
.catch(err => {
this._connectLock = false;
this._handleError(new Events.ErrorEvent(Error(err.message), this))
})
this._handleError(new Events.ErrorEvent(Error(err.message), this));
});
}

private _handleTimeout() {
Expand Down

0 comments on commit 4abec32

Please sign in to comment.