Skip to content

Commit

Permalink
refactor(typings): make typing information more specific (#1418)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-fong authored and darrachequesne committed Dec 7, 2020
1 parent adffc03 commit 226b491
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 84 deletions.
17 changes: 10 additions & 7 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = exports = lookup;
/**
* Managers cache.
*/
const cache = (exports.managers = {});
const cache: Record<string, Manager> = (exports.managers = {});

/**
* Looks up an existing `Manager` for multiplexing.
Expand All @@ -27,31 +27,34 @@ const cache = (exports.managers = {});
*
* @public
*/
function lookup(opts?: Partial<ManagerOptions | SocketOptions>): Socket;
function lookup(opts?: Partial<ManagerOptions & SocketOptions>): Socket;
function lookup(
uri: string,
opts?: Partial<ManagerOptions | SocketOptions>
opts?: Partial<ManagerOptions & SocketOptions>
): Socket;
function lookup(uri: any, opts?: any): Socket {
function lookup(
uri: string | Partial<ManagerOptions & SocketOptions>,
opts?: Partial<ManagerOptions & SocketOptions>
): Socket {
if (typeof uri === "object") {
opts = uri;
uri = undefined;
}

opts = opts || {};

const parsed = url(uri);
const parsed = url(uri as string);
const source = parsed.source;
const id = parsed.id;
const path = parsed.path;
const sameNamespace = cache[id] && path in cache[id].nsps;
const sameNamespace = cache[id] && path in cache[id]["nsps"];
const newConnection =
opts.forceNew ||
opts["force new connection"] ||
false === opts.multiplex ||
sameNamespace;

let io;
let io: Manager;

if (newConnection) {
debug("ignoring socket cache for %s", source);
Expand Down
64 changes: 35 additions & 29 deletions lib/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as eio from "engine.io-client";
import { Socket, SocketOptions } from "./socket";
import Emitter = require("component-emitter");
import * as parser from "socket.io-parser";
import { Decoder, Encoder } from "socket.io-parser";
import { Decoder, Encoder, Packet } from "socket.io-parser";
import { on } from "./on";
import * as bind from "component-bind";
import * as Backoff from "backo2";
Expand Down Expand Up @@ -280,11 +280,11 @@ export class Manager extends Emitter {
_reconnecting: boolean;

private readonly uri: string;
private readonly opts: object;
private readonly opts: Partial<ManagerOptions>;

private nsps: object = {};
private subs: Array<any> = [];
private backoff: any;
private nsps: Record<string, Socket> = {};
private subs: Array<ReturnType<typeof on>> = [];
private backoff: Backoff;
private _reconnection: boolean;
private _reconnectionAttempts: number;
private _reconnectionDelay: number;
Expand All @@ -300,13 +300,16 @@ export class Manager extends Emitter {
/**
* `Manager` constructor.
*
* @param {String} uri - engine instance or engine uri/opts
* @param {Object} opts - options
* @param uri - engine instance or engine uri/opts
* @param opts - options
* @public
*/
constructor(opts: Partial<ManagerOptions>);
constructor(uri?: string, opts?: Partial<ManagerOptions>);
constructor(uri?: any, opts?: any) {
constructor(
uri?: string | Partial<ManagerOptions>,
opts?: Partial<ManagerOptions>
) {
super();
if (uri && "object" === typeof uri) {
opts = uri;
Expand All @@ -328,7 +331,7 @@ export class Manager extends Emitter {
});
this.timeout(null == opts.timeout ? 20000 : opts.timeout);
this._readyState = "closed";
this.uri = uri;
this.uri = uri as string;
const _parser = opts.parser || parser;
this.encoder = new _parser.Encoder();
this.decoder = new _parser.Decoder();
Expand Down Expand Up @@ -378,46 +381,47 @@ export class Manager extends Emitter {
public reconnectionDelay(v?: number): Manager | number {
if (v === undefined) return this._reconnectionDelay;
this._reconnectionDelay = v;
this.backoff && this.backoff.setMin(v);
this.backoff?.setMin(v);
return this;
}

/**
* Sets the randomization factor
*
* @param {Number} v - the randomization factor
* @return {Manager} self or value
* @param v - the randomization factor
* @return self or value
* @public
*/
public randomizationFactor(v: number): Manager;
public randomizationFactor(): number;
public randomizationFactor(v?: number): Manager | number {
if (v === undefined) return this._randomizationFactor;
this._randomizationFactor = v;
this.backoff && this.backoff.setJitter(v);
this.backoff?.setJitter(v);
return this;
}

/**
* Sets the maximum delay between reconnections.
*
* @param {Number} v - delay
* @return {Manager} self or value
* @param v - delay
* @return self or value
* @public
*/
public reconnectionDelayMax(v: number): Manager;
public reconnectionDelayMax(): number;
public reconnectionDelayMax(v?: number): Manager | number {
if (v === undefined) return this._reconnectionDelayMax;
this._reconnectionDelayMax = v;
this.backoff && this.backoff.setMax(v);
this.backoff?.setMax(v);
return this;
}

/**
* Sets the connection timeout. `false` to disable
*
* @return {Manager} self or value
* @param v - connection timeout
* @return self or value
* @public
*/
public timeout(v: number | boolean): Manager;
Expand Down Expand Up @@ -450,7 +454,7 @@ export class Manager extends Emitter {
* Sets the current transport `socket`.
*
* @param {Function} fn - optional, callback
* @return {Manager} self
* @return self
* @public
*/
public open(fn?: (err?: Error) => void): Manager {
Expand Down Expand Up @@ -540,11 +544,13 @@ export class Manager extends Emitter {

// add new subs
const socket = this.engine;
this.subs.push(on(socket, "data", bind(this, "ondata")));
this.subs.push(on(socket, "ping", bind(this, "onping")));
this.subs.push(on(socket, "error", bind(this, "onerror")));
this.subs.push(on(socket, "close", bind(this, "onclose")));
this.subs.push(on(this.decoder, "decoded", bind(this, "ondecoded")));
this.subs.push(
on(socket, "data", bind(this, "ondata")),
on(socket, "ping", bind(this, "onping")),
on(socket, "error", bind(this, "onerror")),
on(socket, "close", bind(this, "onclose")),
on(this.decoder, "decoded", bind(this, "ondecoded"))
);
}

/**
Expand Down Expand Up @@ -616,10 +622,10 @@ export class Manager extends Emitter {
/**
* Called upon a socket close.
*
* @param {Socket} socket
* @param socket
* @private
*/
_destroy(socket) {
_destroy(socket: Socket) {
const index = this.connecting.indexOf(socket);
if (~index) this.connecting.splice(index, 1);
if (this.connecting.length) return;
Expand All @@ -630,14 +636,14 @@ export class Manager extends Emitter {
/**
* Writes a packet.
*
* @param {Object} packet
* @param packet
* @private
*/
_packet(packet) {
_packet(packet: Partial<Packet & { query: string; options: any }>) {
debug("writing packet %j", packet);
if (packet.query && packet.type === 0) packet.nsp += "?" + packet.query;

const encodedPackets = this.encoder.encode(packet);
const encodedPackets = this.encoder.encode(packet as Packet);
for (let i = 0; i < encodedPackets.length; i++) {
this.engine.write(encodedPackets[i], packet.options);
}
Expand Down Expand Up @@ -693,7 +699,7 @@ export class Manager extends Emitter {
*
* @private
*/
private onclose(reason) {
private onclose(reason: string) {
debug("onclose");

this.cleanup();
Expand Down
6 changes: 4 additions & 2 deletions lib/on.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export function on(obj, ev, fn) {
import Emitter = require("component-emitter");

export function on(obj: Emitter, ev: string, fn: (err?: any) => any) {
obj.on(ev, fn);
return {
destroy: function () {
obj.removeListener(ev, fn);
obj.off(ev, fn);
},
};
}
Loading

0 comments on commit 226b491

Please sign in to comment.