API cleanups, circular deps (#433)
* Cleanups with @polkadot/util packages * Initi registry at types root * Revert test throw * Fallback to root types when registry lookup fails * Update StorageChangeSet.spec.ts * Update createType.ts * Linting update * Update createType.ts * Rename TypeRegistry.ts to typeRegistry.ts
This commit is contained in:
@@ -10,7 +10,7 @@ import Rpc from '@polkadot/rpc-core/index';
|
||||
import extrinsicsFromMeta from '@polkadot/extrinsics/fromMetadata';
|
||||
import { Storage } from '@polkadot/storage/types';
|
||||
import storageFromMeta from '@polkadot/storage/fromMetadata';
|
||||
import TypeRegistry from '@polkadot/types/codec/TypeRegistry';
|
||||
import registry from '@polkadot/types/codec/typeRegistry';
|
||||
import { Hash, Method, RuntimeVersion } from '@polkadot/types/index';
|
||||
import Event from '@polkadot/types/Event';
|
||||
import RuntimeMetadata from '@polkadot/types/Metadata';
|
||||
@@ -76,7 +76,7 @@ export default abstract class ApiBase<R, S, E> implements ApiBaseInterface<R, S,
|
||||
this._rpc = this.decorateRpc(this._rpcBase);
|
||||
|
||||
if (options.additionalTypes) {
|
||||
TypeRegistry.defaultRegistry.register(options.additionalTypes);
|
||||
registry.register(options.additionalTypes);
|
||||
}
|
||||
|
||||
this.init();
|
||||
|
||||
@@ -2,14 +2,18 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import Null from './Null';
|
||||
|
||||
/**
|
||||
* @name Origin
|
||||
* @description
|
||||
* Where Origin occurs, it should be ignored as an internal-only value, so it should
|
||||
* never actually be constructed
|
||||
*/
|
||||
export default class Origin {
|
||||
export default class Origin extends Null {
|
||||
constructor () {
|
||||
super();
|
||||
|
||||
throw new Error('Origin should not be constructed, it is only a placeholder for compatibility');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import TypeRegistry from './TypeRegistry';
|
||||
import { TypeRegistry } from './typeRegistry';
|
||||
import Text from '../Text';
|
||||
import U32 from '../U32';
|
||||
|
||||
|
||||
@@ -8,9 +8,9 @@ import { Codec, Constructor } from '../types';
|
||||
import Text from '../Text';
|
||||
import Compact from './Compact';
|
||||
import Tuple from './Tuple';
|
||||
import TypeRegistry from './TypeRegistry';
|
||||
import UInt from './UInt';
|
||||
import Vector from './Vector';
|
||||
import registry from './typeRegistry';
|
||||
|
||||
export enum TypeDefInfo {
|
||||
Compact,
|
||||
@@ -142,12 +142,13 @@ export function getTypeClass (value: TypeDef): Constructor {
|
||||
);
|
||||
}
|
||||
|
||||
const registry = TypeRegistry.defaultRegistry;
|
||||
const Type = registry.get(value.type);
|
||||
// NOTE We only load types via require - we have to avoid circular deps between type usage and creation
|
||||
const Types = require('../index');
|
||||
const Type = registry.get(value.type) || Types[value.type];
|
||||
|
||||
assert(Type, `Unable to determine type from '${value.type}'`);
|
||||
|
||||
return Type!;
|
||||
return Type;
|
||||
}
|
||||
|
||||
export default function createType (type: Text | string, value?: any): Codec {
|
||||
|
||||
+10
-11
@@ -2,10 +2,11 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Constructor } from '../types';
|
||||
import * as allTypes from '../index';
|
||||
import { isFunction, isString } from '@polkadot/util';
|
||||
|
||||
export default class TypeRegistry {
|
||||
import { Constructor } from '../types';
|
||||
|
||||
export class TypeRegistry {
|
||||
static readonly defaultRegistry: TypeRegistry = new TypeRegistry();
|
||||
|
||||
private _registry: Map<string, Constructor> = new Map();
|
||||
@@ -13,28 +14,26 @@ export default class TypeRegistry {
|
||||
register (type: Constructor | {[name: string]: Constructor}): void;
|
||||
register (name: string, type: Constructor): void;
|
||||
register (arg1: string | Constructor | {[name: string]: Constructor}, arg2?: Constructor): void {
|
||||
if (typeof arg1 === 'string') {
|
||||
if (isString(arg1)) {
|
||||
const name = arg1;
|
||||
const type = arg2!;
|
||||
|
||||
this._registry.set(name, type);
|
||||
} else if (typeof arg1 === 'function') {
|
||||
} else if (isFunction(arg1)) {
|
||||
const name = arg1.name;
|
||||
const type = arg1;
|
||||
|
||||
this._registry.set(name, type);
|
||||
} else {
|
||||
const dict = arg1;
|
||||
|
||||
for (const [name, type] of Object.entries(dict)) {
|
||||
Object.entries(arg1).forEach(([name, type]) => {
|
||||
this._registry.set(name, type);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
get (name: string) {
|
||||
get (name: string): Constructor | undefined {
|
||||
return this._registry.get(name);
|
||||
}
|
||||
}
|
||||
|
||||
TypeRegistry.defaultRegistry.register(allTypes as any);
|
||||
export default TypeRegistry.defaultRegistry;
|
||||
Reference in New Issue
Block a user