Skip to content

Commit 37cf4b5

Browse files
authored
Explicit class cache clear (#4523)
* Explicit class cache clear * Re-register defaults * Track knownDefaults in getters * get ordering
1 parent 75a4e7b commit 37cf4b5

File tree

3 files changed

+16
-31
lines changed

3 files changed

+16
-31
lines changed

packages/api/src/base/Init.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export abstract class Init<ApiType extends ApiTypes> extends Decorate<ApiType> {
8585
* @description Decorates a registry based on the runtime version
8686
*/
8787
private _initRegistry (registry: Registry, chain: Text, version: { specName: Text, specVersion: BN }, metadata: Metadata, chainProps?: ChainProperties): void {
88+
registry.clearCache();
8889
registry.setChainProperties(chainProps || this.registry.getChainProperties());
8990
registry.setKnownTypes(this._options);
9091
registry.register(getSpecTypes(registry, chain, version.specName, version.specVersion));

packages/types/src/create/registry.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export class TypeRegistry implements Registry {
154154

155155
#hasher: (data: Uint8Array) => Uint8Array = blake2AsU8a;
156156

157-
readonly #knownDefaults: RegistryTypes;
157+
readonly #knownDefaults: Record<string, CodecClass>;
158158

159159
readonly #knownDefinitions: Record<string, Definitions>;
160160

@@ -170,9 +170,6 @@ export class TypeRegistry implements Registry {
170170
this.#knownDefaults = objectSpread({ Json, Metadata, PortableRegistry, Raw }, baseTypes);
171171
this.#knownDefinitions = definitions;
172172

173-
// register know, first classes then on-demand-created definitions
174-
this.register(this.#knownDefaults);
175-
176173
const allKnown = Object.values(this.#knownDefinitions);
177174

178175
for (let i = 0; i < allKnown.length; i++) {
@@ -250,6 +247,10 @@ export class TypeRegistry implements Registry {
250247
return this.#signedExtensions;
251248
}
252249

250+
public clearCache (): void {
251+
this.#classes = new Map();
252+
}
253+
253254
/**
254255
* @describe Creates an instance of the class
255256
*/
@@ -314,7 +315,7 @@ export class TypeRegistry implements Registry {
314315
}
315316

316317
public getUnsafe <T extends Codec = Codec, K extends string = string> (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): CodecClass<T> | undefined {
317-
let Type = this.#classes.get(name);
318+
let Type = this.#classes.get(name) || this.#knownDefaults[name];
318319

319320
// we have not already created the type, attempt it
320321
if (!Type) {
@@ -357,6 +358,12 @@ export class TypeRegistry implements Registry {
357358
// (previously this used to be a simple find & return)
358359
const names: string[] = [];
359360

361+
for (const [name, Clazz] of Object.entries(this.#knownDefaults)) {
362+
if (Type === Clazz) {
363+
names.push(name);
364+
}
365+
}
366+
360367
for (const [name, Clazz] of this.#classes.entries()) {
361368
if (Type === Clazz) {
362369
names.push(name);
@@ -400,7 +407,7 @@ export class TypeRegistry implements Registry {
400407
}
401408

402409
public hasClass (name: string): boolean {
403-
return this.#classes.has(name);
410+
return this.#classes.has(name) || !!this.#knownDefaults[name];
404411
}
405412

406413
public hasDef (name: string): boolean {

packages/types/src/types/augmentRegistry.ts

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,50 +21,27 @@ declare module '@polkadot/types-codec/types/registry' {
2121
}
2222

2323
export interface Registry {
24-
// readonly chainDecimals: number[];
25-
// readonly chainSS58: number | undefined;
26-
// readonly chainTokens: string[];
2724
readonly knownTypes: RegisteredTypes;
28-
// readonly lookup: PortableRegistry;
2925
readonly metadata: MetadataLatest;
3026
readonly unknownTypes: string[];
3127
readonly signedExtensions: string[];
3228

33-
// createdAtHash?: Hash;
34-
3529
findMetaCall (callIndex: Uint8Array): CallFunctionExt;
3630
findMetaError (errorIndex: Uint8Array | { error: BN, index: BN }): RegistryError;
37-
// due to same circular imports where types don't really want to import from EventData,
38-
// keep this as a generic Codec, however the actual impl. returns the correct
39-
// findMetaEvent (eventIndex: Uint8Array): CodecClass<any>;
4031

41-
// isLookupType (value: string): boolean;
32+
clearCache (): void
33+
4234
createLookupType (lookupId: SiLookupTypeId | number): string;
4335

4436
createClass <T extends Codec = Codec, K extends string = string> (type: K): CodecClass<DetectCodec<T, K>>;
4537
createType <T extends Codec = Codec, K extends string = string> (type: K, ...params: unknown[]): DetectCodec<T, K>;
4638

4739
get <T extends Codec = Codec, K extends string = string> (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): CodecClass<DetectCodec<T, K>> | undefined;
4840
getChainProperties (): ChainProperties | undefined;
49-
// getClassName (clazz: Constructor): string | undefined;
5041
getDefinition (typeName: string): string | undefined;
5142
getModuleInstances (specName: string, moduleName: string): string[] | undefined;
5243

53-
// getOrThrow <T extends Codec = Codec, K extends string = string> (name: K, msg?: string): CodecClass<DetectCodec<T, K>>;
54-
// getOrUnknown <T extends Codec = Codec, K extends string = string> (name: K): CodecClass<DetectCodec<T, K>>;
55-
5644
setKnownTypes (types: RegisteredTypes): void;
57-
// getSignedExtensionExtra (): Record<string, string>;
58-
// getSignedExtensionTypes (): Record<string, string>;
59-
60-
// hasClass (name: string): boolean;
61-
// hasDef (name: string): boolean;
62-
// hasType (name: string): boolean;
63-
// hash (data: Uint8Array): IU8a;
64-
65-
// register (type: CodecClass | RegistryTypes): void;
66-
// register (name: string, type: CodecClass): void;
67-
// register (arg1: string | CodecClass | RegistryTypes, arg2?: CodecClass): void;
6845
setChainProperties (properties?: ChainProperties): void;
6946
setHasher (hasher?: CodecHasher | null): void;
7047
setLookup (lookup: PortableRegistry): void;

0 commit comments

Comments
 (0)