Dedupe collectives (#4302)

* Dedupe collectives

* Members

* Type imports (unused)

* Work-around for TS type imports

* tsconfig

* Adjust with only local imports
This commit is contained in:
Jaco
2021-12-08 18:50:18 +02:00
committed by GitHub
parent bf753cb858
commit 1cc9756d0e
16 changed files with 144 additions and 239 deletions
@@ -13,12 +13,14 @@ import { isFunction } from '@polkadot/util';
import { memo } from '../util';
import { getInstance } from './getInstance';
export function members (instanceId: string, api: ApiInterfaceRx, _section: Collective): () => Observable<AccountId[]> {
const section = getInstance(api, _section);
export function members (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable<AccountId[]> {
return (instanceId: string, api: ApiInterfaceRx) => {
const section = getInstance(api, _section);
return memo(instanceId, (): Observable<AccountId[]> =>
isFunction(api.query[section]?.members)
? api.query[section as 'council'].members()
: of([])
);
return memo(instanceId, (): Observable<AccountId[]> =>
isFunction(api.query[section]?.members)
? api.query[section as 'council'].members()
: of([])
);
};
}
+12 -10
View File
@@ -13,16 +13,18 @@ import { isFunction } from '@polkadot/util';
import { memo } from '../util';
import { getInstance } from './getInstance';
export function prime (instanceId: string, api: ApiInterfaceRx, _section: string): () => Observable<AccountId | null> {
const section = getInstance(api, _section);
export function prime (_section: string): (instanceId: string, api: ApiInterfaceRx) => () => Observable<AccountId | null> {
return (instanceId: string, api: ApiInterfaceRx) => {
const section = getInstance(api, _section);
return memo(instanceId, (): Observable<AccountId | null> =>
isFunction(api.query[section as 'council']?.prime)
? api.query[section as 'council'].prime<Option<AccountId>>().pipe(
map((optPrime): AccountId | null =>
optPrime.unwrapOr(null)
return memo(instanceId, (): Observable<AccountId | null> =>
isFunction(api.query[section as 'council']?.prime)
? api.query[section as 'council'].prime<Option<AccountId>>().pipe(
map((optPrime): AccountId | null =>
optPrime.unwrapOr(null)
)
)
)
: of(null)
);
: of(null)
);
};
}
+49 -40
View File
@@ -53,55 +53,64 @@ function _proposalsFrom (instanceId: string, api: ApiInterfaceRx, section: strin
);
}
export function hasProposals (instanceId: string, api: ApiInterfaceRx, _section: Collective): () => Observable<boolean> {
const section = getInstance(api, _section);
export function hasProposals (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable<boolean> {
return (instanceId: string, api: ApiInterfaceRx) => {
const section = getInstance(api, _section);
return memo(instanceId, (): Observable<boolean> =>
of(isFunction(api.query[section]?.proposals))
);
return memo(instanceId, (): Observable<boolean> =>
of(isFunction(api.query[section]?.proposals))
);
};
}
export function proposalCount (instanceId: string, api: ApiInterfaceRx, _section: Collective): () => Observable<u32 | null> {
const section = getInstance(api, _section);
export function proposalCount (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable<u32 | null> {
return (instanceId: string, api: ApiInterfaceRx) => {
const section = getInstance(api, _section);
return memo(instanceId, (): Observable<u32 | null> =>
isFunction(api.query[section].proposalCount)
? api.query[section as 'council'].proposalCount()
: of(null)
);
return memo(instanceId, (): Observable<u32 | null> =>
isFunction(api.query[section].proposalCount)
? api.query[section as 'council'].proposalCount()
: of(null)
);
};
}
export function proposalHashes (instanceId: string, api: ApiInterfaceRx, _section: Collective): () => Observable<Hash[]> {
const section = getInstance(api, _section);
export function proposalHashes (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable<Hash[]> {
return (instanceId: string, api: ApiInterfaceRx) => {
const section = getInstance(api, _section);
return memo(instanceId, (): Observable<Hash[]> =>
isFunction(api.query[section]?.proposals)
? api.query[section as 'council'].proposals()
: of([])
);
return memo(instanceId, (): Observable<Hash[]> =>
isFunction(api.query[section]?.proposals)
? api.query[section as 'council'].proposals()
: of([])
);
};
}
export function proposals (instanceId: string, api: ApiInterfaceRx, _section: Collective): () => Observable<DeriveCollectiveProposal[]> {
const section = getInstance(api, _section);
const proposalsFrom = _proposalsFrom(instanceId, api, section);
const getHashes = proposalHashes(instanceId, api, _section);
export function proposals (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable<DeriveCollectiveProposal[]> {
return (instanceId: string, api: ApiInterfaceRx) => {
const section = getInstance(api, _section);
const proposalsFrom = _proposalsFrom(instanceId, api, section);
return memo(instanceId, (): Observable<DeriveCollectiveProposal[]> =>
getHashes().pipe(
switchMap(proposalsFrom)
)
);
}
export function proposal (instanceId: string, api: ApiInterfaceRx, _section: Collective): (hash: Hash | Uint8Array | string) => Observable<DeriveCollectiveProposal | null> {
const section = getInstance(api, _section);
const proposalsFrom = _proposalsFrom(instanceId, api, section);
return memo(instanceId, (hash: Hash | Uint8Array | string): Observable<DeriveCollectiveProposal | null> =>
isFunction(api.query[section]?.proposals)
? proposalsFrom([hash]).pipe(
map(([proposal]) => proposal)
return memo(instanceId, (): Observable<DeriveCollectiveProposal[]> =>
api.derive[section as 'council'].proposalHashes().pipe(
switchMap(proposalsFrom)
)
: of(null)
);
);
};
}
export function proposal (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => (hash: Hash | Uint8Array | string) => Observable<DeriveCollectiveProposal | null> {
return (instanceId: string, api: ApiInterfaceRx) => {
const section = getInstance(api, _section);
const proposalsFrom = _proposalsFrom(instanceId, api, section);
return memo(instanceId, (hash: Hash | Uint8Array | string): Observable<DeriveCollectiveProposal | null> =>
isFunction(api.query[section]?.proposals)
? proposalsFrom([hash]).pipe(
map(([proposal]) => proposal)
)
: of(null)
);
};
}
+25 -3
View File
@@ -1,8 +1,30 @@
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
// SPDX-License-Identifier: Apache-2.0
export * from './members';
export * from './prime';
export * from './proposals';
/* eslint-disable @typescript-eslint/no-unused-vars */
// we need these type imports to be available
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import type { ApiInterfaceRx } from '@polkadot/api/types';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import type { u32 } from '@polkadot/types';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import type { AccountId, Hash } from '@polkadot/types/interfaces';
import { hasProposals as collectiveHasProposals, members as collectiveMembers, prime as collectivePrime, proposal as collectiveProposal, proposalCount as collectiveProposalCount, proposalHashes as collectiveProposalHashes, proposals as collectiveProposals } from '../collective';
export * from './votes';
export * from './votesOf';
export const members = collectiveMembers('council');
export const hasProposals = collectiveHasProposals('council');
export const proposal = collectiveProposal('council');
export const proposalCount = collectiveProposalCount('council');
export const proposalHashes = collectiveProposalHashes('council');
export const proposals = collectiveProposals('council');
export const prime = collectivePrime('council');
@@ -1,13 +0,0 @@
// 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 { members as collectiveMembers } from '../collective';
import { memo } from '../util';
export function members (instanceId: string, api: ApiInterfaceRx): () => Observable<AccountId[]> {
return memo(instanceId, collectiveMembers(instanceId, api, 'council'));
}
-13
View File
@@ -1,13 +0,0 @@
// 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 { prime as collectivePrime } from '../collective';
import { memo } from '../util';
export function prime (instanceId: string, api: ApiInterfaceRx): () => Observable<AccountId | null> {
return memo(instanceId, collectivePrime(instanceId, api, 'council'));
}
@@ -1,31 +0,0 @@
// 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 { u32 } from '@polkadot/types';
import type { Hash } from '@polkadot/types/interfaces';
import type { DeriveCollectiveProposal } from '../types';
import { hasProposals as collectiveHasProposals, proposal as collectiveProposal, proposalCount as collectiveProposalCount, proposalHashes as collectiveProposalHashes, proposals as collectiveProposals } from '../collective';
import { memo } from '../util';
export function hasProposals (instanceId: string, api: ApiInterfaceRx): () => Observable<boolean> {
return memo(instanceId, collectiveHasProposals(instanceId, api, 'council'));
}
export function proposal (instanceId: string, api: ApiInterfaceRx): (hash: Hash | Uint8Array | string) => Observable<DeriveCollectiveProposal | null> {
return memo(instanceId, collectiveProposal(instanceId, api, 'council'));
}
export function proposalCount (instanceId: string, api: ApiInterfaceRx): () => Observable<u32 | null> {
return memo(instanceId, collectiveProposalCount(instanceId, api, 'council'));
}
export function proposalHashes (instanceId: string, api: ApiInterfaceRx): () => Observable<Hash[]> {
return memo(instanceId, collectiveProposalHashes(instanceId, api, 'council'));
}
export function proposals (instanceId: string, api: ApiInterfaceRx): () => Observable<DeriveCollectiveProposal[]> {
return memo(instanceId, collectiveProposals(instanceId, api, 'council'));
}
+24 -3
View File
@@ -1,6 +1,27 @@
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
// SPDX-License-Identifier: Apache-2.0
export * from './members';
export * from './prime';
export * from './proposals';
/* eslint-disable @typescript-eslint/no-unused-vars */
// we need these type imports to be available
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import type { ApiInterfaceRx } from '@polkadot/api/types';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import type { u32 } from '@polkadot/types';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import type { AccountId, Hash } from '@polkadot/types/interfaces';
import { hasProposals as collectiveHasProposals, members as collectiveMembers, prime as collectivePrime, proposal as collectiveProposal, proposalCount as collectiveProposalCount, proposalHashes as collectiveProposalHashes, proposals as collectiveProposals } from '../collective';
export const members = collectiveMembers('membership');
export const hasProposals = collectiveHasProposals('membership');
export const proposal = collectiveProposal('membership');
export const proposalCount = collectiveProposalCount('membership');
export const proposalHashes = collectiveProposalHashes('membership');
export const proposals = collectiveProposals('membership');
export const prime = collectivePrime('membership');
@@ -1,13 +0,0 @@
// 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 { members as collectiveMembers } from '../collective';
import { memo } from '../util';
export function members (instanceId: string, api: ApiInterfaceRx): () => Observable<AccountId[]> {
return memo(instanceId, collectiveMembers(instanceId, api, 'membership'));
}
@@ -1,13 +0,0 @@
// 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 { prime as collectivePrime } from '../collective';
import { memo } from '../util';
export function prime (instanceId: string, api: ApiInterfaceRx): () => Observable<AccountId | null> {
return memo(instanceId, collectivePrime(instanceId, api, 'membership'));
}
@@ -1,31 +0,0 @@
// 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 { u32 } from '@polkadot/types';
import type { Hash } from '@polkadot/types/interfaces';
import type { DeriveCollectiveProposal } from '../types';
import { hasProposals as collectiveHasProposals, proposal as collectiveProposal, proposalCount as collectiveProposalCount, proposalHashes as collectiveProposalHashes, proposals as collectiveProposals } from '../collective';
import { memo } from '../util';
export function hasProposals (instanceId: string, api: ApiInterfaceRx): () => Observable<boolean> {
return memo(instanceId, collectiveHasProposals(instanceId, api, 'membership'));
}
export function proposal (instanceId: string, api: ApiInterfaceRx): (hash: Hash | Uint8Array | string) => Observable<DeriveCollectiveProposal | null> {
return memo(instanceId, collectiveProposal(instanceId, api, 'membership'));
}
export function proposalCount (instanceId: string, api: ApiInterfaceRx): () => Observable<u32 | null> {
return memo(instanceId, collectiveProposalCount(instanceId, api, 'membership'));
}
export function proposalHashes (instanceId: string, api: ApiInterfaceRx): () => Observable<Hash[]> {
return memo(instanceId, collectiveProposalHashes(instanceId, api, 'membership'));
}
export function proposals (instanceId: string, api: ApiInterfaceRx): () => Observable<DeriveCollectiveProposal[]> {
return memo(instanceId, collectiveProposals(instanceId, api, 'membership'));
}
@@ -1,6 +1,27 @@
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
// SPDX-License-Identifier: Apache-2.0
export * from './members';
export * from './prime';
export * from './proposals';
/* eslint-disable @typescript-eslint/no-unused-vars */
// we need these type imports to be available
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import type { ApiInterfaceRx } from '@polkadot/api/types';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import type { u32 } from '@polkadot/types';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import type { AccountId, Hash } from '@polkadot/types/interfaces';
import { hasProposals as collectiveHasProposals, members as collectiveMembers, prime as collectivePrime, proposal as collectiveProposal, proposalCount as collectiveProposalCount, proposalHashes as collectiveProposalHashes, proposals as collectiveProposals } from '../collective';
export const members = collectiveMembers('technicalCommittee');
export const hasProposals = collectiveHasProposals('technicalCommittee');
export const proposal = collectiveProposal('technicalCommittee');
export const proposalCount = collectiveProposalCount('technicalCommittee');
export const proposalHashes = collectiveProposalHashes('technicalCommittee');
export const proposals = collectiveProposals('technicalCommittee');
export const prime = collectivePrime('technicalCommittee');
@@ -1,13 +0,0 @@
// 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 { members as collectiveMembers } from '../collective';
import { memo } from '../util';
export function members (instanceId: string, api: ApiInterfaceRx): () => Observable<AccountId[]> {
return memo(instanceId, collectiveMembers(instanceId, api, 'technicalCommittee'));
}
@@ -1,13 +0,0 @@
// 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 { prime as collectivePrime } from '../collective';
import { memo } from '../util';
export function prime (instanceId: string, api: ApiInterfaceRx): () => Observable<AccountId | null> {
return memo(instanceId, collectivePrime(instanceId, api, 'technicalCommittee'));
}
@@ -1,31 +0,0 @@
// 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 { u32 } from '@polkadot/types';
import type { Hash } from '@polkadot/types/interfaces';
import type { DeriveCollectiveProposal } from '../types';
import { hasProposals as collectiveHasProposals, proposal as collectiveProposal, proposalCount as collectiveProposalCount, proposalHashes as collectiveProposalHashes, proposals as collectiveProposals } from '../collective';
import { memo } from '../util';
export function hasProposals (instanceId: string, api: ApiInterfaceRx): () => Observable<boolean> {
return memo(instanceId, collectiveHasProposals(instanceId, api, 'technicalCommittee'));
}
export function proposal (instanceId: string, api: ApiInterfaceRx): (hash: Hash | Uint8Array | string) => Observable<DeriveCollectiveProposal | null> {
return memo(instanceId, collectiveProposal(instanceId, api, 'technicalCommittee'));
}
export function proposalCount (instanceId: string, api: ApiInterfaceRx): () => Observable<u32 | null> {
return memo(instanceId, collectiveProposalCount(instanceId, api, 'technicalCommittee'));
}
export function proposalHashes (instanceId: string, api: ApiInterfaceRx): () => Observable<Hash[]> {
return memo(instanceId, collectiveProposalHashes(instanceId, api, 'technicalCommittee'));
}
export function proposals (instanceId: string, api: ApiInterfaceRx): () => Observable<DeriveCollectiveProposal[]> {
return memo(instanceId, collectiveProposals(instanceId, api, 'technicalCommittee'));
}
+1 -2
View File
@@ -7,8 +7,7 @@
],
"compilerOptions": {
"baseUrl": ".",
"paths":
{
"paths": {
"@polkadot/api": ["packages/api/src"],
"@polkadot/api/*": ["packages/api/src/*"],
"@polkadot/api-contract": ["packages/api-contract/src"],