Explicit class cache clear (#4523)

* Explicit class cache clear

* Re-register defaults

* Track knownDefaults in getters

* get ordering
This commit is contained in:
Jaco
2022-02-03 11:14:43 +02:00
committed by GitHub
parent 75a4e7b5d7
commit 37cf4b5e88
3 changed files with 16 additions and 31 deletions
+13 -6
View File
@@ -154,7 +154,7 @@ export class TypeRegistry implements Registry {
#hasher: (data: Uint8Array) => Uint8Array = blake2AsU8a;
readonly #knownDefaults: RegistryTypes;
readonly #knownDefaults: Record<string, CodecClass>;
readonly #knownDefinitions: Record<string, Definitions>;
@@ -170,9 +170,6 @@ export class TypeRegistry implements Registry {
this.#knownDefaults = objectSpread({ Json, Metadata, PortableRegistry, Raw }, baseTypes);
this.#knownDefinitions = definitions;
// register know, first classes then on-demand-created definitions
this.register(this.#knownDefaults);
const allKnown = Object.values(this.#knownDefinitions);
for (let i = 0; i < allKnown.length; i++) {
@@ -250,6 +247,10 @@ export class TypeRegistry implements Registry {
return this.#signedExtensions;
}
public clearCache (): void {
this.#classes = new Map();
}
/**
* @describe Creates an instance of the class
*/
@@ -314,7 +315,7 @@ export class TypeRegistry implements Registry {
}
public getUnsafe <T extends Codec = Codec, K extends string = string> (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): CodecClass<T> | undefined {
let Type = this.#classes.get(name);
let Type = this.#classes.get(name) || this.#knownDefaults[name];
// we have not already created the type, attempt it
if (!Type) {
@@ -357,6 +358,12 @@ export class TypeRegistry implements Registry {
// (previously this used to be a simple find & return)
const names: string[] = [];
for (const [name, Clazz] of Object.entries(this.#knownDefaults)) {
if (Type === Clazz) {
names.push(name);
}
}
for (const [name, Clazz] of this.#classes.entries()) {
if (Type === Clazz) {
names.push(name);
@@ -400,7 +407,7 @@ export class TypeRegistry implements Registry {
}
public hasClass (name: string): boolean {
return this.#classes.has(name);
return this.#classes.has(name) || !!this.#knownDefaults[name];
}
public hasDef (name: string): boolean {
+2 -25
View File
@@ -21,24 +21,16 @@ declare module '@polkadot/types-codec/types/registry' {
}
export interface Registry {
// readonly chainDecimals: number[];
// readonly chainSS58: number | undefined;
// readonly chainTokens: string[];
readonly knownTypes: RegisteredTypes;
// readonly lookup: PortableRegistry;
readonly metadata: MetadataLatest;
readonly unknownTypes: string[];
readonly signedExtensions: string[];
// createdAtHash?: Hash;
findMetaCall (callIndex: Uint8Array): CallFunctionExt;
findMetaError (errorIndex: Uint8Array | { error: BN, index: BN }): RegistryError;
// due to same circular imports where types don't really want to import from EventData,
// keep this as a generic Codec, however the actual impl. returns the correct
// findMetaEvent (eventIndex: Uint8Array): CodecClass<any>;
// isLookupType (value: string): boolean;
clearCache (): void
createLookupType (lookupId: SiLookupTypeId | number): string;
createClass <T extends Codec = Codec, K extends string = string> (type: K): CodecClass<DetectCodec<T, K>>;
@@ -46,25 +38,10 @@ declare module '@polkadot/types-codec/types/registry' {
get <T extends Codec = Codec, K extends string = string> (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): CodecClass<DetectCodec<T, K>> | undefined;
getChainProperties (): ChainProperties | undefined;
// getClassName (clazz: Constructor): string | undefined;
getDefinition (typeName: string): string | undefined;
getModuleInstances (specName: string, moduleName: string): string[] | undefined;
// getOrThrow <T extends Codec = Codec, K extends string = string> (name: K, msg?: string): CodecClass<DetectCodec<T, K>>;
// getOrUnknown <T extends Codec = Codec, K extends string = string> (name: K): CodecClass<DetectCodec<T, K>>;
setKnownTypes (types: RegisteredTypes): void;
// getSignedExtensionExtra (): Record<string, string>;
// getSignedExtensionTypes (): Record<string, string>;
// hasClass (name: string): boolean;
// hasDef (name: string): boolean;
// hasType (name: string): boolean;
// hash (data: Uint8Array): IU8a;
// register (type: CodecClass | RegistryTypes): void;
// register (name: string, type: CodecClass): void;
// register (arg1: string | CodecClass | RegistryTypes, arg2?: CodecClass): void;
setChainProperties (properties?: ChainProperties): void;
setHasher (hasher?: CodecHasher | null): void;
setLookup (lookup: PortableRegistry): void;