ApiPromise combinator return PromiseSubscription (#576)

* ApiPromise combinator return PromiseSubscription

* PromiseSubscription can return anything
This commit is contained in:
Jaco Greeff
2019-01-11 23:22:27 +01:00
committed by GitHub
parent 64ec9e61f3
commit 13e2aa0b44
5 changed files with 50 additions and 38 deletions
+17 -7
View File
@@ -2,13 +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 { UnsubFunction } from './types';
import { PromiseSubscription } from './types';
import { isFunction } from '@polkadot/util';
export type CombinatorCallback = (value: Array<any>) => any;
export type CombinatorFunction = {
(cb: (value: any) => any): UnsubFunction | any
(cb: (value: any) => any): PromiseSubscription
};
export default class Combinator {
@@ -18,11 +18,11 @@ export default class Combinator {
protected _fns: Array<CombinatorFunction> = [];
protected _isActive: boolean = true;
protected _results: Array<any> = [];
protected _unsubscriptions: Array<UnsubFunction | any> = [];
protected _subscriptions: Array<PromiseSubscription> = [];
constructor (fns: Array<CombinatorFunction | [CombinatorFunction, ...Array<any>]>, callback: CombinatorCallback) {
this._callback = callback;
this._unsubscriptions = fns.map((input, index) => {
this._subscriptions = fns.map(async (input, index) => {
const [fn, ...args] = Array.isArray(input)
? input
: [input];
@@ -65,11 +65,21 @@ export default class Combinator {
}
unsubscribe (): void {
if (!this._isActive) {
return;
}
this._isActive = false;
this._unsubscriptions.forEach((unsubscribe) => {
if (isFunction(unsubscribe)) {
unsubscribe();
this._subscriptions.forEach(async (subscription) => {
try {
const unsubscribe = await subscription;
if (isFunction(unsubscribe)) {
unsubscribe();
}
} catch (error) {
// ignore
}
});
}
+18 -16
View File
@@ -2,16 +2,26 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { PromiseSubscription } from './types';
import Combinator from './Combinator';
describe('Combinator', () => {
let fns: Array<(value: any) => void> = [];
const storeFn = async (cb: (value: any) => void): PromiseSubscription => {
fns.push(cb);
return () => undefined;
};
beforeEach(() => {
fns = [];
});
it('it triggers on all values', (done) => {
let count = 0;
const fns: Array<(value: any) => void> = [];
const combinator = new Combinator(
[
(cb) => Promise.resolve(fns.push(cb))
],
[storeFn],
(value: Array<any>) => {
expect(value[0]).toEqual(`test${count}`);
@@ -31,12 +41,8 @@ describe('Combinator', () => {
});
it('combines values from 2 sources, firing when it has all results', (done) => {
const fns: Array<(value: any) => void> = [];
const combinator = new Combinator(
[
(cb) => Promise.resolve(fns.push(cb)),
(cb) => Promise.resolve(fns.push(cb))
],
[storeFn, storeFn],
(value: Array<any>) => {
expect(value).toEqual(['test0', 'test1']);
@@ -52,12 +58,8 @@ describe('Combinator', () => {
it('combines values from 2 sources, allowing multiple updates', (done) => {
let count = 0;
const fns: Array<(value: any) => void> = [];
const combinator = new Combinator(
[
(cb) => Promise.resolve(fns.push(cb)),
(cb) => Promise.resolve(fns.push(cb))
],
[storeFn, storeFn],
(value: Array<any>) => {
expect(value).toEqual(
count === 0
@@ -80,10 +82,10 @@ describe('Combinator', () => {
});
it('unsubscribes as required', (done) => {
const mocker = () => done;
const mocker = async () => done;
const combinator = new Combinator([
mocker,
() => Promise.resolve(98765)
async () => () => void 0
], (value: Array<any>) => {
// ignore
});
@@ -5,7 +5,7 @@
import { KeyringPair } from '@polkadot/keyring/types';
import { AnyNumber, AnyU8a } from '@polkadot/types/types';
import { SubmittableSendResult } from '../types';
import { ApiPromiseInterface, UnsubFunction } from './types';
import { ApiPromiseInterface, PromiseSubscription } from './types';
import { EventRecord, Extrinsic, ExtrinsicStatus, Hash, Index, Method, SignedBlock } from '@polkadot/types/index';
import { assert } from '@polkadot/util';
@@ -41,7 +41,7 @@ export default class SubmittableExtrinsic extends Extrinsic {
};
}
send (statusCb?: (result: SubmittableSendResult) => any): Promise<Hash | UnsubFunction> {
send (statusCb?: (result: SubmittableSendResult) => any): Promise<Hash> | PromiseSubscription {
if (!statusCb || !this._api.hasSubscriptions) {
return this._api.rpc.author.submitExtrinsic(this);
}
@@ -55,13 +55,13 @@ export default class SubmittableExtrinsic extends Extrinsic {
return this;
}
async signAndSend (signerPair: KeyringPair, statusCb: (result: SubmittableSendResult) => any): Promise<Hash | UnsubFunction> {
async signAndSend (signerPair: KeyringPair, statusCb: (result: SubmittableSendResult) => any): PromiseSubscription {
assert(this._api.hasSubscriptions, 'Api does not support subscriptions');
const signerNonce = await this._api.query.system.accountNonce(signerPair.address());
return this
.sign(signerPair, signerNonce as Index)
.send(statusCb);
.send(statusCb) as PromiseSubscription;
}
}
+5 -5
View File
@@ -5,7 +5,7 @@
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 { ApiPromiseInterface, DecoratedRpc, DecoratedRpc$Method, DecoratedRpc$Section, QueryableStorageFunction, QueryableModuleStorage, QueryableStorage, SubmittableExtrinsics, SubmittableModuleExtrinsics, SubmittableExtrinsicFunction, PromiseSubscription } from './types';
import { EMPTY } from 'rxjs';
import { tap, catchError } from 'rxjs/operators';
@@ -197,7 +197,7 @@ export default class ApiPromise extends ApiBase<DecoratedRpc, QueryableStorage,
rxfn(...params).toPromise();
}
return (..._params: Array<any>): Promise<UnsubFunction> => {
return (..._params: Array<any>): PromiseSubscription => {
const cb = _params[_params.length - 1];
assert(isFunction(cb), 'Expected callback as last paramater for subscription');
@@ -254,7 +254,7 @@ export default class ApiPromise extends ApiBase<DecoratedRpc, QueryableStorage,
* });
* ```
*/
combineLatest (fns: Array<CombinatorFunction | [CombinatorFunction, ...Array<any>]>, callback: CombinatorCallback): UnsubFunction {
async combineLatest (fns: Array<CombinatorFunction | [CombinatorFunction, ...Array<any>]>, callback: CombinatorCallback): PromiseSubscription {
const combinator = new Combinator(fns, callback);
return (): void => {
@@ -298,7 +298,7 @@ export default class ApiPromise extends ApiBase<DecoratedRpc, QueryableStorage,
}
private decorateStorageEntry (method: StorageFunction): QueryableStorageFunction {
const decorated: any = (...args: Array<any>): Promise<Codec | null | undefined> | Promise<UnsubFunction> => {
const decorated: any = (...args: Array<any>): Promise<Codec | null | undefined> | PromiseSubscription => {
const cb = args[args.length - 1];
if (args.length === 0 || !isFunction(cb)) {
@@ -313,7 +313,7 @@ export default class ApiPromise extends ApiBase<DecoratedRpc, QueryableStorage,
[[method, args.length === 1 ? undefined : args[0]]],
(result: Array<Codec | null | undefined> = []) =>
cb(result[0])
) as Promise<UnsubFunction>;
) as PromiseSubscription;
};
decorated.at = (hash: Hash, arg?: any): Promise<Codec | null | undefined> =>
+6 -6
View File
@@ -10,9 +10,9 @@ import { ApiBaseInterface } from '../types';
import SubmittableExtrinsic from './SubmittableExtrinsic';
export type UnsubFunction = () => void;
export type PromiseSubscription = Promise<() => any>;
export type DecoratedRpc$Method = (...params: Array<any>) => Promise<any> | Promise<UnsubFunction>;
export type DecoratedRpc$Method = (...params: Array<any>) => Promise<any> | PromiseSubscription;
export interface DecoratedRpc$Section {
[index: string]: DecoratedRpc$Method;
@@ -26,13 +26,13 @@ export interface DecoratedRpc {
}
export interface QueryableStorageFunction$Subscribe {
(arg: any, cb: (value?: any | null) => any): Promise<UnsubFunction>;
(cb: (value?: any | null) => any): Promise<UnsubFunction>;
(arg: any, cb: (value?: any | null) => any): PromiseSubscription;
(cb: (value?: any | null) => any): PromiseSubscription;
}
export interface QueryableStorageFunction extends StorageFunction {
(cb: (value?: any | null) => any): Promise<UnsubFunction>;
(arg: any, cb: (value?: any | null) => any): Promise<UnsubFunction>;
(cb: (value?: any | null) => any): PromiseSubscription;
(arg: any, cb: (value?: any | null) => any): PromiseSubscription;
(arg?: any): Promise<Codec | null | undefined>;
at: (hash: Hash, arg?: any) => Promise<Codec | null | undefined>;
}