@@ -12,7 +12,7 @@ import { catchError, combineLatest, map, of, switchMap } from 'rxjs';
1212
1313import { isFunction } from '@polkadot/util' ;
1414
15- import { memo } from '../util' ;
15+ import { firstObservable } from '../util' ;
1616import { callMethod , withSection } from './helpers' ;
1717
1818// We are re-exporting these from here to ensure that *.d.ts generation is correct
@@ -36,61 +36,48 @@ function parse (api: ApiInterfaceRx, [hashes, proposals, votes]: Result): Derive
3636 . filter ( ( proposal ) : proposal is DeriveCollectiveProposal => ! ! proposal ) ;
3737}
3838
39- function _proposalsFrom ( section : string , instanceId : string , api : ApiInterfaceRx ) : ( hashes : ( Hash | Uint8Array | string ) [ ] ) => Observable < DeriveCollectiveProposal [ ] > {
40- return memo ( instanceId , ( hashes : ( Hash | Uint8Array | string ) [ ] ) : Observable < DeriveCollectiveProposal [ ] > =>
41- ( isFunction ( api . query [ section ] ?. proposals ) && hashes . length
42- ? combineLatest ( [
43- of ( hashes ) ,
44- // this should simply be api.query[section].proposalOf.multi<Option<Proposal>>(hashes),
45- // however we have had cases on Edgeware where the indices have moved around after an
46- // upgrade, which results in invalid on-chain data
47- combineLatest ( hashes . map ( ( h ) =>
48- api . query [ section ] . proposalOf < Option < Proposal > > ( h ) . pipe (
49- catchError ( ( ) => of ( null ) )
50- )
51- ) ) ,
52- api . query [ section ] . voting . multi < Option < Votes > > ( hashes )
53- ] )
54- : of < Result > ( [ [ ] , [ ] , [ ] ] )
55- ) . pipe (
56- map ( ( r ) => parse ( api , r ) )
57- )
58- ) ;
59- }
60-
61- function withProposals < T > ( _section : Collective , fn : ( section : string , instanceId : string , api : ApiInterfaceRx , proposalsFrom : ( hashes : ( string | Hash | Uint8Array ) [ ] ) => Observable < DeriveCollectiveProposal [ ] > ) => T ) : ( instanceId : string , api : ApiInterfaceRx ) => T {
62- return withSection ( _section , ( section , instanceId , api ) =>
63- fn ( section , instanceId , api , _proposalsFrom ( section , instanceId , api ) )
39+ function _proposalsFrom ( section : string , api : ApiInterfaceRx , hashes : ( Hash | Uint8Array | string ) [ ] ) : Observable < DeriveCollectiveProposal [ ] > {
40+ return ( isFunction ( api . query [ section ] ?. proposals ) && hashes . length
41+ ? combineLatest ( [
42+ of ( hashes ) ,
43+ // this should simply be api.query[section].proposalOf.multi<Option<Proposal>>(hashes),
44+ // however we have had cases on Edgeware where the indices have moved around after an
45+ // upgrade, which results in invalid on-chain data
46+ combineLatest ( hashes . map ( ( h ) =>
47+ api . query [ section ] . proposalOf < Option < Proposal > > ( h ) . pipe (
48+ catchError ( ( ) => of ( null ) )
49+ )
50+ ) ) ,
51+ api . query [ section ] . voting . multi < Option < Votes > > ( hashes )
52+ ] )
53+ : of < Result > ( [ [ ] , [ ] , [ ] ] )
54+ ) . pipe (
55+ map ( ( r ) => parse ( api , r ) )
6456 ) ;
6557}
6658
6759export function hasProposals ( _section : Collective ) : ( instanceId : string , api : ApiInterfaceRx ) => ( ) => Observable < boolean > {
68- return withSection ( _section , ( section , instanceId , api ) =>
69- memo ( instanceId , ( ) : Observable < boolean > =>
60+ return withSection ( _section , ( section , api ) =>
61+ ( ) : Observable < boolean > =>
7062 of ( isFunction ( api . query [ section ] ?. proposals ) )
71- )
7263 ) ;
7364}
7465
7566export function proposals ( _section : Collective ) : ( instanceId : string , api : ApiInterfaceRx ) => ( ) => Observable < DeriveCollectiveProposal [ ] > {
76- return withProposals ( _section , ( section , instanceId , api , proposalsFrom ) =>
77- memo ( instanceId , ( ) : Observable < DeriveCollectiveProposal [ ] > =>
67+ return withSection ( _section , ( section , api ) =>
68+ ( ) : Observable < DeriveCollectiveProposal [ ] > =>
7869 api . derive [ section as 'council' ] . proposalHashes ( ) . pipe (
79- switchMap ( proposalsFrom )
70+ switchMap ( ( all ) => _proposalsFrom ( section , api , all ) )
8071 )
81- )
8272 ) ;
8373}
8474
8575export function proposal ( _section : Collective ) : ( instanceId : string , api : ApiInterfaceRx ) => ( hash : Hash | Uint8Array | string ) => Observable < DeriveCollectiveProposal | null > {
86- return withProposals ( _section , ( section , instanceId , api , proposalsFrom ) =>
87- memo ( instanceId , ( hash : Hash | Uint8Array | string ) : Observable < DeriveCollectiveProposal | null > =>
76+ return withSection ( _section , ( section , api ) =>
77+ ( hash : Hash | Uint8Array | string ) : Observable < DeriveCollectiveProposal | null > =>
8878 isFunction ( api . query [ section ] ?. proposals )
89- ? proposalsFrom ( [ hash ] ) . pipe (
90- map ( ( [ proposal ] ) => proposal )
91- )
79+ ? firstObservable ( _proposalsFrom ( section , api , [ hash ] ) )
9280 : of ( null )
93- )
9481 ) ;
9582}
9683
0 commit comments