From 64ff2265357aed3337d5de0d361e856d41af62f6 Mon Sep 17 00:00:00 2001 From: Keith Ingram Date: Mon, 13 Jan 2020 08:17:04 -0600 Subject: [PATCH] Fix api.derive.collective (#1711) * fix api.derive.collective * imports * Update proposals.ts Co-authored-by: Jaco Greeff --- .../api-derive/src/collective/proposals.ts | 65 +++++++++---------- packages/api-derive/src/council/proposals.ts | 3 +- packages/api-derive/src/index.ts | 2 +- .../src/technicalCommittee/proposals.ts | 3 +- 4 files changed, 34 insertions(+), 39 deletions(-) diff --git a/packages/api-derive/src/collective/proposals.ts b/packages/api-derive/src/collective/proposals.ts index 3c87f7a9a3..ba9934afc5 100644 --- a/packages/api-derive/src/collective/proposals.ts +++ b/packages/api-derive/src/collective/proposals.ts @@ -6,46 +6,39 @@ import { ApiInterfaceRx } from '@polkadot/api/types'; import { Hash, Proposal, Votes } from '@polkadot/types/interfaces'; import { DerivedCollectiveProposals } from '../types'; -import { Observable, combineLatest, of } from 'rxjs'; +import { combineLatest, Observable, of } from 'rxjs'; import { map, switchMap } from 'rxjs/operators'; import { Option } from '@polkadot/types'; -import { memo } from '../util'; - -function retrieveProposals (api: ApiInterfaceRx, section: 'council' | 'technicalCommittee', hashes: Hash[]): Observable { - return combineLatest([ - // We are doing single subscriptions on all these, multi may yield old results - combineLatest( - hashes.map((hash): Observable> => api.query[section].proposalOf(hash)) - ), - api.query[section].voting.multi>(hashes) - ]).pipe( - map(([proposals, votes]: [Option[], Option[]]): DerivedCollectiveProposals => { - const result: DerivedCollectiveProposals = []; - - proposals.forEach((proposalOpt, index): void => { - if (proposalOpt.isSome) { - result.push({ - hash: hashes[index], - proposal: proposalOpt.unwrap(), - votes: votes[index].unwrapOr(null) - }); - } - }); - - return result; - }) - ); -} - export function proposals (api: ApiInterfaceRx, section: 'council' | 'technicalCommittee'): () => Observable { - return memo((): Observable => + return (): Observable => api.query[section] - ? api.query[section].proposals().pipe( - switchMap((proposals: Hash[]): Observable => - retrieveProposals(api, section, proposals) + ? api.query[section].proposals() + .pipe( + switchMap((hashes: Hash[]): Observable<[Hash[], Option[], Option[]]> => { + return combineLatest([ + of(hashes), + combineLatest( + hashes.map((hash): Observable> => api.query[section].proposalOf(hash)) + ), + api.query[section].voting.multi>(hashes) + ]); + }), + map(([hashes, proposals, votes]): DerivedCollectiveProposals => { + const result: DerivedCollectiveProposals = []; + + proposals.forEach((proposalOpt, index): void => { + if (proposalOpt.isSome) { + result.push({ + hash: hashes[index], + proposal: proposalOpt.unwrap(), + votes: votes[index].unwrapOr(null) + }); + } + }); + + return result; + }) ) - ) - : of([] as DerivedCollectiveProposals) - ); + : of([] as DerivedCollectiveProposals); } diff --git a/packages/api-derive/src/council/proposals.ts b/packages/api-derive/src/council/proposals.ts index 96b47653c4..faee798a3c 100644 --- a/packages/api-derive/src/council/proposals.ts +++ b/packages/api-derive/src/council/proposals.ts @@ -7,7 +7,8 @@ import { DerivedCollectiveProposals } from '../types'; import { Observable } from 'rxjs'; import { proposals as collectiveProposals } from '../collective'; +import { memo } from '../util'; export function proposals (api: ApiInterfaceRx): () => Observable { - return collectiveProposals(api, 'council'); + return memo(collectiveProposals(api, 'council')); } diff --git a/packages/api-derive/src/index.ts b/packages/api-derive/src/index.ts index 9f788a397f..75ca705ec3 100644 --- a/packages/api-derive/src/index.ts +++ b/packages/api-derive/src/index.ts @@ -59,7 +59,7 @@ function injectFunctions (api: ApiInterfaceRx, allSections: AllSect }, {} as DeriveAllSections); } -export const derive = { accounts, balances, chain, contracts, council, democracy, elections, imOnline, session, technicalCommittee, staking, treasury }; +export const derive = { accounts, balances, chain, contracts, council, democracy, elections, imOnline, session, staking, technicalCommittee, treasury }; export type ExactDerive = DeriveAllSections; // FIXME The return type of this function should be {...ExactDerive, ...DeriveCustom} diff --git a/packages/api-derive/src/technicalCommittee/proposals.ts b/packages/api-derive/src/technicalCommittee/proposals.ts index 8923a1113d..7eab5b1309 100644 --- a/packages/api-derive/src/technicalCommittee/proposals.ts +++ b/packages/api-derive/src/technicalCommittee/proposals.ts @@ -7,7 +7,8 @@ import { DerivedCollectiveProposals } from '../types'; import { Observable } from 'rxjs'; import { proposals as collectiveProposals } from '../collective'; +import { memo } from '../util'; export function proposals (api: ApiInterfaceRx): () => Observable { - return collectiveProposals(api, 'technicalCommittee'); + return memo(collectiveProposals(api, 'technicalCommittee')); }