reconnecting-websocket
Advanced tools
Comparing version 1.3.0 to 1.3.2
"use strict"; | ||
var isGlobalWebSocket = function () { | ||
return typeof WebSocket !== 'undefined' | ||
&& WebSocket.prototype | ||
&& WebSocket.prototype.CLOSING === 2; | ||
}; | ||
var getDefaultOptions = function () { return ({ | ||
constructor: (typeof WebSocket === 'function') ? WebSocket : null, | ||
constructor: isGlobalWebSocket() ? WebSocket : null, | ||
maxReconnectionDelay: 10000, | ||
@@ -5,0 +10,0 @@ minReconnectionDelay: 1500, |
35
index.ts
@@ -1,3 +0,18 @@ | ||
const getDefaultOptions = () => ({ | ||
constructor: (typeof WebSocket === 'function') ? WebSocket : null, | ||
type Options = { | ||
constructor?: new(url: string, protocols?: string | string[]) => WebSocket; | ||
maxReconnectionDelay?: number; | ||
minReconnectionDelay?: number; | ||
reconnectionDelayGrowFactor?: number; | ||
connectionTimeout?: number; | ||
maxRetries?: number; | ||
debug?: boolean; | ||
}; | ||
const isGlobalWebSocket = () => | ||
typeof WebSocket !== 'undefined' | ||
&& WebSocket.prototype | ||
&& WebSocket.prototype.CLOSING === 2; | ||
const getDefaultOptions = () => <Options>({ | ||
constructor: isGlobalWebSocket() ? WebSocket : null, | ||
maxReconnectionDelay: 10000, | ||
@@ -20,7 +35,7 @@ minReconnectionDelay: 1500, | ||
const initReconnectionDelay = (config) => | ||
const initReconnectionDelay = (config: Options) => | ||
(config.minReconnectionDelay + Math.random() * config.minReconnectionDelay); | ||
const updateReconnectionDelay = (config, previousDelay) => { | ||
let newDelay = previousDelay * config.reconnectionDelayGrowFactor; | ||
const updateReconnectionDelay = (config: Options, previousDelay: number) => { | ||
const newDelay = previousDelay * config.reconnectionDelayGrowFactor; | ||
return (newDelay > config.maxReconnectionDelay) | ||
@@ -33,3 +48,3 @@ ? config.maxReconnectionDelay | ||
const reassignEventListeners = (ws, oldWs, listeners) => { | ||
const reassignEventListeners = (ws: WebSocket, oldWs, listeners) => { | ||
Object.keys(listeners).forEach(type => { | ||
@@ -48,5 +63,5 @@ listeners[type].forEach(([listener, options]) => { | ||
protocols?: string|string[], | ||
options: Object = {} | ||
options = <Options>{} | ||
) { | ||
let ws; | ||
let ws: WebSocket; | ||
let connectingTimeout; | ||
@@ -148,3 +163,3 @@ let reconnectDelay = 0; | ||
this.addEventListener = (type: string, listener: Function, options: any) => { | ||
this.addEventListener = (type: string, listener: EventListener, options: any) => { | ||
if (Array.isArray(listeners[type])) { | ||
@@ -160,3 +175,3 @@ if (!listeners[type].some(([l]) => l === listener)) { | ||
this.removeEventListener = (type: string, listener: Function, options: any) => { | ||
this.removeEventListener = (type: string, listener: EventListener, options: any) => { | ||
if (Array.isArray(listeners[type])) { | ||
@@ -163,0 +178,0 @@ listeners[type] = listeners[type].filter(([l]) => l !== listener); |
{ | ||
"name": "reconnecting-websocket", | ||
"version": "1.3.0", | ||
"version": "1.3.2", | ||
"description": "Reconnecting WebSocket", | ||
"main": "build/index.js", | ||
"typings": "build/index.d.ts", | ||
"scripts": { | ||
@@ -30,2 +31,3 @@ "build": "tsc", | ||
"opn-cli": "^3.1.0", | ||
"typescript": "1.8.10", | ||
"ws": "^1.1.1" | ||
@@ -32,0 +34,0 @@ }, |
@@ -63,3 +63,3 @@ # Reconnecting WebSocket | ||
Options should be self explainatory | ||
Options should be self explanatory | ||
@@ -66,0 +66,0 @@ ```javascript |
@@ -7,7 +7,9 @@ { | ||
"sourceMap": false, | ||
"outDir": "build" | ||
"outDir": "build", | ||
"declaration": true | ||
}, | ||
"exclude": [ | ||
"node_modules" | ||
"node_modules", | ||
"build" | ||
] | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
69303
16
757
6