Add api.registerTypes (#670)
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
import { ProviderInterface } from '@polkadot/rpc-provider/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 {
|
||||
ApiBaseInterface, ApiInterface$Rx, ApiInterface$Events, ApiOptions, ApiType,
|
||||
@@ -110,8 +110,8 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
|
||||
this._rx.signer = options.signer;
|
||||
|
||||
// we only re-register the types (global) if this is not a cloned instance
|
||||
if (!options.source && options.types) {
|
||||
registry.register(options.types);
|
||||
if (!options.source) {
|
||||
this.registerTypes(options.types);
|
||||
}
|
||||
|
||||
this.init();
|
||||
@@ -298,6 +298,15 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
|
||||
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;
|
||||
|
||||
private emit (type: ApiInterface$Events, ...args: Array<any>): void {
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
import { Observable } from 'rxjs';
|
||||
import { ProviderInterface } from '@polkadot/rpc-provider/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 { CodecArg, CodecCallback, Constructor } from '@polkadot/types/types';
|
||||
import { CodecArg, CodecCallback, RegistryTypes } from '@polkadot/types/types';
|
||||
import { MethodFunction } from '@polkadot/types/Method';
|
||||
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`
|
||||
*/
|
||||
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)
|
||||
*/
|
||||
@@ -109,11 +113,7 @@ export interface ApiOptions {
|
||||
* @description Additional types used by runtime modules. This is nessusary if the runtime modules
|
||||
* uses types not available in the base Substrate runtime.
|
||||
*/
|
||||
types?: { [name: string]: Constructor };
|
||||
/**
|
||||
* @description An external signer which will be used to sign extrinsic when account passed in is not KeyringPair
|
||||
*/
|
||||
signer?: Signer;
|
||||
types?: RegistryTypes;
|
||||
}
|
||||
|
||||
export interface ApiInterface$Decorated<CodecResult, SubscriptionResult> {
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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 }
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user