Add api.registerTypes (#670)

This commit is contained in:
Jaco Greeff
2019-02-09 08:02:47 +02:00
committed by GitHub
parent 5f4160ab5b
commit 6d759cd234
4 changed files with 27 additions and 18 deletions
+4 -8
View File
@@ -2,24 +2,20 @@
// 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 { Constructor, RegistryTypes } from '../types';
import { isFunction, isString } from '@polkadot/util';
import { createClass } from './createType';
type ConstructorObj = {
[name: string]: Constructor | string | { [name: string]: string }
};
export class TypeRegistry {
static readonly defaultRegistry: TypeRegistry = new TypeRegistry();
private _registry: Map<string, Constructor> = new Map();
register (type: Constructor | ConstructorObj): void;
register (type: Constructor | RegistryTypes): void;
register (name: string, type: Constructor): void;
register (arg1: string | Constructor | ConstructorObj, arg2?: Constructor): void {
register (arg1: string | Constructor | RegistryTypes, arg2?: Constructor): void {
if (isString(arg1)) {
const name = arg1;
const type = arg2!;
@@ -35,7 +31,7 @@ export class TypeRegistry {
}
}
private registerObject (obj: ConstructorObj) {
private registerObject (obj: RegistryTypes) {
Object.entries(obj).forEach(([name, type]) => {
if (isString(type)) {
this._registry.set(name, createClass(type));
+4
View File
@@ -67,3 +67,7 @@ export type Constructor<T = Codec> = { new(value?: any): T };
export type ConstructorDef<T = Codec> = { [index: string]: Constructor<T> };
export type TypeDef = { [index: string]: Codec };
export type RegistryTypes = {
[name: string]: Constructor | string | { [name: string]: string }
};