Add configurable provider auto-reconnect time. (#2215)
This commit is contained in:
@@ -75,7 +75,7 @@ export default class WsProvider implements WSProviderInterface {
|
||||
|
||||
readonly #waitingForId: Record<string, JsonRpcResponse> = {};
|
||||
|
||||
#autoConnect: boolean;
|
||||
#autoConnectMs: number;
|
||||
|
||||
#isConnected = false;
|
||||
|
||||
@@ -87,16 +87,16 @@ export default class WsProvider implements WSProviderInterface {
|
||||
* @param {string} endpoint The endpoint url. Usually `ws://ip:9944` or `wss://ip:9944`
|
||||
* @param {boolean} autoConnect Whether to connect automatically or not.
|
||||
*/
|
||||
constructor (endpoint: string = defaults.WS_URL, autoConnect = true) {
|
||||
constructor (endpoint: string = defaults.WS_URL, autoConnectMs: number | false = 1000) {
|
||||
assert(/^(wss|ws):\/\//.test(endpoint), `Endpoint should start with 'ws://', received '${endpoint}'`);
|
||||
|
||||
this.#eventemitter = new EventEmitter();
|
||||
this.#autoConnect = autoConnect;
|
||||
this.#autoConnectMs = autoConnectMs || 0;
|
||||
this.#coder = new Coder();
|
||||
this.#endpoint = endpoint;
|
||||
this.#websocket = null;
|
||||
|
||||
if (autoConnect) {
|
||||
if (autoConnectMs > 0) {
|
||||
this.connect();
|
||||
}
|
||||
}
|
||||
@@ -143,7 +143,7 @@ export default class WsProvider implements WSProviderInterface {
|
||||
}
|
||||
|
||||
// switch off autoConnect, we are in manual mode now
|
||||
this.#autoConnect = false;
|
||||
this.#autoConnectMs = 0;
|
||||
|
||||
// 1000 - Normal closure; the connection successfully completed
|
||||
this.#websocket.close(1000);
|
||||
@@ -267,17 +267,17 @@ export default class WsProvider implements WSProviderInterface {
|
||||
}
|
||||
|
||||
#onSocketClose = (event: CloseEvent): void => {
|
||||
if (this.#autoConnect) {
|
||||
if (this.#autoConnectMs > 0) {
|
||||
l.error(`disconnected from ${this.#endpoint} code: '${event.code}' reason: '${event.reason}'`);
|
||||
}
|
||||
|
||||
this.#isConnected = false;
|
||||
this.#emit('disconnected');
|
||||
|
||||
if (this.#autoConnect) {
|
||||
if (this.#autoConnectMs > 0) {
|
||||
setTimeout((): void => {
|
||||
this.connect();
|
||||
}, 1000);
|
||||
}, this.#autoConnectMs);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,19 +20,19 @@ describe('onConnect', (): void => {
|
||||
});
|
||||
|
||||
it('Does not connect when autoConnect is false', (): void => {
|
||||
const provider: WsProvider = new WsProvider(TEST_WS_URL, false);
|
||||
const provider: WsProvider = new WsProvider(TEST_WS_URL, 0);
|
||||
|
||||
expect(provider.isConnected()).toBe(false);
|
||||
});
|
||||
|
||||
it('Does connect when autoConnect is true', (): void => {
|
||||
const provider: WsProvider = new WsProvider(TEST_WS_URL, true);
|
||||
const provider: WsProvider = new WsProvider(TEST_WS_URL, 1000);
|
||||
|
||||
expect(provider.isConnected()).not.toBe(true);
|
||||
});
|
||||
|
||||
it('Creates a new WebSocket instance by calling the connect() method', (): void => {
|
||||
const provider: WsProvider = new WsProvider(TEST_WS_URL, false);
|
||||
const provider: WsProvider = new WsProvider(TEST_WS_URL, 0);
|
||||
|
||||
expect(provider.isConnected()).toBe(false);
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import { mockWs, TEST_WS_URL } from '../../test/mockWs';
|
||||
let ws: WsProvider;
|
||||
let mock: Mock;
|
||||
|
||||
function createWs (requests: any[], autoConnect: boolean | undefined): WsProvider {
|
||||
function createWs (requests: any[], autoConnect: number | undefined = 1000): WsProvider {
|
||||
mock = mockWs(requests);
|
||||
ws = new WsProvider(TEST_WS_URL, autoConnect);
|
||||
|
||||
@@ -25,7 +25,7 @@ describe('Ws', (): void => {
|
||||
|
||||
it('returns the connected state', (): void => {
|
||||
expect(
|
||||
createWs([], true).isConnected()
|
||||
createWs([]).isConnected()
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -9,7 +9,7 @@ import { mockWs, TEST_WS_URL } from '../../test/mockWs';
|
||||
let ws: WsProvider;
|
||||
let mock: Mock;
|
||||
|
||||
function createWs (requests: any[], autoConnect: boolean | undefined): WsProvider {
|
||||
function createWs (requests: any[], autoConnect: number | undefined = 1000): WsProvider {
|
||||
mock = mockWs(requests);
|
||||
ws = new WsProvider(TEST_WS_URL, autoConnect);
|
||||
|
||||
@@ -30,7 +30,7 @@ describe('onOpen', (): void => {
|
||||
reply: {
|
||||
result: 'ok'
|
||||
}
|
||||
}], false);
|
||||
}], 0);
|
||||
const sendPromise = ws.send('test_queue', []);
|
||||
|
||||
ws.connect();
|
||||
|
||||
@@ -17,7 +17,7 @@ function createMock (requests: any[]): void {
|
||||
mock = mockWs(requests);
|
||||
}
|
||||
|
||||
function createWs (autoConnect = true): WsProvider {
|
||||
function createWs (autoConnect = 1000): WsProvider {
|
||||
provider = new WsProvider(TEST_WS_URL, autoConnect);
|
||||
|
||||
return provider;
|
||||
@@ -47,7 +47,7 @@ describe('send', (): void => {
|
||||
}
|
||||
}]);
|
||||
|
||||
return createWs(true)
|
||||
return createWs()
|
||||
.send('test_encoding', [{ error: 'send error' }])
|
||||
.catch((error): void => {
|
||||
expect(error.message).toEqual('send error');
|
||||
@@ -63,7 +63,7 @@ describe('send', (): void => {
|
||||
}
|
||||
}]);
|
||||
|
||||
return createWs(true)
|
||||
return createWs()
|
||||
.send('test_body', ['param'])
|
||||
.then((): void => {
|
||||
expect(
|
||||
@@ -81,7 +81,7 @@ describe('send', (): void => {
|
||||
id: 1
|
||||
}]);
|
||||
|
||||
return createWs(true)
|
||||
return createWs()
|
||||
.send('test_error', [])
|
||||
.catch((error): void => {
|
||||
expect(error.message).toMatch(/666: error/);
|
||||
@@ -97,7 +97,7 @@ describe('send', (): void => {
|
||||
}
|
||||
}]);
|
||||
|
||||
return createWs(true)
|
||||
return createWs()
|
||||
.send('test_sub', [])
|
||||
.then((id): void => {
|
||||
expect(id).toEqual(1);
|
||||
|
||||
@@ -7,13 +7,13 @@ import WsProvider from './';
|
||||
describe('state', (): void => {
|
||||
it('requires an ws:// prefixed endpoint', (): void => {
|
||||
expect(
|
||||
(): WsProvider => new WsProvider('http://', false)
|
||||
(): WsProvider => new WsProvider('http://', 0)
|
||||
).toThrow(/with 'ws/);
|
||||
});
|
||||
|
||||
it('allows wss:// endpoints', (): void => {
|
||||
expect(
|
||||
(): WsProvider => new WsProvider('wss://', false)
|
||||
(): WsProvider => new WsProvider('wss://', 0)
|
||||
).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -17,7 +17,7 @@ function createMock (requests: any[]): void {
|
||||
mock = mockWs(requests);
|
||||
}
|
||||
|
||||
function createWs (autoConnect = true): WsProvider {
|
||||
function createWs (autoConnect = 1000): WsProvider {
|
||||
ws = new WsProvider(TEST_WS_URL, autoConnect);
|
||||
|
||||
return ws;
|
||||
@@ -47,7 +47,7 @@ describe('subscribe', (): void => {
|
||||
}
|
||||
}]);
|
||||
|
||||
return createWs(true)
|
||||
return createWs()
|
||||
.subscribe('type', 'test_sub', [], (cb): void => {
|
||||
expect(cb).toEqual(expect.anything());
|
||||
})
|
||||
|
||||
@@ -17,7 +17,7 @@ function createMock (requests: any): void {
|
||||
mock = mockWs(requests);
|
||||
}
|
||||
|
||||
function createWs (autoConnect = true): WsProvider {
|
||||
function createWs (autoConnect = 1000): WsProvider {
|
||||
ws = new WsProvider(TEST_WS_URL, autoConnect);
|
||||
|
||||
return ws;
|
||||
@@ -56,7 +56,7 @@ describe('subscribe', (): void => {
|
||||
}
|
||||
]);
|
||||
|
||||
const ws = createWs(true);
|
||||
const ws = createWs();
|
||||
|
||||
return ws
|
||||
.subscribe('test', 'subscribe_test', [], (cb): void => {
|
||||
@@ -76,7 +76,7 @@ describe('subscribe', (): void => {
|
||||
}
|
||||
}]);
|
||||
|
||||
const ws = createWs(true);
|
||||
const ws = createWs();
|
||||
|
||||
return ws
|
||||
.subscribe('test', 'subscribe_test', [], (cb): void => {
|
||||
|
||||
Reference in New Issue
Block a user