Provider emits connected/disconnected (#76)

* Add provider.on stub (mock/http)

* Emit connected/disconnected from ws

* Update dependencies

* Expose .on on api
This commit is contained in:
Jaco Greeff
2018-04-24 10:01:47 +02:00
committed by GitHub
parent 0012045cb2
commit 5b9c75b9cf
19 changed files with 79 additions and 14 deletions
+4 -1
View File
@@ -3,10 +3,11 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import type { ProviderInterface, ProviderInterface$Callback } from '../types';
import type { ProviderInterface, ProviderInterface$Callback, ProviderInterface$Emitted, ProviderInterface$EmitCb } from '../types';
require('./polyfill');
const on = require('./on');
const send = require('./send');
const subscribe = require('./subscribe');
const state = require('./state');
@@ -18,6 +19,8 @@ module.exports = function httpProvider (endpoint: string): ProviderInterface {
return {
isConnected: (): boolean =>
true,
on: (type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void =>
on(self, type, sub),
send: (method: string, params: Array<mixed>): Promise<mixed> =>
send(self, method, params),
subscribe: (method: string, params: Array<mixed>, cb: ProviderInterface$Callback): Promise<number> =>
+11
View File
@@ -0,0 +1,11 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { ProviderInterface$Emitted, ProviderInterface$EmitCb } from '../types';
import type { HttpState } from './types';
module.exports = function on (self: HttpState, type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void {
self.l.error(`Provider does not have 'on' emitters for type '${type}'`);
};
+4 -1
View File
@@ -3,9 +3,10 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import type { ProviderInterface } from '../types';
import type { ProviderInterface, ProviderInterface$Emitted, ProviderInterface$EmitCb } from '../types';
const mocks = require('./mocks');
const on = require('./on');
const send = require('./send');
const state = require('./state');
const subscribe = require('./subscribe');
@@ -19,6 +20,8 @@ module.exports = function mockProvider (): ProviderInterface {
return {
isConnected: (): boolean =>
true,
on: (type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void =>
on(self, type, sub),
send: (method: string, params: Array<mixed>): Promise<mixed> =>
send(self, method, params),
subscribe: (method: string, ...params: Array<mixed>): Promise<number> =>
+11
View File
@@ -0,0 +1,11 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { ProviderInterface$Emitted, ProviderInterface$EmitCb } from '../types';
import type { MockState } from './types';
module.exports = function on (self: MockState, type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void {
self.l.error(`Provider does not have 'on' emitters for type '${type}'`);
};
+6
View File
@@ -38,8 +38,14 @@ export type JsonRpcResponse = JsonRpcObject & JsonRpcResponseBase;
export type ProviderInterface$Callback = (error: ?Error, result: mixed) => void;
export type ProviderInterface$Emitted = 'connected' | 'disconnected';
// flowlint-next-line unclear-type:off
export type ProviderInterface$EmitCb = (value?: any) => any;
export type ProviderInterface = {
isConnected (): boolean,
on (type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void,
send (method: string, params: Array<mixed>): Promise<mixed>,
subscribe (method: string, params: Array<mixed>, cb: ProviderInterface$Callback): Promise<number>,
unsubscribe (method: string, id: number): Promise<boolean>
+4 -1
View File
@@ -3,11 +3,12 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import type { ProviderInterface, ProviderInterface$Callback } from '../types';
import type { ProviderInterface, ProviderInterface$Callback, ProviderInterface$Emitted, ProviderInterface$EmitCb } from '../types';
require('./polyfill');
const connect = require('./connect');
const on = require('./on');
const send = require('./send');
const state = require('./state');
const subscribe = require('./subscribe');
@@ -25,6 +26,8 @@ module.exports = function wsProvider (endpoint: string, autoConnect: boolean = t
connect(self),
isConnected: (): boolean =>
self.isConnected,
on: (type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void =>
on(self, type, sub),
send: (method: string, params: Array<mixed>): Promise<mixed> =>
send(self, method, params),
subscribe: (method: string, params: Array<mixed>, cb: ProviderInterface$Callback): Promise<number> =>
+11
View File
@@ -0,0 +1,11 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { ProviderInterface$Emitted, ProviderInterface$EmitCb } from '../types';
import type { WsState } from './types';
module.exports = function on (self: WsState, type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void {
self.emitter.on(type, sub);
};
+1
View File
@@ -10,6 +10,7 @@ module.exports = function onClose (self: WsState): () => void {
self.l.debug(() => ['disconnected from', self.endpoint]);
self.isConnected = false;
self.emitter.emit('disconnected');
if (self.autoConnect) {
setTimeout(() => {
+1
View File
@@ -10,6 +10,7 @@ module.exports = function onOpen (self: WsState): () => boolean {
self.l.debug(() => ['connected to', self.endpoint]);
self.isConnected = true;
self.emitter.emit('connected');
Object.keys(self.queued).forEach((id) => {
try {
+2
View File
@@ -5,6 +5,7 @@
import type { WsState } from './types';
const EventEmitter = require('eventemitter3');
const assert = require('@polkadot/util/assert');
const l = require('@polkadot/util/logger')('api-ws');
@@ -16,6 +17,7 @@ module.exports = function state (endpoint: string, autoConnect: boolean): $Shape
return {
autoConnect,
coder: coder(),
emitter: new EventEmitter(),
endpoint,
handlers: {},
isConnected: false,
+2
View File
@@ -3,6 +3,7 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import type EventEmitter from 'eventemitter3';
import type { Logger } from '@polkadot/util/types';
import type { RpcCoder } from '../coder/json/types';
import type { ProviderInterface$Callback } from '../types';
@@ -20,6 +21,7 @@ export type WsState = {
autoConnect: boolean,
coder: RpcCoder,
endpoint: string,
emitter: EventEmitter,
handlers: { [number]: WsState$Awaiting },
isConnected: boolean,
l: Logger,