Skip to content

Commit

Permalink
Allow client to always use native setTimeout function. This allows so…
Browse files Browse the repository at this point in the history
…cket.io client to be used in testing infrastructure (such as Karma) where the clock may be mocked.
  • Loading branch information
vartan committed Jul 1, 2021
1 parent b466c6f commit 6461b35
Show file tree
Hide file tree
Showing 4 changed files with 16,882 additions and 64 deletions.
20 changes: 18 additions & 2 deletions lib/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ interface EngineOptions {
* @default true
*/
closeOnBeforeunload: boolean;

/**
* Whether to always use the native setTimeout function. This allows the
* client to reconnect when the native setTimeout function is overridden,
* such as when mock clocks are installed.
* @default false
*/
useNativeSetTimeout: boolean;
}

export interface ManagerOptions extends EngineOptions {
Expand Down Expand Up @@ -321,6 +329,7 @@ export class Manager<
private nsps: Record<string, Socket> = {};
private subs: Array<ReturnType<typeof on>> = [];
private backoff: Backoff;
private setTimeoutFn: typeof setTimeout;
private _reconnection: boolean;
private _reconnectionAttempts: number;
private _reconnectionDelay: number;
Expand Down Expand Up @@ -375,6 +384,9 @@ export class Manager<
this.encoder = new _parser.Encoder();
this.decoder = new _parser.Decoder();
this._autoConnect = opts.autoConnect !== false;
this.setTimeoutFn = opts.useNativeSetTimeout
? NATIVE_SET_TIMEOUT
: setTimeout;
if (this._autoConnect) this.open();
}

Expand Down Expand Up @@ -542,7 +554,7 @@ export class Manager<
}

// set timer
const timer = setTimeout(() => {
const timer = this.setTimeoutFn(() => {
debug("connect attempt timed out after %d", timeout);
openSubDestroy();
socket.close();
Expand Down Expand Up @@ -769,7 +781,7 @@ export class Manager<
debug("will wait %dms before reconnect attempt", delay);

this._reconnecting = true;
const timer = setTimeout(() => {
const timer = this.setTimeoutFn(() => {
if (self.skipReconnect) return;

debug("attempting reconnect");
Expand Down Expand Up @@ -813,3 +825,7 @@ export class Manager<
this.emitReserved("reconnect", attempt);
}
}

// Keep a reference to the real setTimeout function so it can be used when
// setTimeout is overridden.
const NATIVE_SET_TIMEOUT = setTimeout;
Loading

0 comments on commit 6461b35

Please sign in to comment.