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
+12 -3
View File
@@ -4,7 +4,7 @@
import { ProviderInterface } from '@polkadot/rpc-provider/types'; import { ProviderInterface } from '@polkadot/rpc-provider/types';
import { Storage } from '@polkadot/storage/types'; import { Storage } from '@polkadot/storage/types';
import { Codec, CodecArg, CodecCallback } from '@polkadot/types/types'; import { Codec, CodecArg, CodecCallback, RegistryTypes } from '@polkadot/types/types';
import { RxResult } from './rx/types'; import { RxResult } from './rx/types';
import { import {
ApiBaseInterface, ApiInterface$Rx, ApiInterface$Events, ApiOptions, ApiType, ApiBaseInterface, ApiInterface$Rx, ApiInterface$Events, ApiOptions, ApiType,
@@ -110,8 +110,8 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
this._rx.signer = options.signer; this._rx.signer = options.signer;
// we only re-register the types (global) if this is not a cloned instance // we only re-register the types (global) if this is not a cloned instance
if (!options.source && options.types) { if (!options.source) {
registry.register(options.types); this.registerTypes(options.types);
} }
this.init(); this.init();
@@ -298,6 +298,15 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
return this; return this;
} }
/**
* @description Register additional user-defined of chain-specific types in the type registry
*/
registerTypes (types?: RegistryTypes): void {
if (types) {
registry.register(types);
}
}
protected abstract onCall (method: OnCallFunction<RxResult, RxResult>, params?: Array<CodecArg>, callback?: CodecCallback, needsCallback?: boolean): CodecResult | SubscriptionResult; protected abstract onCall (method: OnCallFunction<RxResult, RxResult>, params?: Array<CodecArg>, callback?: CodecCallback, needsCallback?: boolean): CodecResult | SubscriptionResult;
private emit (type: ApiInterface$Events, ...args: Array<any>): void { private emit (type: ApiInterface$Events, ...args: Array<any>): void {
+7 -7
View File
@@ -5,9 +5,9 @@
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { ProviderInterface } from '@polkadot/rpc-provider/types'; import { ProviderInterface } from '@polkadot/rpc-provider/types';
import { RpcRxInterface$Events } from '@polkadot/rpc-rx/types'; import { RpcRxInterface$Events } from '@polkadot/rpc-rx/types';
import { Hash, Metadata, RuntimeVersion, u64 as U64, Extrinsic } from '@polkadot/types/index'; import { Extrinsic, Hash, Metadata, RuntimeVersion, u64 as U64 } from '@polkadot/types/index';
import { SignatureOptions } from '@polkadot/types/ExtrinsicSignature'; import { SignatureOptions } from '@polkadot/types/ExtrinsicSignature';
import { CodecArg, CodecCallback, Constructor } from '@polkadot/types/types'; import { CodecArg, CodecCallback, RegistryTypes } from '@polkadot/types/types';
import { MethodFunction } from '@polkadot/types/Method'; import { MethodFunction } from '@polkadot/types/Method';
import { StorageFunction } from '@polkadot/types/StorageKey'; import { StorageFunction } from '@polkadot/types/StorageKey';
@@ -101,6 +101,10 @@ export interface ApiOptions {
* connecting to a WsProvider connecting localhost with the default port, i.e. `ws://127.0.0.1:9944` * connecting to a WsProvider connecting localhost with the default port, i.e. `ws://127.0.0.1:9944`
*/ */
provider?: ProviderInterface; provider?: ProviderInterface;
/**
* @description An external signer which will be used to sign extrinsic when account passed in is not KeyringPair
*/
signer?: Signer;
/** /**
* @description The source object to use for runtime information (only used when cloning) * @description The source object to use for runtime information (only used when cloning)
*/ */
@@ -109,11 +113,7 @@ export interface ApiOptions {
* @description Additional types used by runtime modules. This is nessusary if the runtime modules * @description Additional types used by runtime modules. This is nessusary if the runtime modules
* uses types not available in the base Substrate runtime. * uses types not available in the base Substrate runtime.
*/ */
types?: { [name: string]: Constructor }; types?: RegistryTypes;
/**
* @description An external signer which will be used to sign extrinsic when account passed in is not KeyringPair
*/
signer?: Signer;
} }
export interface ApiInterface$Decorated<CodecResult, SubscriptionResult> { export interface ApiInterface$Decorated<CodecResult, SubscriptionResult> {
+4 -8
View File
@@ -2,24 +2,20 @@
// This software may be modified and distributed under the terms // This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details. // 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 { isFunction, isString } from '@polkadot/util';
import { createClass } from './createType'; import { createClass } from './createType';
type ConstructorObj = {
[name: string]: Constructor | string | { [name: string]: string }
};
export class TypeRegistry { export class TypeRegistry {
static readonly defaultRegistry: TypeRegistry = new TypeRegistry(); static readonly defaultRegistry: TypeRegistry = new TypeRegistry();
private _registry: Map<string, Constructor> = new Map(); private _registry: Map<string, Constructor> = new Map();
register (type: Constructor | ConstructorObj): void; register (type: Constructor | RegistryTypes): void;
register (name: string, type: Constructor): 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)) { if (isString(arg1)) {
const name = arg1; const name = arg1;
const type = arg2!; const type = arg2!;
@@ -35,7 +31,7 @@ export class TypeRegistry {
} }
} }
private registerObject (obj: ConstructorObj) { private registerObject (obj: RegistryTypes) {
Object.entries(obj).forEach(([name, type]) => { Object.entries(obj).forEach(([name, type]) => {
if (isString(type)) { if (isString(type)) {
this._registry.set(name, createClass(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 ConstructorDef<T = Codec> = { [index: string]: Constructor<T> };
export type TypeDef = { [index: string]: Codec }; export type TypeDef = { [index: string]: Codec };
export type RegistryTypes = {
[name: string]: Constructor | string | { [name: string]: string }
};