Update promise combineLatest with generics (#2214)

This commit is contained in:
Jaco Greeff
2020-04-27 15:53:17 +02:00
committed by GitHub
parent c93dad0bc5
commit 0d1d42f6c2
3 changed files with 29 additions and 13 deletions
+22 -7
View File
@@ -8,7 +8,7 @@ import { DeriveSessionInfo, DeriveStakingAccount, DeriveStakingQuery, DeriveUnlo
import BN from 'bn.js';
import { Observable, combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';
import { map, switchMap } from 'rxjs/operators';
import { isUndefined } from '@polkadot/util';
@@ -70,16 +70,31 @@ function parseResult (api: ApiInterfaceRx, sessionInfo: DeriveSessionInfo, query
};
}
export function _account (api: ApiInterfaceRx): (sessionInfo: DeriveSessionInfo, accountId: Uint8Array | string) => Observable<DeriveStakingAccount> {
return memo((sessionInfo: DeriveSessionInfo, accountId: Uint8Array | string): Observable<DeriveStakingAccount> =>
api.derive.staking.query(accountId).pipe(
map((query) => parseResult(api, sessionInfo, query))
));
}
/**
* @description From a stash, retrieve the controllerId and fill in all the relevant staking details
*/
export function account (api: ApiInterfaceRx): (accountId: Uint8Array | string) => Observable<DeriveStakingAccount> {
return memo((accountId: Uint8Array | string): Observable<DeriveStakingAccount> =>
combineLatest([
api.derive.session.info(),
api.derive.staking.query(accountId)
]).pipe(
map(([sessionInfo, query]: [DeriveSessionInfo, DeriveStakingQuery]) =>
parseResult(api, sessionInfo, query))
api.derive.session.info().pipe(
switchMap((sessionInfo) => api.derive.staking._account(sessionInfo, accountId))
));
}
/**
* @description From a list of stashes, fill in all the relevant staking details
*/
export function accounts (api: ApiInterfaceRx): (accountIds: (Uint8Array | string)[]) => Observable<DeriveStakingAccount[]> {
return memo((accountIds: (Uint8Array | string)[]): Observable<DeriveStakingAccount[]> =>
api.derive.session.info().pipe(
switchMap((sessionInfo) =>
combineLatest(accountIds.map((accountId) => api.derive.staking._account(sessionInfo, accountId)))
)
));
}
+1 -1
View File
@@ -264,7 +264,7 @@ export default class ApiPromise extends ApiBase<'promise'> {
* ```
*/
// eslint-disable-next-line @typescript-eslint/require-await
public async combineLatest (fns: (CombinatorFunction | [CombinatorFunction, ...any[]])[], callback: CombinatorCallback): UnsubscribePromise {
public async combineLatest <T extends any[] = any[]> (fns: (CombinatorFunction | [CombinatorFunction, ...any[]])[], callback: CombinatorCallback<T>): UnsubscribePromise {
const combinator = new Combinator(fns, callback);
return (): void => {
+6 -5
View File
@@ -7,15 +7,16 @@ import { UnsubscribePromise } from '../types';
import { isFunction } from '@polkadot/util';
export type CombinatorCallback = Callback<any[]>;
export type CombinatorCallback <T extends any[]> = Callback<T>;
export interface CombinatorFunction {
(cb: Callback<any>): UnsubscribePromise;
}
export default class Combinator {
export default class Combinator<T extends any[] = any[]> {
#allHasFired = false;
#callback: CombinatorCallback;
#callback: CombinatorCallback<T>;
#fired: boolean[] = [];
@@ -27,7 +28,7 @@ export default class Combinator {
#subscriptions: UnsubscribePromise[] = [];
constructor (fns: (CombinatorFunction | [CombinatorFunction, ...any[]])[], callback: CombinatorCallback) {
constructor (fns: (CombinatorFunction | [CombinatorFunction, ...any[]])[], callback: CombinatorCallback<T>) {
this.#callback = callback;
// eslint-disable-next-line @typescript-eslint/require-await
@@ -67,7 +68,7 @@ export default class Combinator {
}
try {
this.#callback(this.#results);
this.#callback(this.#results as T);
} catch (error) {
// swallow, we don't want the handler to trip us up
}