@based/client
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -25,3 +25,4 @@ import Emitter from './Emitter'; | ||
}); | ||
connect(url: string | (() => Promise<string>)): void; | ||
connect(url?: string | (() => Promise<string>)): void; | ||
private _url; | ||
opts: BasedOpts; | ||
@@ -28,0 +29,0 @@ disconnect(): void; |
@@ -65,3 +65,11 @@ "use strict"; | ||
connect(url) { | ||
this.client.connection = (0, websocket_1.default)(this.client, url); | ||
if (!url && this._url) { | ||
if (!this.client.connection) { | ||
this.client.connection = (0, websocket_1.default)(this.client, this._url); | ||
} | ||
} | ||
else { | ||
this._url = url; | ||
this.client.connection = (0, websocket_1.default)(this.client, url); | ||
} | ||
} | ||
@@ -71,2 +79,4 @@ disconnect() { | ||
this.client.connection.disconnected = true; | ||
console.info('DESTROY'); | ||
this.client.connection.destroy(); | ||
if (this.client.connection.ws) { | ||
@@ -73,0 +83,0 @@ this.client.connection.ws.close(); |
@@ -8,3 +8,27 @@ "use strict"; | ||
const isomorphic_ws_1 = __importDefault(require("isomorphic-ws")); | ||
const connect = (client, url, connection = {}, time = 0, reconnect = false) => { | ||
const activityListeners = new Map(); | ||
let activeTimer; | ||
if (typeof window !== 'undefined') { | ||
document.addEventListener('visibilitychange', function (event) { | ||
clearTimeout(activeTimer); | ||
if (document.hidden) { | ||
activeTimer = setTimeout(() => { | ||
activityListeners.forEach((fn) => { | ||
fn(false); | ||
}); | ||
}, 30e3); | ||
} | ||
else { | ||
activityListeners.forEach((fn) => { | ||
fn(true); | ||
}); | ||
} | ||
}); | ||
} | ||
const connect = (client, url, connection = { | ||
destroy: () => { | ||
console.info('remove'); | ||
activityListeners.delete(connection); | ||
}, | ||
}, time = 0, reconnect = false) => { | ||
(0, urlLoader_1.default)(url, (realUrl) => { | ||
@@ -15,2 +39,17 @@ setTimeout(() => { | ||
} | ||
let isActive = true; | ||
activityListeners.set(connection, (active) => { | ||
if (!connection.disconnected) { | ||
if (!active && isActive) { | ||
console.warn('Send to background - close connection'); | ||
isActive = false; | ||
client.onClose(); | ||
ws.close(); | ||
} | ||
else if (!isActive && active) { | ||
activityListeners.delete(connection); | ||
connect(client, url, connection, 0, true); | ||
} | ||
} | ||
}); | ||
const ws = (connection.ws = new isomorphic_ws_1.default(realUrl)); | ||
@@ -20,17 +59,21 @@ ws.onerror = () => { }; | ||
ws.onopen = () => { | ||
if (connection.disconnected) { | ||
return; | ||
if (isActive) { | ||
if (connection.disconnected) { | ||
return; | ||
} | ||
time = 100; | ||
if (reconnect) { | ||
client.onReconnect(); | ||
} | ||
client.onOpen(); | ||
} | ||
time = 100; | ||
if (reconnect) { | ||
client.onReconnect(); | ||
} | ||
client.onOpen(); | ||
}; | ||
ws.onclose = () => { | ||
if (connection.disconnected) { | ||
return; | ||
if (isActive) { | ||
if (connection.disconnected) { | ||
return; | ||
} | ||
client.onClose(); | ||
connect(client, url, connection, Math.min(1250, Math.min(time + 500)), true); | ||
} | ||
client.onClose(); | ||
connect(client, url, connection, Math.min(1250, Math.min(time + 500)), true); | ||
}; | ||
@@ -37,0 +80,0 @@ }, time); |
/// <reference types="ws" /> | ||
import WebSocket from 'isomorphic-ws'; | ||
export declare type Connection = { | ||
export declare class Connection { | ||
ws?: WebSocket; | ||
disconnected?: boolean; | ||
}; | ||
destroy: () => void; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Connection = void 0; | ||
class Connection { | ||
} | ||
exports.Connection = Connection; | ||
//# sourceMappingURL=types.js.map |
{ | ||
"name": "@based/client", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
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
141213
2388