Cache getTypeClass (#1183)
* Put memoizee only in types * Add some tests * Add failing tests * Make instanceof work * Better tests
This commit is contained in:
@@ -28,7 +28,9 @@
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.5.5",
|
||||
"@polkadot/util": "^0.94.1",
|
||||
"@polkadot/util-crypto": "^0.94.1"
|
||||
"@polkadot/util-crypto": "^0.94.1",
|
||||
"@types/memoizee": "^0.4.2",
|
||||
"memoizee": "^0.4.14"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/keyring": "^0.94.1"
|
||||
|
||||
@@ -4,7 +4,10 @@
|
||||
|
||||
import { Codec, Constructor } from '../types';
|
||||
|
||||
import createType, { TypeDef, TypeDefInfo, getTypeClass, getTypeDef, typeSplit } from './createType';
|
||||
import createType, { TypeDef, TypeDefInfo, createClass, getTypeClass, getTypeDef, typeSplit, ClassOf } from './createType';
|
||||
import Balance from '../primitive/Balance';
|
||||
import Text from '../primitive/Text';
|
||||
import { injectDefinitions } from '../srml';
|
||||
|
||||
describe('typeSplit', (): void => {
|
||||
it('splits simple types into an array', (): void => {
|
||||
@@ -210,6 +213,33 @@ describe('getTypeClass', (): void => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('createClass', (): void => {
|
||||
beforeAll((): void => {
|
||||
injectDefinitions();
|
||||
});
|
||||
|
||||
it('should memoize from strings', (): void => {
|
||||
const a = createClass('BabeWeight');
|
||||
const b = createClass('BabeWeight');
|
||||
|
||||
expect(a).toBe(b);
|
||||
});
|
||||
|
||||
it('should memoize from Text', (): void => {
|
||||
const a = createClass(new Text('TrieId'));
|
||||
const b = createClass(new Text('TrieId'));
|
||||
|
||||
expect(a).toBe(b);
|
||||
});
|
||||
|
||||
it('should memoize from string/Text combo', (): void => {
|
||||
const a = createClass(new Text('AuthorityWeight'));
|
||||
const b = createClass('AuthorityWeight');
|
||||
|
||||
expect(a).toBe(b);
|
||||
});
|
||||
});
|
||||
|
||||
describe('createType', (): void => {
|
||||
it('allows creation of a Struct', (): void => {
|
||||
expect(
|
||||
@@ -262,4 +292,37 @@ describe('createType', (): void => {
|
||||
(): Codec => createType('Vec<(BlockNumber,EventIndex)>', base, true)
|
||||
).toThrow(/Input doesn't match output, received 0x, created 0x00/);
|
||||
});
|
||||
|
||||
describe('instanceof', (): void => {
|
||||
beforeAll((): void => {
|
||||
injectDefinitions();
|
||||
});
|
||||
|
||||
it('instanceof should work (primitive type)', (): void => {
|
||||
const value = createType('Balance', 1234);
|
||||
|
||||
expect(value instanceof Balance).toBe(true);
|
||||
});
|
||||
|
||||
it('instanceof should work (srml type)', (): void => {
|
||||
const value = createType('Gas', 1234);
|
||||
const Gas = ClassOf('Gas');
|
||||
|
||||
expect(value instanceof Gas).toBe(true);
|
||||
});
|
||||
|
||||
it('instanceof should work (complex type)', (): void => {
|
||||
const complexType = '{"balance":"Balance","account_id":"AccountId","log":"(u64, u32)","fromSrml":"Gas"}';
|
||||
|
||||
const value = createType(complexType, {
|
||||
balance: 123,
|
||||
accountId: '',
|
||||
log: [456, 789],
|
||||
fromSrml: 0
|
||||
});
|
||||
const ComplexType = createClass(complexType);
|
||||
|
||||
expect(value instanceof ComplexType).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,6 +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 memoizee from 'memoizee';
|
||||
import { assert } from '@polkadot/util';
|
||||
|
||||
import { Codec, Constructor } from '../types';
|
||||
@@ -174,11 +175,22 @@ export function getTypeDef (_type: Text | string, name?: string): TypeDef {
|
||||
return value;
|
||||
}
|
||||
|
||||
export function createClass <T extends Codec = Codec> (type: Text | string): Constructor<T> {
|
||||
// Memoized helper of the `createClass` function below
|
||||
const memoizedCreateClass = memoizee(<T extends Codec = Codec>(type: Text | string): Constructor<T> => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
return getTypeClass<T>(
|
||||
getTypeDef(type)
|
||||
);
|
||||
}, {
|
||||
length: 1,
|
||||
// Normalize args so that different args that should be cached
|
||||
// together are cached together.
|
||||
// E.g.: `createClass('abc') === createClass(new Text('abc'));`
|
||||
normalizer: JSON.stringify
|
||||
});
|
||||
|
||||
export function createClass<T extends Codec = Codec> (type: Text | string): Constructor<T> {
|
||||
return memoizedCreateClass(type);
|
||||
}
|
||||
|
||||
// create an array of constructors from the input
|
||||
@@ -192,7 +204,7 @@ export function getTypeClassMap (defs: TypeDef[]): { [index: string]: Constructo
|
||||
}
|
||||
|
||||
// Returns the type Class for construction
|
||||
export function getTypeClass <T extends Codec = Codec> (value: TypeDef, Fallback?: Constructor<T>): Constructor<T> {
|
||||
export function getTypeClass<T extends Codec = Codec> (value: TypeDef, Fallback?: Constructor<T>): Constructor<T> {
|
||||
const Type = getRegistry().get<T>(value.type);
|
||||
|
||||
if (Type) {
|
||||
@@ -251,7 +263,7 @@ export function getTypeClass <T extends Codec = Codec> (value: TypeDef, Fallback
|
||||
|
||||
return Linkage.withKey(
|
||||
getTypeClass<Codec>(value.sub as TypeDef)
|
||||
)as unknown as Constructor<T>;
|
||||
) as unknown as Constructor<T>;
|
||||
case TypeDefInfo.DoubleMap:
|
||||
assert(value.sub && !Array.isArray(value.sub), 'Expected subtype for DoubleMap');
|
||||
|
||||
@@ -268,11 +280,11 @@ export function getTypeClass <T extends Codec = Codec> (value: TypeDef, Fallback
|
||||
}
|
||||
|
||||
// alias for createClass
|
||||
export function ClassOf <T extends Codec = Codec> (name: string): Constructor<T> {
|
||||
export function ClassOf<T extends Codec = Codec> (name: string): Constructor<T> {
|
||||
return createClass<T>(name);
|
||||
}
|
||||
|
||||
function initType <T extends Codec = Codec> (Type: Constructor<T>, value?: any, isPedantic?: boolean): T {
|
||||
function initType<T extends Codec = Codec> (Type: Constructor<T>, value?: any, isPedantic?: boolean): T {
|
||||
try {
|
||||
const created = new Type(value);
|
||||
|
||||
@@ -303,7 +315,7 @@ function initType <T extends Codec = Codec> (Type: Constructor<T>, value?: any,
|
||||
}
|
||||
}
|
||||
|
||||
export default function createType <T extends Codec = Codec> (type: Text | string, value?: any, isPedantic?: boolean): T {
|
||||
export default function createType<T extends Codec = Codec> (type: Text | string, value?: any, isPedantic?: boolean): T {
|
||||
// l.debug(() => ['createType', { type, value }]);
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user