Skip to content

Commit 6276e45

Browse files
authored
More cleanups to collective derives (#4320)
1 parent c7cb1d8 commit 6276e45

File tree

3 files changed

+32
-48
lines changed

3 files changed

+32
-48
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,17 @@ export function getInstance (api: ApiInterfaceRx, section: string): string {
2222
: section;
2323
}
2424

25-
export function withSection <T> (_section: Collective, fn: (section: string, instanceId: string, api: ApiInterfaceRx) => T): (instanceId: string, api: ApiInterfaceRx) => T {
25+
export function withSection <T, F extends (...args: any[]) => Observable<T>> (_section: Collective, fn: (section: string, api: ApiInterfaceRx, instanceId: string) => F): (instanceId: string, api: ApiInterfaceRx) => F {
2626
return (instanceId: string, api: ApiInterfaceRx) =>
27-
fn(getInstance(api, _section), instanceId, api);
27+
memo(instanceId, fn(getInstance(api, _section), api, instanceId)) as unknown as F;
2828
}
2929

3030
export function callMethod <T> (method: 'members' | 'proposals' | 'proposalCount', empty: T): (_section: Collective) => (instanceId: string, api: ApiInterfaceRx) => () => Observable<T> {
3131
return (_section: Collective) =>
32-
withSection(_section, (section, instanceId, api) =>
33-
memo(instanceId, (): Observable<T> =>
32+
withSection(_section, (section, api) =>
33+
(): Observable<T> =>
3434
isFunction(api.query[section]?.[method])
3535
? api.query[section as 'council'][method]() as unknown as Observable<T>
3636
: of(empty)
37-
)
3837
);
3938
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,21 @@ import { map, of } from 'rxjs';
1111

1212
import { isFunction } from '@polkadot/util';
1313

14-
import { memo } from '../util';
1514
import { withSection } from './helpers';
1615

1716
// We are re-exporting these from here to ensure that *.d.ts generation is correct
1817
export type { ApiInterfaceRx } from '@polkadot/api/types';
1918
export type { AccountId } from '@polkadot/types/interfaces';
2019

2120
export function prime (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable<AccountId | null> {
22-
return withSection(_section, (section, instanceId, api) =>
23-
memo(instanceId, (): Observable<AccountId | null> =>
21+
return withSection(_section, (section, api) =>
22+
(): Observable<AccountId | null> =>
2423
isFunction(api.query[section as 'council']?.prime)
2524
? api.query[section as 'council'].prime<Option<AccountId>>().pipe(
2625
map((optPrime): AccountId | null =>
2726
optPrime.unwrapOr(null)
2827
)
2928
)
3029
: of(null)
31-
)
3230
);
3331
}

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

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { catchError, combineLatest, map, of, switchMap } from 'rxjs';
1212

1313
import { isFunction } from '@polkadot/util';
1414

15-
import { memo } from '../util';
15+
import { firstObservable } from '../util';
1616
import { 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

6759
export 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

7566
export 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

8575
export 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

Comments
 (0)