Lazy decoration (part 3, derives) (#4142)
* Lazy decoration (part 3, prep for derives) * Derives are lazy * ... it should have derives * dedupe api-derive lazy * function -> inline * Cleanup defineProperty * Adjust * Final readability tweaks * ... I lied, alignment tweaks * Exportable lazyDeriveSection * lazy from @polkadot/types * Adjust
This commit is contained in:
@@ -5,6 +5,7 @@ import type { Observable } from 'rxjs';
|
||||
import type { ApiInterfaceRx } from '@polkadot/api/types';
|
||||
import type { AnyFunction } from '@polkadot/types/types';
|
||||
|
||||
import { lazyDeriveSection } from './util/lazy';
|
||||
import * as accounts from './accounts';
|
||||
import * as balances from './balances';
|
||||
import * as bounties from './bounties';
|
||||
@@ -32,6 +33,8 @@ interface Avail {
|
||||
withDetect?: boolean;
|
||||
}
|
||||
|
||||
export { lazyDeriveSection };
|
||||
|
||||
export const derive = { accounts, balances, bounties, chain, contracts, council, crowdloan, democracy, elections, imOnline, membership, parachains, session, society, staking, technicalCommittee, treasury, tx };
|
||||
|
||||
type DeriveSection<Section> = {
|
||||
@@ -66,49 +69,60 @@ const checks: Record<string, Avail> = {
|
||||
treasury: { instances: ['treasury'] }
|
||||
};
|
||||
|
||||
function getModuleInstances (api: ApiInterfaceRx, specName: string, moduleName: string): string[] {
|
||||
return api.registry.getModuleInstances(specName, moduleName) || [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an object that will inject `api` into all the functions inside
|
||||
* `allSections`, and keep the object architecture of `allSections`.
|
||||
*/
|
||||
/** @internal */
|
||||
function injectFunctions<AllSections> (instanceId: string, api: ApiInterfaceRx, allSections: AllSections): DeriveAllSections<AllSections> {
|
||||
function injectFunctions (instanceId: string, api: ApiInterfaceRx, derives: DeriveCustom): ExactDerive {
|
||||
const queryKeys = Object.keys(api.query);
|
||||
const specName = api.runtimeVersion.specName.toString();
|
||||
|
||||
return Object
|
||||
.keys(allSections)
|
||||
.filter((sectionName) =>
|
||||
!checks[sectionName] ||
|
||||
checks[sectionName].instances.some((q) => queryKeys.includes(q)) ||
|
||||
(
|
||||
checks[sectionName].withDetect &&
|
||||
checks[sectionName].instances.some((q) =>
|
||||
(api.registry.getModuleInstances(specName, q) || []).some((q) => queryKeys.includes(q))
|
||||
)
|
||||
)
|
||||
const filterQueryKeys = (q: string) =>
|
||||
queryKeys.includes(q);
|
||||
|
||||
const filterInstances = (q: string) =>
|
||||
getModuleInstances(api, specName, q).some(filterQueryKeys);
|
||||
|
||||
const isIncluded = (s: string) => (
|
||||
!checks[s] ||
|
||||
checks[s].instances.some(filterQueryKeys) ||
|
||||
(
|
||||
checks[s].withDetect &&
|
||||
checks[s].instances.some(filterInstances)
|
||||
)
|
||||
.reduce((derives, sectionName): DeriveAllSections<AllSections> => {
|
||||
const section = allSections[sectionName as keyof AllSections];
|
||||
);
|
||||
|
||||
derives[sectionName as keyof AllSections] = Object
|
||||
.entries(section)
|
||||
.reduce((methods, [methodName, creator]): DeriveSection<typeof section> => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-call
|
||||
methods[methodName as keyof typeof section] = creator(instanceId, api);
|
||||
const getKeys = (s: string) =>
|
||||
Object.keys(derives[s]);
|
||||
|
||||
return methods;
|
||||
}, {} as DeriveSection<typeof section>);
|
||||
const creator = (s: string, m: string) =>
|
||||
derives[s][m](instanceId, api);
|
||||
|
||||
return derives;
|
||||
}, {} as DeriveAllSections<AllSections>);
|
||||
const result: Record<string, Record<string, AnyFunction>> = {};
|
||||
const names = Object.keys(derives);
|
||||
|
||||
for (let i = 0; i < names.length; i++) {
|
||||
const name = names[i];
|
||||
|
||||
if (isIncluded(name)) {
|
||||
lazyDeriveSection(result, name, getKeys, creator);
|
||||
}
|
||||
}
|
||||
|
||||
return result as ExactDerive;
|
||||
}
|
||||
|
||||
// FIXME The return type of this function should be {...ExactDerive, ...DeriveCustom}
|
||||
// For now we just drop the custom derive typings
|
||||
/** @internal */
|
||||
export function decorateDerive (instanceId: string, api: ApiInterfaceRx, custom: DeriveCustom = {}): ExactDerive {
|
||||
export function getAvailableDerives (instanceId: string, api: ApiInterfaceRx, custom: DeriveCustom = {}): ExactDerive {
|
||||
return {
|
||||
...injectFunctions(instanceId, api, derive),
|
||||
...injectFunctions(instanceId, api, derive as DeriveCustom),
|
||||
...injectFunctions(instanceId, api, custom)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { lazyMethod, lazyMethods } from '@polkadot/types';
|
||||
|
||||
type LazySection <T> = Record<string, T>;
|
||||
|
||||
type LazyRecord <T> = Record<string, LazySection<T>>;
|
||||
|
||||
export function lazyDeriveSection <T> (result: LazyRecord<T>, section: string, getKeys: (s: string) => string[], creator: (s: string, m: string) => T): void {
|
||||
lazyMethod(result, section, (s) => lazyMethods(getKeys(s), (m) => creator(s, m)));
|
||||
}
|
||||
@@ -14,7 +14,7 @@ import type { VersionedRegistry } from './types';
|
||||
|
||||
import { BehaviorSubject, combineLatest, from, map, of, switchMap, tap, toArray } from 'rxjs';
|
||||
|
||||
import { decorateDerive, ExactDerive } from '@polkadot/api-derive';
|
||||
import { ExactDerive, getAvailableDerives } from '@polkadot/api-derive';
|
||||
import { memo, RpcCore } from '@polkadot/rpc-core';
|
||||
import { WsProvider } from '@polkadot/rpc-provider';
|
||||
import { expandMetadata, Metadata, TypeRegistry, unwrapStorageType } from '@polkadot/types';
|
||||
@@ -22,7 +22,7 @@ import { arrayChunk, arrayFlatten, assert, BN, BN_ZERO, compactStripLength, logg
|
||||
|
||||
import { createSubmittable } from '../submittable';
|
||||
import { augmentObject } from '../util/augmentObject';
|
||||
import { decorateSections, DeriveAllSections } from '../util/decorate';
|
||||
import { decorateDeriveSections, DeriveAllSections } from '../util/decorate';
|
||||
import { extractStorageArgs } from '../util/validate';
|
||||
import { Events } from './Events';
|
||||
import { findCall, findError } from './find';
|
||||
@@ -734,19 +734,18 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
|
||||
|
||||
protected _decorateDeriveRx (decorateMethod: DecorateMethod<ApiType>): DeriveAllSections<'rxjs', ExactDerive> {
|
||||
const specName = this._runtimeVersion?.specName.toString();
|
||||
const derives = {
|
||||
...this._options.derives,
|
||||
...(this._options.typesBundle?.spec?.[specName || '']?.derives || {})
|
||||
};
|
||||
|
||||
// Pull in derive from api-derive
|
||||
const derive = decorateDerive(this.#instanceId, this._rx, derives);
|
||||
const available = getAvailableDerives(this.#instanceId, this._rx, {
|
||||
...this._options.derives,
|
||||
...(this._options.typesBundle?.spec?.[specName || '']?.derives || {})
|
||||
});
|
||||
|
||||
return decorateSections<'rxjs', ExactDerive>(derive, decorateMethod);
|
||||
return decorateDeriveSections<'rxjs', ExactDerive>(decorateMethod, available);
|
||||
}
|
||||
|
||||
protected _decorateDerive (decorateMethod: DecorateMethod<ApiType>): DeriveAllSections<ApiType, ExactDerive> {
|
||||
return decorateSections<ApiType, ExactDerive>(this._rx.derive, decorateMethod);
|
||||
return decorateDeriveSections<ApiType, ExactDerive>(decorateMethod, this._rx.derive);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -64,4 +64,5 @@ export interface PaginationOptions<A = unknown> {
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
export type DecorateMethod<ApiType extends ApiTypes> = <Method extends (...args: any[]) => Observable<any>>(method: Method, options?: DecorateMethodOptions) => any;
|
||||
export type DecorateMethod<ApiType extends ApiTypes> =
|
||||
<M extends (...args: any[]) => Observable<any>>(method: M, options?: DecorateMethodOptions) => any;
|
||||
|
||||
@@ -4,11 +4,15 @@
|
||||
import type { AnyFunction } from '@polkadot/types/types';
|
||||
import type { ApiTypes, DecorateMethod, MethodResult } from '../types';
|
||||
|
||||
import { lazyDeriveSection } from '@polkadot/api-derive';
|
||||
|
||||
type AnyDeriveSection = Record<string, AnyFunction>;
|
||||
|
||||
// Most generic typings for `api.derive.*.*`
|
||||
type AnyDerive = Record<string, Record<string, AnyFunction>>;
|
||||
type AnyDerive = Record<string, AnyDeriveSection>;
|
||||
|
||||
// Exact typings for a particular section `api.derive.section.*`
|
||||
type DeriveSection<ApiType extends ApiTypes, Section extends Record<string, AnyFunction>> = {
|
||||
type DeriveSection<ApiType extends ApiTypes, Section extends AnyDeriveSection> = {
|
||||
[MethodName in keyof Section]: MethodResult<ApiType, Section[MethodName]>
|
||||
};
|
||||
|
||||
@@ -17,37 +21,22 @@ export type DeriveAllSections<ApiType extends ApiTypes, AllSections extends AnyD
|
||||
[SectionName in keyof AllSections]: DeriveSection<ApiType, AllSections[SectionName]>
|
||||
};
|
||||
|
||||
// A technically unsafe version of Object.keys(obj) that assumes that
|
||||
// obj only has known properties of T
|
||||
function keys<T extends Record<string, unknown>> (obj: T): (keyof T)[] {
|
||||
return Object.keys(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a methods decorator which keeps all type information.
|
||||
*/
|
||||
function decorateMethods<ApiType extends ApiTypes, Section extends Record<string, AnyFunction>> (section: Section, decorateMethod: DecorateMethod<ApiType>): DeriveSection<ApiType, Section> {
|
||||
const result = {} as DeriveSection<ApiType, Section>;
|
||||
const names = keys(section);
|
||||
|
||||
for (let k = 0; k < names.length; k++) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
result[names[k]] = decorateMethod(section[names[k]]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a section decorator which keeps all type information.
|
||||
*/
|
||||
export function decorateSections<ApiType extends ApiTypes, AllSections extends AnyDerive> (allSections: AllSections, decorateMethod: DecorateMethod<ApiType>): DeriveAllSections<ApiType, AllSections> {
|
||||
const result = {} as DeriveAllSections<ApiType, AllSections>;
|
||||
const names = keys(allSections);
|
||||
export function decorateDeriveSections<ApiType extends ApiTypes, A extends AnyDerive> (decorateMethod: DecorateMethod<ApiType>, derives: AnyDerive): DeriveAllSections<ApiType, A> {
|
||||
const getKeys = (s: string) =>
|
||||
Object.keys(derives[s]);
|
||||
|
||||
for (let k = 0; k < names.length; k++) {
|
||||
result[names[k]] = decorateMethods(allSections[names[k]], decorateMethod);
|
||||
const creator = (s: string, m: string) =>
|
||||
decorateMethod(derives[s][m]) as AnyFunction;
|
||||
|
||||
const result: AnyDerive = {};
|
||||
const names = Object.keys(derives);
|
||||
|
||||
for (let i = 0; i < names.length; i++) {
|
||||
lazyDeriveSection(result, names[i], getKeys, creator);
|
||||
}
|
||||
|
||||
return result;
|
||||
return result as DeriveAllSections<ApiType, A>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user