Compose with E3, not extend (#258)

* Compose with E3, not extend

* Cleanup docs
This commit is contained in:
Jaco Greeff
2018-10-11 18:47:55 +02:00
committed by GitHub
parent 4be4d8004f
commit e103e98f16
3 changed files with 36 additions and 17 deletions
+13 -5
View File
@@ -2,9 +2,9 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { ApiBaseInterface } from './types';
import { ApiBaseInterface, ApiInterface$Events } from './types';
import E3 from 'eventemitter3';
import EventEmitter from 'eventemitter3';
import WsProvider from '@polkadot/rpc-provider/ws';
import Rpc from '@polkadot/rpc-core/index';
import { Extrinsics } from '@polkadot/extrinsics/types';
@@ -29,7 +29,8 @@ const l = logger('api');
const INIT_ERROR = `Api needs to be initialised before using, listen on 'ready'`;
export default abstract class ApiBase<R, S, E> extends E3.EventEmitter implements ApiBaseInterface<R, S, E> {
export default abstract class ApiBase<R, S, E> implements ApiBaseInterface<R, S, E> {
private _eventemitter: EventEmitter;
protected _extrinsics?: E;
protected _genesisHash?: Hash;
protected _storage?: S;
@@ -54,8 +55,7 @@ export default abstract class ApiBase<R, S, E> extends E3.EventEmitter implement
* ```
*/
constructor (wsProvider?: WsProvider) {
super();
this._eventemitter = new EventEmitter();
this._rpcBase = new Rpc(wsProvider);
this._rpc = this.decorateRpc(this._rpcBase);
@@ -146,6 +146,14 @@ export default abstract class ApiBase<R, S, E> extends E3.EventEmitter implement
return this._extrinsics as E;
}
on (type: ApiInterface$Events, handler: (...args: Array<any>) => any): void {
this._eventemitter.on(type, handler);
}
protected emit (type: ApiInterface$Events, ...args: Array<any>): void {
this._eventemitter.emit(type, ...args);
}
private init (): void {
let isReady: boolean = false;
+10 -7
View File
@@ -8,7 +8,7 @@ import { JsonRpcResponse, ProviderInterface, ProviderInterface$Callback, Provide
import './polyfill';
import E3 from 'eventemitter3';
import EventEmitter from 'eventemitter3';
import assert from '@polkadot/util/assert';
import isNull from '@polkadot/util/is/null';
import isUndefined from '@polkadot/util/is/undefined';
@@ -55,7 +55,8 @@ interface WSProviderInterface extends ProviderInterface {
*
* @see [[HttpProvider]]
*/
export default class WsProvider extends E3.EventEmitter implements WSProviderInterface {
export default class WsProvider implements WSProviderInterface {
private _eventemitter: EventEmitter;
private autoConnect: boolean;
private coder: RpcCoder;
private endpoint: string;
@@ -77,10 +78,9 @@ export default class WsProvider extends E3.EventEmitter implements WSProviderInt
* @param {boolean} autoConnect Whether to connect automatically or not.
*/
constructor (endpoint: string, autoConnect: boolean = true) {
super();
assert(/^(wss|ws):\/\//.test(endpoint), `Endpoint should start with 'ws://', received '${endpoint}'`);
this._eventemitter = new EventEmitter();
this.autoConnect = autoConnect;
this.coder = coder();
this.endpoint = endpoint;
@@ -126,10 +126,9 @@ export default class WsProvider extends E3.EventEmitter implements WSProviderInt
* @summary Listens on events after having subscribed using the [[subscribe]] function.
* @param {ProviderInterface$Emitted} type Event
* @param {ProviderInterface$EmitCb} sub Callback
* @return {this} [description]
*/
on (type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): this {
return super.on(type, sub);
on (type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void {
this._eventemitter.on(type, sub);
}
/**
@@ -212,6 +211,10 @@ export default class WsProvider extends E3.EventEmitter implements WSProviderInt
return result as boolean;
}
private emit (type: ProviderInterface$Emitted, ...args: Array<any>): void {
this._eventemitter.emit(type, ...args);
}
private onSocketClose = (): void => {
this.l.debug(() => ['disconnected from', this.endpoint]);
+13 -5
View File
@@ -4,9 +4,9 @@
import { RpcInterface$Section } from '@polkadot/rpc-core/types';
import { ProviderInterface } from '@polkadot/rpc-provider/types';
import { RpcRxInterface, RpcRxInterface$Section } from './types';
import { RpcRxInterface, RpcRxInterface$Events, RpcRxInterface$Section } from './types';
import E3 from 'eventemitter3';
import EventEmitter from 'eventemitter3';
import { BehaviorSubject, ReplaySubject, Observable, Subscriber, from } from 'rxjs';
import Rpc from '@polkadot/rpc-core/index';
import isFunction from '@polkadot/util/is/function';
@@ -34,9 +34,10 @@ type CachedMap = {
* const api = new RpcRx(provider);
* ```
*/
export default class RpcRx extends E3.EventEmitter implements RpcRxInterface {
export default class RpcRx implements RpcRxInterface {
private _api: Rpc;
private _cacheMap: CachedMap;
private _eventemitter: EventEmitter;
private _isConnected: BehaviorSubject<boolean>;
readonly author: RpcRxInterface$Section;
readonly chain: RpcRxInterface$Section;
@@ -47,12 +48,11 @@ export default class RpcRx extends E3.EventEmitter implements RpcRxInterface {
* @param {ProviderInterface} provider An API provider using HTTP or WebSocket
*/
constructor (providerOrRpc?: Rpc | ProviderInterface) {
super();
this._api = providerOrRpc instanceof Rpc
? providerOrRpc
: new Rpc(providerOrRpc);
this._cacheMap = {};
this._eventemitter = new EventEmitter();
this._isConnected = new BehaviorSubject(this._api._provider.isConnected());
this.initEmitters(this._api._provider);
@@ -67,6 +67,14 @@ export default class RpcRx extends E3.EventEmitter implements RpcRxInterface {
return this._isConnected;
}
on (type: RpcRxInterface$Events, handler: (...args: Array<any>) => any): void {
this._eventemitter.on(type, handler);
}
protected emit (type: RpcRxInterface$Events, ...args: Array<any>): void {
this._eventemitter.emit(type, ...args);
}
private initEmitters (provider: ProviderInterface): void {
provider.on('connected', () => {
this._isConnected.next(true);