diff --git a/packages/api/src/promise/index.ts b/packages/api/src/promise/index.ts index b13963671e..019dfdcd37 100644 --- a/packages/api/src/promise/index.ts +++ b/packages/api/src/promise/index.ts @@ -14,10 +14,7 @@ import { isFunction } from '@polkadot/util'; import ApiBase from '../Base'; import Combinator, { CombinatorCallback, CombinatorFunction } from './Combinator'; -interface RxFn { - (...params: Array): Observable; - isSubscription: boolean; -} +type RxFn = (...params: Array) => Observable; /** * # @polkadot/api/promise @@ -196,8 +193,8 @@ export default class ApiPromise extends ApiBase implements ApiPromiseInt }; } - protected onCall (method: RxFn, params: Array, isSubscription: boolean = false): OnCall { - if (!params || params.length === 0 || !isSubscription) { + protected onCall (method: RxFn, params: Array, isSubscription?: boolean): OnCall { + if (!params || params.length === 0 || isSubscription === false) { return method(...params).pipe(first()).toPromise(); } diff --git a/packages/api/test/e2e/promise-queries.spec.js b/packages/api/test/e2e/promise-queries.spec.js index 2f70fd3b33..fa5f700fc6 100644 --- a/packages/api/test/e2e/promise-queries.spec.js +++ b/packages/api/test/e2e/promise-queries.spec.js @@ -1,7 +1,6 @@ // Copyright 2017-2019 @polkadot/api authors & contributors // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -// @ts-check import BN from 'bn.js'; import testingPairs from '@polkadot/keyring/testingPairs'; diff --git a/packages/api/test/e2e/promise-tx.spec.js b/packages/api/test/e2e/promise-tx.spec.js index 38aefaf8ab..6df72275b8 100644 --- a/packages/api/test/e2e/promise-tx.spec.js +++ b/packages/api/test/e2e/promise-tx.spec.js @@ -1,7 +1,6 @@ // Copyright 2017-2019 @polkadot/api authors & contributors // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -// @ts-check import testingPairs from '@polkadot/keyring/testingPairs'; @@ -47,7 +46,7 @@ describe.skip('e2e transactions', () => { }); it('makes a transfer (signAndSend)', async (done) => { - const unsubscribe = await api.tx.balances + api.tx.balances .transfer('12ghjsRJpeJpUQaCQeHcBv9pRQA3tdcMxeL8cVk9JHWJGHjd', 12345) .signAndSend(keyring.dave, ({ events, status, type }) => { console.log('Transaction status:', type); @@ -60,8 +59,9 @@ describe.skip('e2e transactions', () => { console.log('\t', phase.toString(), `: ${section}.${method}`, data.toString()); }); - unsubscribe(); - done(); + if (events.length) { + done(); + } } }); }); diff --git a/packages/api/test/e2e/rx-queries.spec.js b/packages/api/test/e2e/rx-queries.spec.js index b607b77da9..13979a7ec8 100644 --- a/packages/api/test/e2e/rx-queries.spec.js +++ b/packages/api/test/e2e/rx-queries.spec.js @@ -1,7 +1,6 @@ // Copyright 2017-2019 @polkadot/api authors & contributors // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -// @ts-check import { switchMap } from 'rxjs/operators'; import testingPairs from '@polkadot/keyring/testingPairs'; diff --git a/packages/api/test/e2e/rx-tx.spec.js b/packages/api/test/e2e/rx-tx.spec.js index 14d28c5fa9..def6f0894b 100644 --- a/packages/api/test/e2e/rx-tx.spec.js +++ b/packages/api/test/e2e/rx-tx.spec.js @@ -1,7 +1,6 @@ // Copyright 2017-2019 @polkadot/api authors & contributors // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -// @ts-check import { first, switchMap } from 'rxjs/operators'; import testingPairs from '@polkadot/keyring/testingPairs'; diff --git a/packages/rpc-rx/src/index.ts b/packages/rpc-rx/src/index.ts index e60d8a25cf..d708db526b 100644 --- a/packages/rpc-rx/src/index.ts +++ b/packages/rpc-rx/src/index.ts @@ -4,7 +4,7 @@ import { RpcInterface$Method, RpcInterface$Section } from '@polkadot/rpc-core/types'; import { ProviderInterface } from '@polkadot/rpc-provider/types'; -import { RpcRxInterface, RpcRxInterface$Events, RpcRxInterface$Section, RpcRxObervable } from './types'; +import { RpcRxInterface, RpcRxInterface$Events, RpcRxInterface$Section } from './types'; import EventEmitter from 'eventemitter3'; import { BehaviorSubject, Observable, Subscriber, from } from 'rxjs'; @@ -100,27 +100,21 @@ export default class RpcRx implements RpcRxInterface { }, ({} as RpcRxInterface$Section)); } - private createObservable (subName: string, name: string, section: RpcInterface$Section): RpcRxObervable { - const isSubscription = isFunction(section[name].unsubscribe); - const fn = isSubscription - ? this.createCachedObservable(subName, name, section) - : (...params: Array): Observable => - from( - section[name] - .apply(null, params) - .catch((error: Error) => { - console.error(error); - }) - ); + private createObservable (subName: string, name: string, section: RpcInterface$Section): (...params: Array) => Observable { + if (isFunction(section[name].unsubscribe)) { + return this.createCachedObservable(subName, name, section); + } - const result = fn as RpcRxObervable; - - result.isSubscription = isSubscription; - - return result; + return (...params: Array): Observable => + from( + section[name] + .apply(null, params) + .catch((error: Error) => { + console.error(error); + }) + ); } - // FIXME Memoization as in derive, https://github.com/polkadot-js/api/issues/553 private createCachedObservable (subName: string, name: string, section: RpcInterface$Section): (...params: Array) => Observable { if (!this._cacheMap[subName]) { this._cacheMap[subName] = {}; diff --git a/packages/rpc-rx/src/types.ts b/packages/rpc-rx/src/types.ts index 4b02c793a7..60328d4bdf 100644 --- a/packages/rpc-rx/src/types.ts +++ b/packages/rpc-rx/src/types.ts @@ -5,13 +5,10 @@ import { ReplaySubject, Observable } from 'rxjs'; import { ProviderInterface$Emitted } from '@polkadot/rpc-provider/types'; -export interface RpcRxObervable { - (...params: Array): Observable | ReplaySubject; - isSubscription: boolean; -} +export type RpcRxInterface$Method = (...params: Array) => Observable | ReplaySubject; export type RpcRxInterface$Section = { - [index: string]: RpcRxObervable + [index: string]: RpcRxInterface$Method }; export type RpcRxInterface$Events = ProviderInterface$Emitted;