Skip to content

Commit 1a6025e

Browse files
authored
Collective dedupe (#4319)
1 parent 83655c9 commit 1a6025e

File tree

3 files changed

+24
-40
lines changed

3 files changed

+24
-40
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import type { Observable } from 'rxjs';
45
import type { ApiInterfaceRx } from '@polkadot/api/types';
56
import type { Collective } from './types';
67

8+
import { of } from 'rxjs';
9+
10+
import { isFunction } from '@polkadot/util';
11+
12+
import { memo } from '../util';
13+
714
// We are re-exporting these from here to ensure that *.d.ts generation is correct
815
export type { ApiInterfaceRx } from '@polkadot/api/types';
916

@@ -19,3 +26,14 @@ export function withSection <T> (_section: Collective, fn: (section: string, ins
1926
return (instanceId: string, api: ApiInterfaceRx) =>
2027
fn(getInstance(api, _section), instanceId, api);
2128
}
29+
30+
export function callMethod <T> (method: 'members' | 'proposals' | 'proposalCount', empty: T): (_section: Collective) => (instanceId: string, api: ApiInterfaceRx) => () => Observable<T> {
31+
return (_section: Collective) =>
32+
withSection(_section, (section, instanceId, api) =>
33+
memo(instanceId, (): Observable<T> =>
34+
isFunction(api.query[section]?.[method])
35+
? api.query[section as 'council'][method]() as unknown as Observable<T>
36+
: of(empty)
37+
)
38+
);
39+
}
Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,11 @@
11
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import type { Observable } from 'rxjs';
5-
import type { ApiInterfaceRx } from '@polkadot/api/types';
64
import type { AccountId } from '@polkadot/types/interfaces';
7-
import type { Collective } from './types';
85

9-
import { of } from 'rxjs';
10-
11-
import { isFunction } from '@polkadot/util';
12-
13-
import { memo } from '../util';
14-
import { withSection } from './helpers';
6+
import { callMethod } from './helpers';
157

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

20-
export function members (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable<AccountId[]> {
21-
return withSection(_section, (section, instanceId, api) =>
22-
memo(instanceId, (): Observable<AccountId[]> =>
23-
isFunction(api.query[section]?.members)
24-
? api.query[section as 'council'].members()
25-
: of([])
26-
)
27-
);
28-
}
11+
export const members = callMethod<AccountId[]>('members', []);

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

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { catchError, combineLatest, map, of, switchMap } from 'rxjs';
1313
import { isFunction } from '@polkadot/util';
1414

1515
import { memo } from '../util';
16-
import { withSection } from './helpers';
16+
import { callMethod, withSection } from './helpers';
1717

1818
// We are re-exporting these from here to ensure that *.d.ts generation is correct
1919
export type { ApiInterfaceRx } from '@polkadot/api/types';
@@ -72,26 +72,6 @@ export function hasProposals (_section: Collective): (instanceId: string, api: A
7272
);
7373
}
7474

75-
export function proposalCount (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable<u32 | null> {
76-
return withSection(_section, (section, instanceId, api) =>
77-
memo(instanceId, (): Observable<u32 | null> =>
78-
isFunction(api.query[section].proposalCount)
79-
? api.query[section as 'council'].proposalCount()
80-
: of(null)
81-
)
82-
);
83-
}
84-
85-
export function proposalHashes (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable<Hash[]> {
86-
return withSection(_section, (section, instanceId, api) =>
87-
memo(instanceId, (): Observable<Hash[]> =>
88-
isFunction(api.query[section]?.proposals)
89-
? api.query[section as 'council'].proposals()
90-
: of([])
91-
)
92-
);
93-
}
94-
9575
export function proposals (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable<DeriveCollectiveProposal[]> {
9676
return withProposals(_section, (section, instanceId, api, proposalsFrom) =>
9777
memo(instanceId, (): Observable<DeriveCollectiveProposal[]> =>
@@ -113,3 +93,6 @@ export function proposal (_section: Collective): (instanceId: string, api: ApiIn
11393
)
11494
);
11595
}
96+
97+
export const proposalCount = callMethod<u32 | null>('proposalCount', null);
98+
export const proposalHashes = callMethod<Hash[]>('proposals', []);

0 commit comments

Comments
 (0)