Skip to content

Commit

Permalink
Merge 9680880 into 4be4d80
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr committed Oct 11, 2018
2 parents 4be4d80 + 9680880 commit fdd4b44
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
18 changes: 13 additions & 5 deletions packages/api/src/Base.ts
Expand Up @@ -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';
Expand All @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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;

Expand Down
17 changes: 10 additions & 7 deletions packages/rpc-provider/src/ws/index.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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]);

Expand Down
18 changes: 13 additions & 5 deletions packages/rpc-rx/src/index.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit fdd4b44

Please sign in to comment.