@@ -6,46 +6,39 @@ import { ApiInterfaceRx } from '@polkadot/api/types';
66import { Hash , Proposal , Votes } from '@polkadot/types/interfaces' ;
77import { DerivedCollectiveProposals } from '../types' ;
88
9- import { Observable , combineLatest , of } from 'rxjs' ;
9+ import { combineLatest , Observable , of } from 'rxjs' ;
1010import { map , switchMap } from 'rxjs/operators' ;
1111import { 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-
4113export 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}
0 commit comments