Additional derive cleanups (#4307)
* Additional derive cleanups * Adjust * Multi cache set * Adjust mapping
This commit is contained in:
+6
@@ -2,6 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import type { Collective } from './types';
|
||||
|
||||
export function getInstance (api: ApiInterfaceRx, section: string): string {
|
||||
const instances = api.registry.getModuleInstances(api.runtimeVersion.specName.toString(), section);
|
||||
@@ -10,3 +11,8 @@ export function getInstance (api: ApiInterfaceRx, section: string): string {
|
||||
? instances[0]
|
||||
: section;
|
||||
}
|
||||
|
||||
export function withSection <T> (_section: Collective, fn: (section: string, instanceId: string, api: ApiInterfaceRx) => T): (instanceId: string, api: ApiInterfaceRx) => T {
|
||||
return (instanceId: string, api: ApiInterfaceRx) =>
|
||||
fn(getInstance(api, _section), instanceId, api);
|
||||
}
|
||||
@@ -11,16 +11,14 @@ import { of } from 'rxjs';
|
||||
import { isFunction } from '@polkadot/util';
|
||||
|
||||
import { memo } from '../util';
|
||||
import { getInstance } from './getInstance';
|
||||
import { withSection } from './helpers';
|
||||
|
||||
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[]> =>
|
||||
return withSection(_section, (section, instanceId, api) =>
|
||||
memo(instanceId, (): Observable<AccountId[]> =>
|
||||
isFunction(api.query[section]?.members)
|
||||
? api.query[section as 'council'].members()
|
||||
: of([])
|
||||
);
|
||||
};
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -5,19 +5,18 @@ import type { Observable } from 'rxjs';
|
||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import type { Option } from '@polkadot/types';
|
||||
import type { AccountId } from '@polkadot/types/interfaces';
|
||||
import type { Collective } from './types';
|
||||
|
||||
import { map, of } from 'rxjs';
|
||||
|
||||
import { isFunction } from '@polkadot/util';
|
||||
|
||||
import { memo } from '../util';
|
||||
import { getInstance } from './getInstance';
|
||||
import { withSection } from './helpers';
|
||||
|
||||
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> =>
|
||||
export function prime (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable<AccountId | null> {
|
||||
return withSection(_section, (section, instanceId, api) =>
|
||||
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 =>
|
||||
@@ -25,6 +24,6 @@ export function prime (_section: string): (instanceId: string, api: ApiInterface
|
||||
)
|
||||
)
|
||||
: of(null)
|
||||
);
|
||||
};
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -13,17 +13,17 @@ import { catchError, combineLatest, map, of, switchMap } from 'rxjs';
|
||||
import { isFunction } from '@polkadot/util';
|
||||
|
||||
import { memo } from '../util';
|
||||
import { getInstance } from './getInstance';
|
||||
import { withSection } from './helpers';
|
||||
|
||||
type Result = [(Hash | Uint8Array | string)[], (Option<Proposal> | null)[], Option<Votes>[]];
|
||||
|
||||
function parse (api: ApiInterfaceRx, [hashes, proposals, votes]: Result): DeriveCollectiveProposal[] {
|
||||
return proposals
|
||||
.map((proposalOpt, index): DeriveCollectiveProposal | null =>
|
||||
proposalOpt && proposalOpt.isSome
|
||||
.map((o, index): DeriveCollectiveProposal | null =>
|
||||
o && o.isSome
|
||||
? {
|
||||
hash: api.registry.createType('Hash', hashes[index]),
|
||||
proposal: proposalOpt.unwrap(),
|
||||
proposal: o.unwrap(),
|
||||
votes: votes[index].unwrapOr(null)
|
||||
}
|
||||
: null
|
||||
@@ -31,7 +31,7 @@ function parse (api: ApiInterfaceRx, [hashes, proposals, votes]: Result): Derive
|
||||
.filter((proposal): proposal is DeriveCollectiveProposal => !!proposal);
|
||||
}
|
||||
|
||||
function _proposalsFrom (instanceId: string, api: ApiInterfaceRx, section: string): (hashes: (Hash | Uint8Array | string)[]) => Observable<DeriveCollectiveProposal[]> {
|
||||
function _proposalsFrom (section: string, instanceId: string, api: ApiInterfaceRx): (hashes: (Hash | Uint8Array | string)[]) => Observable<DeriveCollectiveProposal[]> {
|
||||
return memo(instanceId, (hashes: (Hash | Uint8Array | string)[]): Observable<DeriveCollectiveProposal[]> =>
|
||||
(isFunction(api.query[section]?.proposals) && hashes.length
|
||||
? combineLatest([
|
||||
@@ -39,8 +39,8 @@ function _proposalsFrom (instanceId: string, api: ApiInterfaceRx, section: strin
|
||||
// this should simply be api.query[section].proposalOf.multi<Option<Proposal>>(hashes),
|
||||
// however we have had cases on Edgeware where the indices have moved around after an
|
||||
// upgrade, which results in invalid on-chain data
|
||||
combineLatest(hashes.map((hash) =>
|
||||
api.query[section].proposalOf<Option<Proposal>>(hash).pipe(
|
||||
combineLatest(hashes.map((h) =>
|
||||
api.query[section].proposalOf<Option<Proposal>>(h).pipe(
|
||||
catchError(() => of(null))
|
||||
)
|
||||
)),
|
||||
@@ -48,69 +48,63 @@ function _proposalsFrom (instanceId: string, api: ApiInterfaceRx, section: strin
|
||||
])
|
||||
: of<Result>([[], [], []])
|
||||
).pipe(
|
||||
map((result) => parse(api, result))
|
||||
map((r) => parse(api, r))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
export function hasProposals (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable<boolean> {
|
||||
return (instanceId: string, api: ApiInterfaceRx) => {
|
||||
const section = getInstance(api, _section);
|
||||
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 {
|
||||
return withSection(_section, (section, instanceId, api) =>
|
||||
fn(section, instanceId, api, _proposalsFrom(section, instanceId, api))
|
||||
);
|
||||
}
|
||||
|
||||
return memo(instanceId, (): Observable<boolean> =>
|
||||
export function hasProposals (_section: Collective): (instanceId: string, api: ApiInterfaceRx) => () => Observable<boolean> {
|
||||
return withSection(_section, (section, instanceId, api) =>
|
||||
memo(instanceId, (): Observable<boolean> =>
|
||||
of(isFunction(api.query[section]?.proposals))
|
||||
);
|
||||
};
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
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> =>
|
||||
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 (instanceId: string, api: ApiInterfaceRx) => {
|
||||
const section = getInstance(api, _section);
|
||||
|
||||
return memo(instanceId, (): 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 (instanceId: string, api: ApiInterfaceRx) => {
|
||||
const section = getInstance(api, _section);
|
||||
const proposalsFrom = _proposalsFrom(instanceId, api, section);
|
||||
|
||||
return memo(instanceId, (): Observable<DeriveCollectiveProposal[]> =>
|
||||
return withProposals(_section, (section, instanceId, api, proposalsFrom) =>
|
||||
memo(instanceId, (): Observable<DeriveCollectiveProposal[]> =>
|
||||
api.derive[section as 'council'].proposalHashes().pipe(
|
||||
switchMap(proposalsFrom)
|
||||
)
|
||||
);
|
||||
};
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
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> =>
|
||||
return withProposals(_section, (section, instanceId, api, proposalsFrom) =>
|
||||
memo(instanceId, (hash: Hash | Uint8Array | string): Observable<DeriveCollectiveProposal | null> =>
|
||||
isFunction(api.query[section]?.proposals)
|
||||
? proposalsFrom([hash]).pipe(
|
||||
map(([proposal]) => proposal)
|
||||
)
|
||||
: of(null)
|
||||
);
|
||||
};
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user