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
This commit is contained in:
@@ -9,7 +9,7 @@ import { assert, hexToU8a, isHex, isNumber, isObject, isString, isU8a, isUndefin
|
||||
|
||||
import { Null } from '../primitive/Null';
|
||||
import { Struct } from './Struct';
|
||||
import { mapToTypeMap } from './utils';
|
||||
import { defineProperty, mapToTypeMap } from './utils';
|
||||
|
||||
// export interface, this is used in Enum.with, so required as public by TS
|
||||
export interface EnumConstructor<T = Codec> {
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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<string, unknown> | null): [string, any][] {
|
||||
@@ -34,13 +34,11 @@ export class Json extends Map<string, any> 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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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<string, number | BN>;
|
||||
|
||||
@@ -108,15 +108,13 @@ export class CodecSet extends Set<string> implements ISet<string> {
|
||||
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));
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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<T = Codec> = Record<string, string | Constructor<T>>;
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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<string, unknown>)[key])) {
|
||||
Object.defineProperty(that, key, { enumerable: true, get });
|
||||
}
|
||||
}
|
||||
@@ -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';
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user