Use rpc-rx internally to api-promise (#539)

* Use rpc-rx internally to api-promise

* Don't swallow errors

* Remove unused imports
This commit is contained in:
Jaco Greeff
2019-01-07 15:44:15 +01:00
committed by GitHub
parent f2748347c5
commit 70ff3c5dfc
5 changed files with 26 additions and 36 deletions
+2
View File
@@ -185,6 +185,7 @@ export default abstract class ApiBase<R, S, E> implements ApiBaseInterface<R, S,
*/
on (type: ApiInterface$Events, handler: (...args: Array<any>) => any): this {
this._eventemitter.on(type, handler);
return this;
}
@@ -209,6 +210,7 @@ export default abstract class ApiBase<R, S, E> implements ApiBaseInterface<R, S,
*/
once (type: ApiInterface$Events, handler: (...args: Array<any>) => any): this {
this._eventemitter.once(type, handler);
return this;
}
+15 -27
View File
@@ -2,12 +2,13 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { RpcInterface$Section$Method } from '@polkadot/rpc-core/types';
import { RpcRxInterface$Method } from '@polkadot/rpc-rx/types';
import { ProviderInterface } from '@polkadot/rpc-provider/types';
import { ApiOptions } from '../types';
import { ApiPromiseInterface, DecoratedRpc, DecoratedRpc$Method, DecoratedRpc$Section, QueryableStorageFunction, QueryableModuleStorage, QueryableStorage, SubmittableExtrinsics, SubmittableModuleExtrinsics, SubmittableExtrinsicFunction, UnsubFunction } from './types';
import Rpc from '@polkadot/rpc-core/index';
import RpcRx from '@polkadot/rpc-rx/index';
import { Storage } from '@polkadot/storage/types';
import { Hash } from '@polkadot/types/index';
import { Codec } from '@polkadot/types/types';
@@ -19,10 +20,6 @@ import ApiBase from '../Base';
import Combinator, { CombinatorCallback, CombinatorFunction } from './Combinator';
import SubmittableExtrinsic from './SubmittableExtrinsic';
const NOOP = () => {
// ignore
};
const l = logger('api/promise');
/**
@@ -180,10 +177,14 @@ export default class ApiPromise extends ApiBase<DecoratedRpc, QueryableStorage,
protected decorateRpc (rpc: Rpc): DecoratedRpc {
const names = ['author', 'chain', 'state', 'system'] as Array<keyof DecoratedRpc>;
const rpcrx = new RpcRx(rpc);
return names.reduce((result, section) => {
result[section] = Object.keys(rpc[section]).reduce((fns, method) => {
fns[method] = this.decorateMethod(rpc[section][method]);
result[section] = Object.keys(rpcrx[section]).reduce((fns, method) => {
fns[method] = this.decorateMethod(
rpcrx[section][method],
isFunction(rpc[section][method].unsubscribe)
);
return fns;
}, {} as DecoratedRpc$Section);
@@ -192,9 +193,10 @@ export default class ApiPromise extends ApiBase<DecoratedRpc, QueryableStorage,
}, {} as DecoratedRpc);
}
protected decorateMethod (fn: RpcInterface$Section$Method): DecoratedRpc$Method {
if (!isFunction(fn.unsubscribe)) {
return fn;
protected decorateMethod (rxfn: RpcRxInterface$Method, isSubscription: boolean): DecoratedRpc$Method {
if (!isSubscription) {
return (...params: Array<any>): Promise<any> =>
rxfn(...params).toPromise();
}
return (..._params: Array<any>): UnsubFunction => {
@@ -202,25 +204,11 @@ export default class ApiPromise extends ApiBase<DecoratedRpc, QueryableStorage,
assert(isFunction(cb), 'Expected callback as last paramater for subscription');
let isActive = true;
const params = _params
.slice(0, _params.length - 1)
.concat([(value?: any) => {
return !isActive
? undefined
: cb(value);
}]);
const subscriptionIdPromise = fn(...params);
const params = _params.slice(0, _params.length - 1);
const subscription = rxfn(...params).subscribe(cb);
return (): void => {
if (!isActive) {
return;
}
isActive = false;
subscriptionIdPromise
.then((subscriptionId) => fn.unsubscribe(subscriptionId))
.catch(NOOP);
subscription.unsubscribe();
};
};
}
+5 -5
View File
@@ -4,7 +4,7 @@
import { ProviderInterface, ProviderInterface$Callback } from '@polkadot/rpc-provider/types';
import { RpcSection, RpcMethod } from '@polkadot/jsonrpc/types';
import { RpcInterface, RpcInterface$Section, RpcInterface$Section$Method } from './types';
import { RpcInterface, RpcInterface$Method, RpcInterface$Section } from './types';
import interfaces from '@polkadot/jsonrpc/index';
import WsProvider from '@polkadot/rpc-provider/ws';
@@ -99,7 +99,7 @@ export default class Rpc implements RpcInterface {
}, {} as RpcInterface$Section);
}
private createMethodSend (method: RpcMethod): RpcInterface$Section$Method {
private createMethodSend (method: RpcMethod): RpcInterface$Method {
const rpcName = `${method.section}_${method.method}`;
const call = async (...values: Array<any>): Promise<any> => {
@@ -119,10 +119,10 @@ export default class Rpc implements RpcInterface {
}
};
return call as RpcInterface$Section$Method;
return call as RpcInterface$Method;
}
private createMethodSubscribe (method: RpcMethod): RpcInterface$Section$Method {
private createMethodSubscribe (method: RpcMethod): RpcInterface$Method {
const [updateType, subMethod, unsubMethod] = method.pubsub;
const subName = `${method.section}_${subMethod}`;
const unsubName = `${method.section}_${unsubMethod}`;
@@ -157,7 +157,7 @@ export default class Rpc implements RpcInterface {
}
};
const call = _call as RpcInterface$Section$Method;
const call = _call as RpcInterface$Method;
call.unsubscribe = unsubscribe;
+2 -2
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
export interface RpcInterface$Section$Method {
export interface RpcInterface$Method {
(...params: Array<any>): Promise<any>;
subscription: string;
@@ -10,7 +10,7 @@ export interface RpcInterface$Section$Method {
}
export type RpcInterface$Section = {
[index: string]: RpcInterface$Section$Method
[index: string]: RpcInterface$Method
};
export interface RpcInterface {
+2 -2
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { RpcInterface$Section, RpcInterface$Section$Method } from '@polkadot/rpc-core/types';
import { RpcInterface$Method, RpcInterface$Section } from '@polkadot/rpc-core/types';
import { ProviderInterface } from '@polkadot/rpc-provider/types';
import { RpcRxInterface, RpcRxInterface$Events, RpcRxInterface$Section } from './types';
@@ -174,7 +174,7 @@ export default class RpcRx implements RpcRxInterface {
};
}
private createReplayUnsub (fn: RpcInterface$Section$Method, subscribe: Promise<number>, subName: string, paramStr: string): () => void {
private createReplayUnsub (fn: RpcInterface$Method, subscribe: Promise<number>, subName: string, paramStr: string): () => void {
return (): void => {
subscribe
.then((subscriptionId: number) =>