From 35663319d927ae870f358e9c47882dcca2d41997 Mon Sep 17 00:00:00 2001 From: Jaco Date: Tue, 2 Nov 2021 12:21:16 +0100 Subject: [PATCH] Lazy decoration (part 5, augmentObject) (#4145) * Lazy decoration (part 5, augmentObject) * hasOwnProperty update * Fix comments * loop indexes * Extract lazySection * Use hasOwnProperty on is/as getters * defineProperty helper * Update defineProperty & use for loops * skip/adjust for some keys --- packages/api/src/util/augmentObject.ts | 73 +++++++++++-------- packages/types/src/codec/Enum.ts | 32 ++++---- packages/types/src/codec/Json.ts | 16 ++-- packages/types/src/codec/Set.ts | 16 ++-- packages/types/src/codec/Struct.ts | 16 ++-- .../types/src/codec/utils/defineProperty.ts | 18 +++++ packages/types/src/codec/utils/index.ts | 1 + packages/types/src/generic/Vote.spec.ts | 4 +- 8 files changed, 100 insertions(+), 76 deletions(-) create mode 100644 packages/types/src/codec/utils/defineProperty.ts diff --git a/packages/api/src/util/augmentObject.ts b/packages/api/src/util/augmentObject.ts index 01e12b2a9b..71f332e2fb 100644 --- a/packages/api/src/util/augmentObject.ts +++ b/packages/api/src/util/augmentObject.ts @@ -1,12 +1,21 @@ // Copyright 2017-2021 @polkadot/api authors & contributors // SPDX-License-Identifier: Apache-2.0 +import { lazyMethod } from '@polkadot/types'; import { logger } from '@polkadot/util'; type StringsStrings = [string[], string[]]; const l = logger('api/augment'); +function clearObject (obj: Record): void { + const keys = Object.keys(obj); + + for (let i = 0; i < keys.length; i++) { + delete obj[keys[i]]; + } +} + function logLength (type: 'added' | 'removed', values: string[], and: string[] = []): string { return values.length ? ` ${values.length} ${type}${and.length ? ' and' : ''}` @@ -34,7 +43,7 @@ function findSectionIncludes (a: string[], b: string[]): string[] { return a.filter((s) => b.includes(s)); } -function extractSections (src: Record>, dst: Record>): StringsStrings { +function extractSections (src: Record>, dst: Record>): StringsStrings { const srcSections = Object.keys(src); const dstSections = Object.keys(dst); @@ -44,7 +53,7 @@ function extractSections (src: Record>, dst: Record< ]; } -function findMethodExcludes (src: Record>, dst: Record>): string[] { +function findMethodExcludes (src: Record>, dst: Record>): string[] { const srcSections = Object.keys(src); const dstSections = findSectionIncludes(Object.keys(dst), srcSections); const excludes: string[] = []; @@ -64,27 +73,48 @@ function findMethodExcludes (src: Record>, dst: Reco return excludes; } -function extractMethods (src: Record>, dst: Record>): StringsStrings { +function extractMethods (src: Record>, dst: Record>): StringsStrings { return [ findMethodExcludes(dst, src), findMethodExcludes(src, dst) ]; } +function lazySection (section: string, src: Record>, dst: Record>): void { + // NOTE: Unlike the other lazy decorations, here we actually do decorate the + // sections, just the internal form is not available (i.e. we skip lazyMethods) + // The initial issue is that we have a confict between "this already exists" and + // "this needs to start clean", so for a first round we skip... + if (!dst[section]) { + dst[section] = {}; + } + + const creator = (method: string) => src[section][method]; + const methods = Object.keys(src[section]); + + for (let i = 0; i < methods.length; i++) { + const method = methods[i]; + + // We use hasOwnproperty here to only check for the existence of the key, + // instead of reading dst[section][method] which will evaluate when already + // set as a lazy value previously + if (!Object.prototype.hasOwnProperty.call(dst[section], method)) { + lazyMethod(dst[section], method, creator); + } + } +} + /** - * Takes a decorated api section (e.g. api.tx) and augment it with the details. It does not override what is - * already available, but rather just adds new missing ites into the result object. + * @description Takes a decorated api section (e.g. api.tx) and augment it with the details. It does not override what is + * already available, but rather just adds new missing items into the result object. * @internal */ export function augmentObject (prefix: string | null, src: Record>, dst: Record>, fromEmpty = false): Record> { - if (fromEmpty) { - const dstKeys = Object.keys(dst); - - for (let k = 0; k < dstKeys.length; k++) { - delete dst[dstKeys[k]]; - } - } + fromEmpty && clearObject(dst); + // NOTE: This part is slightly problematic since it will get the + // values for at least all the sections and the names of the methods + // (Since methods won't be decorated before lazy, this _may_ be ok) if (prefix && Object.keys(dst).length) { warn(prefix, 'modules', extractSections(src, dst)); warn(prefix, 'calls', extractMethods(src, dst)); @@ -92,23 +122,8 @@ export function augmentObject (prefix: string | null, src: Record { @@ -202,27 +202,21 @@ export class Enum implements IEnum { constructor (registry: Registry, value?: unknown, index?: number) { super(registry, Types, value, index); - Object.keys(this.#def).forEach((_key): void => { - const name = stringUpperFirst(stringCamelCase(_key.replace(' ', '_'))); + const keys = Object.keys(this.#def); + + for (let i = 0; i < keys.length; i++) { + const key = keys[i]; + const name = stringUpperFirst(stringCamelCase(key.replace(' ', '_'))); const askey = `as${name}`; const iskey = `is${name}`; - isUndefined(this[iskey as keyof this]) && - Object.defineProperty(this, iskey, { - enumerable: true, - get: () => this.type === _key - }); + defineProperty(this, iskey, () => this.type === key); + defineProperty(this, askey, (): Codec => { + assert(this[iskey as keyof this], () => `Cannot convert '${this.type}' via ${askey}`); - isUndefined(this[askey as keyof this]) && - Object.defineProperty(this, askey, { - enumerable: true, - get: (): Codec => { - assert(this[iskey as keyof this], () => `Cannot convert '${this.type}' via ${askey}`); - - return this.value; - } - }); - }); + return this.value; + }); + } } }; } @@ -281,7 +275,7 @@ export class Enum implements IEnum { * @deprecated use isNone */ public get isNull (): boolean { - return this.isNone; + return this.#raw instanceof Null; } /** diff --git a/packages/types/src/codec/Json.ts b/packages/types/src/codec/Json.ts index a936599c25..a13d147707 100644 --- a/packages/types/src/codec/Json.ts +++ b/packages/types/src/codec/Json.ts @@ -5,9 +5,9 @@ import type { HexString } from '@polkadot/util/types'; import type { CodecHash, Hash } from '../interfaces/runtime'; import type { AnyJson, Codec, Registry } from '../types'; -import { isFunction, isUndefined, stringify } from '@polkadot/util'; +import { isFunction, stringify } from '@polkadot/util'; -import { compareMap } from './utils'; +import { compareMap, defineProperty } from './utils'; /** @internal */ function decodeJson (value?: Record | null): [string, any][] { @@ -34,13 +34,11 @@ export class Json extends Map implements Codec { this.registry = registry; - decoded.forEach(([key]): void => { - isUndefined(this[key as keyof this]) && - Object.defineProperty(this, key, { - enumerable: true, - get: (): Codec | undefined => this.get(key) as Codec - }); - }); + for (let i = 0; i < decoded.length; i++) { + const [key] = decoded[i]; + + defineProperty(this, key, () => this.get(key) as Codec); + } } /** diff --git a/packages/types/src/codec/Set.ts b/packages/types/src/codec/Set.ts index 79e566cd8c..4da4e37f2a 100644 --- a/packages/types/src/codec/Set.ts +++ b/packages/types/src/codec/Set.ts @@ -7,7 +7,7 @@ import type { Constructor, ISet, Registry } from '../types'; import { assert, BN, bnToBn, bnToU8a, isBn, isNumber, isString, isU8a, isUndefined, stringCamelCase, stringify, stringUpperFirst, u8aToBn, u8aToHex, u8aToU8a } from '@polkadot/util'; -import { compareArray } from './utils'; +import { compareArray, defineProperty } from './utils'; type SetValues = Record; @@ -108,15 +108,13 @@ export class CodecSet extends Set implements ISet { constructor (registry: Registry, value?: unknown) { super(registry, values, value as undefined, bitLength); - Object.keys(values).forEach((_key): void => { - const iskey = `is${stringUpperFirst(stringCamelCase(_key))}`; + const keys = Object.keys(values); - isUndefined(this[iskey as keyof this]) && - Object.defineProperty(this, iskey, { - enumerable: true, - get: () => this.strings.includes(_key) - }); - }); + for (let i = 0; i < keys.length; i++) { + const key = keys[i]; + + defineProperty(this, `is${stringUpperFirst(stringCamelCase(key))}`, () => this.strings.includes(key)); + } } }; } diff --git a/packages/types/src/codec/Struct.ts b/packages/types/src/codec/Struct.ts index 27fbb95ddb..f65adca624 100644 --- a/packages/types/src/codec/Struct.ts +++ b/packages/types/src/codec/Struct.ts @@ -7,7 +7,7 @@ import type { AnyJson, BareOpts, Codec, Constructor, ConstructorDef, IStruct, Re import { assert, hexToU8a, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, stringCamelCase, stringify, u8aConcat, u8aToHex } from '@polkadot/util'; -import { compareMap, decodeU8a, mapToTypeMap } from './utils'; +import { compareMap, decodeU8a, defineProperty, mapToTypeMap } from './utils'; type TypesDef = Record>; @@ -147,13 +147,13 @@ export class Struct< constructor (registry: Registry, value?: unknown) { super(registry, Types, value as HexString, jsonMap); - Object.keys(Types).forEach((key): void => { - isUndefined(this[key as keyof this]) && - Object.defineProperty(this, key, { - enumerable: true, - get: (): Codec | undefined => this.get(key as keyof S) - }); - }); + const keys = Object.keys(Types); + + for (let i = 0; i < keys.length; i++) { + const key = keys[i]; + + defineProperty(this, key, () => this.get(key as keyof S)); + } } }; } diff --git a/packages/types/src/codec/utils/defineProperty.ts b/packages/types/src/codec/utils/defineProperty.ts new file mode 100644 index 0000000000..e5e7df0d4a --- /dev/null +++ b/packages/types/src/codec/utils/defineProperty.ts @@ -0,0 +1,18 @@ +// Copyright 2017-2021 @polkadot/types authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { isUndefined } from '@polkadot/util'; + +const SKIP_KEYS = ['hash']; + +export function defineProperty (that: object, key: string, get: () => unknown): void { + if (SKIP_KEYS.includes(key)) { + return defineProperty(that, `_${key}`, get); + } + + // We use an isUndefined check here, not hasOwwnProperty since there are set in derived + // classes (inside with) and _Own_ properties refers to the class only, not parents + if (isUndefined((that as Record)[key])) { + Object.defineProperty(that, key, { enumerable: true, get }); + } +} diff --git a/packages/types/src/codec/utils/index.ts b/packages/types/src/codec/utils/index.ts index 9a7e983c79..cd003ba209 100644 --- a/packages/types/src/codec/utils/index.ts +++ b/packages/types/src/codec/utils/index.ts @@ -5,6 +5,7 @@ export { compareArray } from './compareArray'; export { compareMap } from './compareMap'; export { compareSet } from './compareSet'; export { decodeU8a } from './decodeU8a'; +export { defineProperty } from './defineProperty'; export { mapToTypeMap } from './mapToTypeMap'; export { typeToConstructor } from './typeToConstructor'; export { sortAsc, sortSet, sortMap } from './sortValues'; diff --git a/packages/types/src/generic/Vote.spec.ts b/packages/types/src/generic/Vote.spec.ts index c22aadbb2a..b46a274607 100644 --- a/packages/types/src/generic/Vote.spec.ts +++ b/packages/types/src/generic/Vote.spec.ts @@ -7,7 +7,7 @@ import { GenericVote } from '.'; describe('GenericVote', (): void => { const registry = new TypeRegistry(); - describe('construction', (): void => { + describe.only('construction', (): void => { it('constructs via boolean true', (): void => { expect(new GenericVote(registry, true).toU8a()).toEqual(new Uint8Array([128])); expect(new GenericVote(registry, true).isAye).toBe(true); @@ -58,7 +58,7 @@ describe('GenericVote', (): void => { expect(new GenericVote(registry, new Uint8Array([1])).isNay).toBe(true); }); - it('constructs via Uint8Array (aye)', (): void => { + it.only('constructs via Uint8Array (aye)', (): void => { const test = new GenericVote(registry, new Uint8Array([0b10000010])); expect(test.isNay).toBe(false);