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

Allow client to always use native setTimeout function. #1479

Closed
wants to merge 7 commits into from
Closed
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
19 changes: 17 additions & 2 deletions lib/manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as eio from "engine.io-client";
import { installTimeoutFunctions } from "engine.io-client/lib/util";
import { Socket, SocketOptions } from "./socket";
import * as parser from "socket.io-parser";
import { Decoder, Encoder, Packet } from "socket.io-parser";
Expand Down Expand Up @@ -203,6 +204,14 @@ interface EngineOptions {
* @default true
*/
closeOnBeforeunload: boolean;

/**
* Whether to always use the native timeouts. This allows the client to
* reconnect when the native timeout functions are overridden, such as when
* mock clocks are installed.
* @default false
*/
useNativeTimeouts: boolean;
}

export interface ManagerOptions extends EngineOptions {
Expand Down Expand Up @@ -321,6 +330,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 @@ -358,6 +368,7 @@ export class Manager<

opts.path = opts.path || "/socket.io";
this.opts = opts;
installTimeoutFunctions(this, opts);
this.reconnection(opts.reconnection !== false);
this.reconnectionAttempts(opts.reconnectionAttempts || Infinity);
this.reconnectionDelay(opts.reconnectionDelay || 1000);
Expand Down Expand Up @@ -542,7 +553,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 +780,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 +824,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