Collective dedupe (#4319)

This commit is contained in:
Jaco
2021-12-11 10:08:41 +02:00
committed by GitHub
parent 83655c92c4
commit 1a6025e012
3 changed files with 24 additions and 40 deletions
@@ -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 <T> (_section: Collective, fn: (section: string, ins
return (instanceId: string, api: ApiInterfaceRx) =>
fn(getInstance(api, _section), instanceId, api);
}
export function callMethod <T> (method: 'members' | 'proposals' | 'proposalCount', empty: T): (_section: Collective) => (instanceId: string, api: ApiInterfaceRx) => () => Observable<T> {
return (_section: Collective) =>
withSection(_section, (section, instanceId, api) =>
memo(instanceId, (): Observable<T> =>
isFunction(api.query[section]?.[method])
? api.query[section as 'council'][method]() as unknown as Observable<T>
: of(empty)
)
);
}
+2 -19
View File
@@ -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<AccountId[]> {
return withSection(_section, (section, instanceId, api) =>
memo(instanceId, (): Observable<AccountId[]> =>
isFunction(api.query[section]?.members)
? api.query[section as 'council'].members()
: of([])
)
);
}
export const members = callMethod<AccountId[]>('members', []);
@@ -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<u32 | null> {
return withSection(_section, (section, instanceId, api) =>
memo(instanceId, (): Observable<u32 | null> =>
isFunction(api.query[section].proposalCount)
? api.query[section as 'council'].proposalCount()
: of(null)
)
);
}
export function proposalHashes (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable<Hash[]> {
return withSection(_section, (section, instanceId, api) =>
memo(instanceId, (): Observable<Hash[]> =>
isFunction(api.query[section]?.proposals)
? api.query[section as 'council'].proposals()
: of([])
)
);
}
export function proposals (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable<DeriveCollectiveProposal[]> {
return withProposals(_section, (section, instanceId, api, proposalsFrom) =>
memo(instanceId, (): Observable<DeriveCollectiveProposal[]> =>
@@ -113,3 +93,6 @@ export function proposal (_section: Collective): (instanceId: string, api: ApiIn
)
);
}
export const proposalCount = callMethod<u32 | null>('proposalCount', null);
export const proposalHashes = callMethod<Hash[]>('proposals', []);