From fd027f452cac26961ecdad08eb854ae70b163610 Mon Sep 17 00:00:00 2001 From: Jaco Date: Tue, 2 Nov 2021 11:10:30 +0100 Subject: [PATCH] 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 --- packages/api-derive/src/bundle.ts | 64 +++++++++++++++++----------- packages/api-derive/src/util/lazy.ts | 12 ++++++ packages/api/src/base/Decorate.ts | 17 ++++---- packages/api/src/types/base.ts | 3 +- packages/api/src/util/decorate.ts | 47 ++++++++------------ 5 files changed, 79 insertions(+), 64 deletions(-) create mode 100644 packages/api-derive/src/util/lazy.ts diff --git a/packages/api-derive/src/bundle.ts b/packages/api-derive/src/bundle.ts index b1d5012aa9..2dc783fd48 100644 --- a/packages/api-derive/src/bundle.ts +++ b/packages/api-derive/src/bundle.ts @@ -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
= { @@ -66,49 +69,60 @@ const checks: Record = { 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 (instanceId: string, api: ApiInterfaceRx, allSections: AllSections): DeriveAllSections { +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 => { - const section = allSections[sectionName as keyof AllSections]; + ); - derives[sectionName as keyof AllSections] = Object - .entries(section) - .reduce((methods, [methodName, creator]): DeriveSection => { - // 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); + const creator = (s: string, m: string) => + derives[s][m](instanceId, api); - return derives; - }, {} as DeriveAllSections); + const result: Record> = {}; + 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) }; } diff --git a/packages/api-derive/src/util/lazy.ts b/packages/api-derive/src/util/lazy.ts new file mode 100644 index 0000000000..b45e2903ed --- /dev/null +++ b/packages/api-derive/src/util/lazy.ts @@ -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 = Record; + +type LazyRecord = Record>; + +export function lazyDeriveSection (result: LazyRecord, section: string, getKeys: (s: string) => string[], creator: (s: string, m: string) => T): void { + lazyMethod(result, section, (s) => lazyMethods(getKeys(s), (m) => creator(s, m))); +} diff --git a/packages/api/src/base/Decorate.ts b/packages/api/src/base/Decorate.ts index cc5197103d..a39ea41114 100644 --- a/packages/api/src/base/Decorate.ts +++ b/packages/api/src/base/Decorate.ts @@ -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 extends Events { protected _decorateDeriveRx (decorateMethod: DecorateMethod): 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): DeriveAllSections { - return decorateSections(this._rx.derive, decorateMethod); + return decorateDeriveSections(decorateMethod, this._rx.derive); } /** diff --git a/packages/api/src/types/base.ts b/packages/api/src/types/base.ts index fe573d0316..d103becc43 100644 --- a/packages/api/src/types/base.ts +++ b/packages/api/src/types/base.ts @@ -64,4 +64,5 @@ export interface PaginationOptions { } // eslint-disable-next-line @typescript-eslint/no-unused-vars -export type DecorateMethod = Observable>(method: Method, options?: DecorateMethodOptions) => any; +export type DecorateMethod = + Observable>(method: M, options?: DecorateMethodOptions) => any; diff --git a/packages/api/src/util/decorate.ts b/packages/api/src/util/decorate.ts index d469335044..fb4f8b5167 100644 --- a/packages/api/src/util/decorate.ts +++ b/packages/api/src/util/decorate.ts @@ -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; + // Most generic typings for `api.derive.*.*` -type AnyDerive = Record>; +type AnyDerive = Record; // Exact typings for a particular section `api.derive.section.*` -type DeriveSection> = { +type DeriveSection = { [MethodName in keyof Section]: MethodResult }; @@ -17,37 +21,22 @@ export type DeriveAllSections }; -// A technically unsafe version of Object.keys(obj) that assumes that -// obj only has known properties of T -function keys> (obj: T): (keyof T)[] { - return Object.keys(obj); -} - -/** - * This is a methods decorator which keeps all type information. - */ -function decorateMethods> (section: Section, decorateMethod: DecorateMethod): DeriveSection { - const result = {} as DeriveSection; - 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 (allSections: AllSections, decorateMethod: DecorateMethod): DeriveAllSections { - const result = {} as DeriveAllSections; - const names = keys(allSections); +export function decorateDeriveSections (decorateMethod: DecorateMethod, derives: AnyDerive): DeriveAllSections { + 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; }