diff --git a/packages/api/src/Base.ts b/packages/api/src/Base.ts index 05ea1dd0a1..9d8bf66076 100644 --- a/packages/api/src/Base.ts +++ b/packages/api/src/Base.ts @@ -185,6 +185,7 @@ export default abstract class ApiBase implements ApiBaseInterface) => any): this { this._eventemitter.on(type, handler); + return this; } @@ -209,6 +210,7 @@ export default abstract class ApiBase implements ApiBaseInterface) => any): this { this._eventemitter.once(type, handler); + return this; } diff --git a/packages/api/src/promise/index.ts b/packages/api/src/promise/index.ts index 85c1c94f42..8028cd4c98 100644 --- a/packages/api/src/promise/index.ts +++ b/packages/api/src/promise/index.ts @@ -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; + 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): Promise => + rxfn(...params).toPromise(); } return (..._params: Array): UnsubFunction => { @@ -202,25 +204,11 @@ export default class ApiPromise extends ApiBase { - 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(); }; }; } diff --git a/packages/rpc-core/src/index.ts b/packages/rpc-core/src/index.ts index b1a346de5e..77decaabaf 100644 --- a/packages/rpc-core/src/index.ts +++ b/packages/rpc-core/src/index.ts @@ -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): Promise => { @@ -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; diff --git a/packages/rpc-core/src/types.ts b/packages/rpc-core/src/types.ts index 57ac54fd1a..afdf14ffee 100644 --- a/packages/rpc-core/src/types.ts +++ b/packages/rpc-core/src/types.ts @@ -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): Promise; 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 { diff --git a/packages/rpc-rx/src/index.ts b/packages/rpc-rx/src/index.ts index 31d8031981..d708db526b 100644 --- a/packages/rpc-rx/src/index.ts +++ b/packages/rpc-rx/src/index.ts @@ -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, subName: string, paramStr: string): () => void { + private createReplayUnsub (fn: RpcInterface$Method, subscribe: Promise, subName: string, paramStr: string): () => void { return (): void => { subscribe .then((subscriptionId: number) =>