diff --git a/packages/api-derive/src/collective/helpers.ts b/packages/api-derive/src/collective/helpers.ts index c41567236d..74b1bf0730 100644 --- a/packages/api-derive/src/collective/helpers.ts +++ b/packages/api-derive/src/collective/helpers.ts @@ -1,9 +1,16 @@ // Copyright 2017-2021 @polkadot/api-derive authors & contributors // SPDX-License-Identifier: Apache-2.0 +import type { Observable } from 'rxjs'; import type { ApiInterfaceRx } from '@polkadot/api/types'; import type { Collective } from './types'; +import { of } from 'rxjs'; + +import { isFunction } from '@polkadot/util'; + +import { memo } from '../util'; + // We are re-exporting these from here to ensure that *.d.ts generation is correct export type { ApiInterfaceRx } from '@polkadot/api/types'; @@ -19,3 +26,14 @@ export function withSection (_section: Collective, fn: (section: string, ins return (instanceId: string, api: ApiInterfaceRx) => fn(getInstance(api, _section), instanceId, api); } + +export function callMethod (method: 'members' | 'proposals' | 'proposalCount', empty: T): (_section: Collective) => (instanceId: string, api: ApiInterfaceRx) => () => Observable { + return (_section: Collective) => + withSection(_section, (section, instanceId, api) => + memo(instanceId, (): Observable => + isFunction(api.query[section]?.[method]) + ? api.query[section as 'council'][method]() as unknown as Observable + : of(empty) + ) + ); +} diff --git a/packages/api-derive/src/collective/members.ts b/packages/api-derive/src/collective/members.ts index 77a850fdd9..f1185d7f0b 100644 --- a/packages/api-derive/src/collective/members.ts +++ b/packages/api-derive/src/collective/members.ts @@ -1,28 +1,11 @@ // Copyright 2017-2021 @polkadot/api-derive authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { Observable } from 'rxjs'; -import type { ApiInterfaceRx } from '@polkadot/api/types'; import type { AccountId } from '@polkadot/types/interfaces'; -import type { Collective } from './types'; -import { of } from 'rxjs'; - -import { isFunction } from '@polkadot/util'; - -import { memo } from '../util'; -import { withSection } from './helpers'; +import { callMethod } from './helpers'; // We are re-exporting these from here to ensure that *.d.ts generation is correct -export type { ApiInterfaceRx } from '@polkadot/api/types'; export type { AccountId } from '@polkadot/types/interfaces'; -export function members (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable { - return withSection(_section, (section, instanceId, api) => - memo(instanceId, (): Observable => - isFunction(api.query[section]?.members) - ? api.query[section as 'council'].members() - : of([]) - ) - ); -} +export const members = callMethod('members', []); diff --git a/packages/api-derive/src/collective/proposals.ts b/packages/api-derive/src/collective/proposals.ts index b40548d69d..80ec083355 100644 --- a/packages/api-derive/src/collective/proposals.ts +++ b/packages/api-derive/src/collective/proposals.ts @@ -13,7 +13,7 @@ import { catchError, combineLatest, map, of, switchMap } from 'rxjs'; import { isFunction } from '@polkadot/util'; import { memo } from '../util'; -import { withSection } from './helpers'; +import { callMethod, withSection } from './helpers'; // We are re-exporting these from here to ensure that *.d.ts generation is correct export type { ApiInterfaceRx } from '@polkadot/api/types'; @@ -72,26 +72,6 @@ export function hasProposals (_section: Collective): (instanceId: string, api: A ); } -export function proposalCount (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable { - return withSection(_section, (section, instanceId, api) => - memo(instanceId, (): Observable => - isFunction(api.query[section].proposalCount) - ? api.query[section as 'council'].proposalCount() - : of(null) - ) - ); -} - -export function proposalHashes (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable { - return withSection(_section, (section, instanceId, api) => - memo(instanceId, (): Observable => - isFunction(api.query[section]?.proposals) - ? api.query[section as 'council'].proposals() - : of([]) - ) - ); -} - export function proposals (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable { return withProposals(_section, (section, instanceId, api, proposalsFrom) => memo(instanceId, (): Observable => @@ -113,3 +93,6 @@ export function proposal (_section: Collective): (instanceId: string, api: ApiIn ) ); } + +export const proposalCount = callMethod('proposalCount', null); +export const proposalHashes = callMethod('proposals', []);