Lazy decoration (part 10, Import lazy* from @polkadot/util) (#4166)

* Impot lazy* from @polkadot/util

* Bumps
This commit is contained in:
Jaco
2021-11-04 14:51:06 +02:00
committed by GitHub
parent 4a33b18ac4
commit e918aa8b14
21 changed files with 120 additions and 172 deletions
+1 -50
View File
@@ -3,61 +3,12 @@
import type { PortableRegistry, SiLookupTypeId, SiVariant } from '../interfaces';
import { isUndefined } from '@polkadot/util';
import { lazyMethod } from '@polkadot/util';
interface TypeHolder {
type: SiLookupTypeId
}
type WithToString = { toString: () => string };
export function lazyMethod <T, K> (result: Record<string, T>, item: K, creator: (d: K) => T, getName?: (d: K) => string): void {
const name = getName
? getName(item)
: (item as WithToString).toString();
let value: T | undefined;
Object.defineProperty(result, name, {
// This allows for re-configuration with the embedded defineProperty below
// and ensures that on tested browsers and Node, it _will_ be redefined
// and thus short-circuited for future access
configurable: true,
enumerable: true,
// Use a function here, we don't want to capture the outer
// this (i.e. don't use arrow functions in this context)
get: function (): T {
// This check should _always_ be false and unneeded, since we override
// with a value below ... however we ensure we are quire vigilant against
// all environment failures, so we are rather be safe than sorry
if (isUndefined(value)) {
value = creator(item);
try {
// re-define the property as a value, next time around this
// getter will only return the computed value
Object.defineProperty(this, name, { value });
} catch {
// ignore any errors, since this _should_ not happen due to
// the "configurable" property above. But if it ever does
// from here-on we will be the cached value the next time
// around (with a very slight dip in performance)
}
}
return value;
}
});
}
export function lazyMethods <T, K> (result: Record<string, T>, items: K[], creator: (v: K) => T, getName?: (m: K) => string): Record<string, T> {
for (let i = 0; i < items.length; i++) {
lazyMethod(result, items[i], creator, getName);
}
return result;
}
export function lazyVariants <T> (lookup: PortableRegistry, { type }: TypeHolder, getName: (v: SiVariant) => string, creator: (v: SiVariant) => T): Record<string, T> {
const result: Record<string, T> = {};
const variants = lookup.getSiType(type).def.asVariant.variants;
+2 -2
View File
@@ -6,7 +6,7 @@ import type { ChainProperties, CodecHash, DispatchErrorModule, Hash, MetadataLat
import type { CallFunction, Codec, CodecHasher, Constructor, DetectCodec, DetectConstructor, RegisteredTypes, Registry, RegistryError, RegistryTypes } from '../types';
import type { CreateOptions } from './types';
import { assert, assertReturn, BN_ZERO, formatBalance, isFunction, isString, isU8a, logger, stringCamelCase, stringify } from '@polkadot/util';
import { assert, assertReturn, BN_ZERO, formatBalance, isFunction, isString, isU8a, lazyMethod, logger, stringCamelCase, stringify } from '@polkadot/util';
import { blake2AsU8a } from '@polkadot/util-crypto';
import { DoNotConstruct } from '../codec/DoNotConstruct';
@@ -21,7 +21,7 @@ import { createCallFunction } from '../metadata/decorate/extrinsics';
import { Metadata } from '../metadata/Metadata';
import { createClass } from './createClass';
import { createTypeUnsafe } from './createType';
import { lazyMethod, lazyVariants } from './lazy';
import { lazyVariants } from './lazy';
const l = logger('registry');
@@ -5,9 +5,8 @@ import type { MetadataLatest, PalletConstantMetadataLatest } from '../../../inte
import type { Registry } from '../../../types';
import type { ConstantCodec, Constants } from '../types';
import { hexToU8a, stringCamelCase } from '@polkadot/util';
import { hexToU8a, lazyMethod, lazyMethods, stringCamelCase } from '@polkadot/util';
import { lazyMethod, lazyMethods } from '../../../create/lazy';
import { objectNameToCamel } from '../util';
/** @internal */
@@ -6,9 +6,9 @@ import type { Text, u8 } from '../../../primitive';
import type { Registry } from '../../../types';
import type { Errors, IsError } from '../types';
import { stringCamelCase } from '@polkadot/util';
import { lazyMethod, stringCamelCase } from '@polkadot/util';
import { lazyMethod, lazyVariants } from '../../../create/lazy';
import { lazyVariants } from '../../../create/lazy';
import { objectNameToString } from '../util';
interface ItemMeta {
@@ -5,9 +5,9 @@ import type { MetadataLatest, PalletMetadataLatest, SiVariant } from '../../../i
import type { AnyTuple, IEvent, Registry } from '../../../types';
import type { Events, IsEvent } from '../types';
import { stringCamelCase } from '@polkadot/util';
import { lazyMethod, stringCamelCase } from '@polkadot/util';
import { lazyMethod, lazyVariants } from '../../../create/lazy';
import { lazyVariants } from '../../../create/lazy';
import { variantToMeta } from '../errors';
import { objectNameToString } from '../util';
@@ -5,9 +5,9 @@ import type { MetadataLatest, PalletMetadataLatest, PortableRegistry, SiVariant
import type { CallFunction, Registry } from '../../../types';
import type { Extrinsics } from '../types';
import { stringCamelCase } from '@polkadot/util';
import { lazyMethod, stringCamelCase } from '@polkadot/util';
import { lazyMethod, lazyVariants } from '../../../create/lazy';
import { lazyVariants } from '../../../create/lazy';
import { getSiName } from '../../util';
import { objectNameToCamel } from '../util';
import { createUnchecked } from './createUnchecked';
@@ -5,9 +5,8 @@ import type { MetadataLatest, StorageEntryMetadataLatest } from '../../../interf
import type { Registry } from '../../../types';
import type { Storage } from '../types';
import { stringCamelCase } from '@polkadot/util';
import { lazyMethod, lazyMethods, stringCamelCase } from '@polkadot/util';
import { lazyMethod, lazyMethods } from '../../../create/lazy';
import { objectNameFirstLower } from '../util';
import { createFunction, createKeyRaw } from './createFunction';
import { getStorage } from './getStorage';