Skip to content

Commit 64ff226

Browse files
kwingram25jacogr
andcommitted
Fix api.derive.collective (#1711)
* fix api.derive.collective * imports * Update proposals.ts Co-authored-by: Jaco Greeff <[email protected]>
1 parent e762746 commit 64ff226

File tree

4 files changed

+34
-39
lines changed

4 files changed

+34
-39
lines changed

packages/api-derive/src/collective/proposals.ts

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,39 @@ import { ApiInterfaceRx } from '@polkadot/api/types';
66
import { Hash, Proposal, Votes } from '@polkadot/types/interfaces';
77
import { DerivedCollectiveProposals } from '../types';
88

9-
import { Observable, combineLatest, of } from 'rxjs';
9+
import { combineLatest, Observable, of } from 'rxjs';
1010
import { map, switchMap } from 'rxjs/operators';
1111
import { Option } from '@polkadot/types';
1212

13-
import { memo } from '../util';
14-
15-
function retrieveProposals (api: ApiInterfaceRx, section: 'council' | 'technicalCommittee', hashes: Hash[]): Observable<DerivedCollectiveProposals> {
16-
return combineLatest([
17-
// We are doing single subscriptions on all these, multi may yield old results
18-
combineLatest(
19-
hashes.map((hash): Observable<Option<Proposal>> => api.query[section].proposalOf(hash))
20-
),
21-
api.query[section].voting.multi<Option<Votes>>(hashes)
22-
]).pipe(
23-
map(([proposals, votes]: [Option<Proposal>[], Option<Votes>[]]): DerivedCollectiveProposals => {
24-
const result: DerivedCollectiveProposals = [];
25-
26-
proposals.forEach((proposalOpt, index): void => {
27-
if (proposalOpt.isSome) {
28-
result.push({
29-
hash: hashes[index],
30-
proposal: proposalOpt.unwrap(),
31-
votes: votes[index].unwrapOr(null)
32-
});
33-
}
34-
});
35-
36-
return result;
37-
})
38-
);
39-
}
40-
4113
export function proposals (api: ApiInterfaceRx, section: 'council' | 'technicalCommittee'): () => Observable<DerivedCollectiveProposals> {
42-
return memo((): Observable<DerivedCollectiveProposals> =>
14+
return (): Observable<DerivedCollectiveProposals> =>
4315
api.query[section]
44-
? api.query[section].proposals().pipe(
45-
switchMap((proposals: Hash[]): Observable<DerivedCollectiveProposals> =>
46-
retrieveProposals(api, section, proposals)
16+
? api.query[section].proposals()
17+
.pipe(
18+
switchMap((hashes: Hash[]): Observable<[Hash[], Option<Proposal>[], Option<Votes>[]]> => {
19+
return combineLatest([
20+
of(hashes),
21+
combineLatest(
22+
hashes.map((hash): Observable<Option<Proposal>> => api.query[section].proposalOf(hash))
23+
),
24+
api.query[section].voting.multi<Option<Votes>>(hashes)
25+
]);
26+
}),
27+
map(([hashes, proposals, votes]): DerivedCollectiveProposals => {
28+
const result: DerivedCollectiveProposals = [];
29+
30+
proposals.forEach((proposalOpt, index): void => {
31+
if (proposalOpt.isSome) {
32+
result.push({
33+
hash: hashes[index],
34+
proposal: proposalOpt.unwrap(),
35+
votes: votes[index].unwrapOr(null)
36+
});
37+
}
38+
});
39+
40+
return result;
41+
})
4742
)
48-
)
49-
: of([] as DerivedCollectiveProposals)
50-
);
43+
: of([] as DerivedCollectiveProposals);
5144
}

packages/api-derive/src/council/proposals.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { DerivedCollectiveProposals } from '../types';
77

88
import { Observable } from 'rxjs';
99
import { proposals as collectiveProposals } from '../collective';
10+
import { memo } from '../util';
1011

1112
export function proposals (api: ApiInterfaceRx): () => Observable<DerivedCollectiveProposals> {
12-
return collectiveProposals(api, 'council');
13+
return memo(collectiveProposals(api, 'council'));
1314
}

packages/api-derive/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function injectFunctions<AllSections> (api: ApiInterfaceRx, allSections: AllSect
5959
}, {} as DeriveAllSections<AllSections>);
6060
}
6161

62-
export const derive = { accounts, balances, chain, contracts, council, democracy, elections, imOnline, session, technicalCommittee, staking, treasury };
62+
export const derive = { accounts, balances, chain, contracts, council, democracy, elections, imOnline, session, staking, technicalCommittee, treasury };
6363
export type ExactDerive = DeriveAllSections<typeof derive>;
6464

6565
// FIXME The return type of this function should be {...ExactDerive, ...DeriveCustom}

packages/api-derive/src/technicalCommittee/proposals.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { DerivedCollectiveProposals } from '../types';
77

88
import { Observable } from 'rxjs';
99
import { proposals as collectiveProposals } from '../collective';
10+
import { memo } from '../util';
1011

1112
export function proposals (api: ApiInterfaceRx): () => Observable<DerivedCollectiveProposals> {
12-
return collectiveProposals(api, 'technicalCommittee');
13+
return memo(collectiveProposals(api, 'technicalCommittee'));
1314
}

0 commit comments

Comments
 (0)