Caching for previously-created complex classes (#4177)

* Caching for previousl-created complex classes

* Drop non-output testing test
This commit is contained in:
Jaco
2021-11-06 12:17:07 +02:00
committed by GitHub
parent 0a6f1d87d4
commit 0ee1dfb353
3 changed files with 14 additions and 16 deletions
+7 -9
View File
@@ -175,16 +175,9 @@ const infoMapping: Record<TypeDefInfo, (registry: Registry, value: TypeDef) => C
WrapperOpaque.with(getSubType(value))
};
// Returns the type Class for construction
export function getTypeClass<T extends Codec = Codec> (registry: Registry, typeDef: TypeDef): Constructor<T> {
let Type = registry.get(typeDef.type);
if (Type) {
return Type as Constructor<T>;
}
export function constructTypeClass<T extends Codec = Codec> (registry: Registry, typeDef: TypeDef): Constructor<T> {
try {
Type = infoMapping[typeDef.info](registry, typeDef);
const Type = infoMapping[typeDef.info](registry, typeDef);
assert(Type, 'No class created');
@@ -201,6 +194,11 @@ export function getTypeClass<T extends Codec = Codec> (registry: Registry, typeD
}
}
// Returns the type Class for construction
export function getTypeClass<T extends Codec = Codec> (registry: Registry, typeDef: TypeDef): Constructor<T> {
return registry.get(typeDef.type, false, typeDef) as Constructor<T>;
}
export function createClass<T extends Codec = Codec, K extends string = string> (registry: Registry, type: K): DetectConstructor<T, K> {
return getTypeClass(
registry,
+5 -5
View File
@@ -4,7 +4,7 @@
import type { ExtDef } from '../extrinsic/signedExtensions/types';
import type { ChainProperties, CodecHash, DispatchErrorModule, Hash, MetadataLatest, SiField, SiLookupTypeId, SiVariant } from '../interfaces/types';
import type { CallFunction, Codec, CodecHasher, Constructor, DetectCodec, DetectConstructor, RegisteredTypes, Registry, RegistryError, RegistryTypes } from '../types';
import type { CreateOptions } from './types';
import type { CreateOptions, TypeDef } from './types';
import { assert, assertReturn, BN_ZERO, formatBalance, isFunction, isString, isU8a, lazyMethod, logger, objectSpread, stringCamelCase, stringify } from '@polkadot/util';
import { blake2AsU8a } from '@polkadot/util-crypto';
@@ -20,7 +20,7 @@ import { decorateConstants, filterCallsSome, filterEventsSome } from '../metadat
import { createCallFunction } from '../metadata/decorate/extrinsics';
import { Metadata } from '../metadata/Metadata';
import { PortableRegistry } from '../metadata/PortableRegistry';
import { createClass } from './createClass';
import { constructTypeClass, createClass } from './createClass';
import { createTypeUnsafe } from './createType';
import { lazyVariants } from './lazy';
@@ -316,7 +316,7 @@ export class TypeRegistry implements Registry {
);
}
public get <T extends Codec = Codec, K extends string = string> (name: K, withUnknown?: boolean): DetectConstructor<T, K> | undefined {
public get <T extends Codec = Codec, K extends string = string> (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): DetectConstructor<T, K> | undefined {
let Type = this.#classes.get(name);
// we have not already created the type, attempt it
@@ -327,8 +327,8 @@ export class TypeRegistry implements Registry {
// we have a definition, so create the class now (lazily)
if (definition) {
BaseType = createClass(this, definition);
this.#classes.set(name, BaseType);
} else if (knownTypeDef) {
BaseType = constructTypeClass(this, knownTypeDef);
} else if (withUnknown) {
l.warn(`Unable to resolve type ${name}, it will fail on construction`);
+2 -2
View File
@@ -3,7 +3,7 @@
import type { Observable } from 'rxjs';
import type { BN } from '@polkadot/util';
import type { CreateOptions } from '../create/types';
import type { CreateOptions, TypeDef } from '../create/types';
import type { ExtDef } from '../extrinsic/signedExtensions/types';
import type { MetadataLatest } from '../interfaces/metadata';
import type { CodecHash, Hash } from '../interfaces/runtime';
@@ -126,7 +126,7 @@ export interface Registry {
createClass <T extends Codec = Codec, K extends string = string> (type: K): DetectConstructor<T, K>;
createType <T extends Codec = Codec, K extends string = string> (type: K, ...params: unknown[]): DetectCodec<T, K>;
createTypeUnsafe <T extends Codec = Codec, K extends string = string> (type: K, params: unknown[], options?: CreateOptions): DetectCodec<T, K>;
get <T extends Codec = Codec, K extends string = string> (name: K, withUnknown?: boolean): DetectConstructor<T, K> | undefined;
get <T extends Codec = Codec, K extends string = string> (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): DetectConstructor<T, K> | undefined;
getChainProperties (): ChainProperties | undefined;
getClassName (clazz: Constructor): string | undefined;
getDefinition (typeName: string): string | undefined;