Swap to objectProperty from @polkadot/util (#4183)

* Swap to objectProperty from @polkadot/util

* Re-apply
This commit is contained in:
Jaco
2021-11-07 09:45:37 +02:00
committed by GitHub
parent 9219fa3197
commit 5a6eece6d6
20 changed files with 126 additions and 161 deletions
+1 -1
View File
@@ -26,7 +26,7 @@
"@babel/runtime": "^7.16.0",
"@polkadot/api": "6.6.2-26",
"@polkadot/types": "6.6.2-26",
"@polkadot/util": "^7.8.1",
"@polkadot/util": "^7.8.2",
"rxjs": "^7.4.0"
}
}
+3 -3
View File
@@ -27,12 +27,12 @@
"@polkadot/api": "6.6.2-26",
"@polkadot/rpc-core": "6.6.2-26",
"@polkadot/types": "6.6.2-26",
"@polkadot/util": "^7.8.1",
"@polkadot/util-crypto": "^7.8.1",
"@polkadot/util": "^7.8.2",
"@polkadot/util-crypto": "^7.8.2",
"rxjs": "^7.4.0"
},
"devDependencies": {
"@polkadot/keyring": "^7.8.1",
"@polkadot/keyring": "^7.8.2",
"@polkadot/rpc-provider": "6.6.2-26"
}
}
+3 -3
View File
@@ -25,13 +25,13 @@
"dependencies": {
"@babel/runtime": "^7.16.0",
"@polkadot/api-derive": "6.6.2-26",
"@polkadot/keyring": "^7.8.1",
"@polkadot/keyring": "^7.8.2",
"@polkadot/rpc-core": "6.6.2-26",
"@polkadot/rpc-provider": "6.6.2-26",
"@polkadot/types": "6.6.2-26",
"@polkadot/types-known": "6.6.2-26",
"@polkadot/util": "^7.8.1",
"@polkadot/util-crypto": "^7.8.1",
"@polkadot/util": "^7.8.2",
"@polkadot/util-crypto": "^7.8.2",
"eventemitter3": "^4.0.7",
"rxjs": "^7.4.0"
}
+2 -2
View File
@@ -26,10 +26,10 @@
"@babel/runtime": "^7.16.0",
"@polkadot/rpc-provider": "6.6.2-26",
"@polkadot/types": "6.6.2-26",
"@polkadot/util": "^7.8.1",
"@polkadot/util": "^7.8.2",
"rxjs": "^7.4.0"
},
"devDependencies": {
"@polkadot/keyring": "^7.8.1"
"@polkadot/keyring": "^7.8.2"
}
}
+6 -6
View File
@@ -25,15 +25,15 @@
"dependencies": {
"@babel/runtime": "^7.16.0",
"@polkadot/types": "6.6.2-26",
"@polkadot/util": "^7.8.1",
"@polkadot/util-crypto": "^7.8.1",
"@polkadot/x-fetch": "^7.8.1",
"@polkadot/x-global": "^7.8.1",
"@polkadot/x-ws": "^7.8.1",
"@polkadot/util": "^7.8.2",
"@polkadot/util-crypto": "^7.8.2",
"@polkadot/x-fetch": "^7.8.2",
"@polkadot/x-global": "^7.8.2",
"@polkadot/x-ws": "^7.8.2",
"eventemitter3": "^4.0.7"
},
"devDependencies": {
"@polkadot/keyring": "^7.8.1",
"@polkadot/keyring": "^7.8.2",
"@polkadot/types": "6.6.2-26",
"mock-socket": "^9.0.7",
"nock": "^13.1.4"
+1 -1
View File
@@ -37,7 +37,7 @@
"@polkadot/rpc-provider": "6.6.2-26",
"@polkadot/types": "6.6.2-26",
"@polkadot/types-support": "6.6.2-26",
"@polkadot/util": "^7.8.1",
"@polkadot/util": "^7.8.2",
"handlebars": "^4.7.7",
"websocket": "^1.0.34",
"yargs": "^17.2.1"
+2 -2
View File
@@ -24,8 +24,8 @@
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.16.0",
"@polkadot/networks": "^7.8.1",
"@polkadot/networks": "^7.8.2",
"@polkadot/types": "6.6.2-26",
"@polkadot/util": "^7.8.1"
"@polkadot/util": "^7.8.2"
}
}
+1 -1
View File
@@ -24,6 +24,6 @@
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.16.0",
"@polkadot/util": "^7.8.1"
"@polkadot/util": "^7.8.2"
}
}
+3 -3
View File
@@ -24,12 +24,12 @@
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.16.0",
"@polkadot/util": "^7.8.1",
"@polkadot/util-crypto": "^7.8.1",
"@polkadot/util": "^7.8.2",
"@polkadot/util-crypto": "^7.8.2",
"rxjs": "^7.4.0"
},
"devDependencies": {
"@polkadot/keyring": "^7.8.1",
"@polkadot/keyring": "^7.8.2",
"@polkadot/types-support": "6.6.2-26",
"@types/bn.js": "^4.11.6",
"bn.js": "^4.12.0"
+4 -4
View File
@@ -5,10 +5,10 @@ import type { HexString } from '@polkadot/util/types';
import type { CodecHash, Hash } from '../interfaces';
import type { AnyJson, Codec, Constructor, IEnum, Registry } from '../types';
import { assert, hexToU8a, isHex, isNumber, isObject, isString, isU8a, isUndefined, stringCamelCase, stringify, stringUpperFirst, u8aConcat, u8aToHex } from '@polkadot/util';
import { assert, hexToU8a, isHex, isNumber, isObject, isString, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, stringUpperFirst, u8aConcat, u8aToHex } from '@polkadot/util';
import { Null } from '../primitive/Null';
import { defineProperties, mapToTypeMap, typesToMap } from './utils';
import { mapToTypeMap, typesToMap } from './utils';
// export interface, this is used in Enum.with, so required as public by TS
export interface EnumConstructor<T = Codec> {
@@ -214,8 +214,8 @@ export class Enum implements IEnum {
constructor (registry: Registry, value?: unknown, index?: number) {
super(registry, Types, value, index);
defineProperties(this, isKeys, (_, i) => this.type === keys[i]);
defineProperties(this, asKeys, (k, i): Codec => {
objectProperties(this, isKeys, (_, i) => this.type === keys[i]);
objectProperties(this, asKeys, (k, i): Codec => {
assert(this[isKeys[i] as keyof this], () => `Cannot convert '${this.type}' via ${k}`);
return this.value;
+3 -3
View File
@@ -5,9 +5,9 @@ import type { HexString } from '@polkadot/util/types';
import type { CodecHash, Hash } from '../interfaces/runtime';
import type { AnyJson, Codec, Registry } from '../types';
import { isFunction, stringify } from '@polkadot/util';
import { isFunction, objectProperties, stringify } from '@polkadot/util';
import { compareMap, defineProperties } from './utils';
import { compareMap } from './utils';
/** @internal */
function decodeJson (value?: Record<string, unknown> | null): [string, any][] {
@@ -34,7 +34,7 @@ export class Json extends Map<string, any> implements Codec {
this.registry = registry;
defineProperties(this, decoded.map(([k]) => k), (k) => this.get(k));
objectProperties(this, decoded.map(([k]) => k), (k) => this.get(k));
}
/**
+3 -3
View File
@@ -5,9 +5,9 @@ import type { HexString } from '@polkadot/util/types';
import type { CodecHash, Hash } from '../interfaces/runtime';
import type { Constructor, ISet, Registry } from '../types';
import { assert, BN, bnToBn, bnToU8a, isBn, isNumber, isString, isU8a, isUndefined, stringCamelCase, stringify, stringUpperFirst, u8aToBn, u8aToHex, u8aToU8a } from '@polkadot/util';
import { assert, BN, bnToBn, bnToU8a, isBn, isNumber, isString, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, stringUpperFirst, u8aToBn, u8aToHex, u8aToU8a } from '@polkadot/util';
import { compareArray, defineProperties } from './utils';
import { compareArray } from './utils';
type SetValues = Record<string, number | BN>;
@@ -115,7 +115,7 @@ export class CodecSet extends Set<string> implements ISet<string> {
constructor (registry: Registry, value?: unknown) {
super(registry, values, value as undefined, bitLength);
defineProperties(this, isKeys, (_, i) => this.strings.includes(keys[i]));
objectProperties(this, isKeys, (_, i) => this.strings.includes(keys[i]));
}
};
}
+3 -3
View File
@@ -5,9 +5,9 @@ import type { HexString } from '@polkadot/util/types';
import type { CodecHash, Hash } from '../interfaces/runtime';
import type { AnyJson, BareOpts, Codec, Constructor, ConstructorDef, IStruct, Registry } from '../types';
import { assert, hexToU8a, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, stringCamelCase, stringify, u8aConcat, u8aToHex } from '@polkadot/util';
import { assert, hexToU8a, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcat, u8aToHex } from '@polkadot/util';
import { compareMap, decodeU8a, defineProperties, mapToTypeMap, typesToMap } from './utils';
import { compareMap, decodeU8a, mapToTypeMap, typesToMap } from './utils';
type TypesDef<T = Codec> = Record<string, string | Constructor<T>>;
@@ -153,7 +153,7 @@ export class Struct<
constructor (registry: Registry, value?: unknown) {
super(registry, Types, value as HexString, jsonMap);
defineProperties(this, keys, (k) => this.get(k));
objectProperties(this, keys, (k) => this.get(k));
}
};
}
@@ -1,27 +0,0 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { isUndefined } from '@polkadot/util';
const SKIP_KEYS = ['entries', 'hash', 'keys', 'values'];
export function defineProperty (that: object, key: string, get: () => unknown): void {
if (SKIP_KEYS.includes(key)) {
return defineProperty(that, `_${key}`, get);
}
// We use an isUndefined check here, not hasOwwnProperty since there are set in derived
// classes (inside with) and _Own_ properties refers to the class only, not parents
if (isUndefined((that as Record<string, unknown>)[key])) {
Object.defineProperty(that, key, { enumerable: true, get });
}
}
export function defineProperties (that: object, keys: string[], get: (k: string, i: number) => unknown): void {
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const index = i;
defineProperty(that, key, () => get(key, index));
}
}
-1
View File
@@ -5,7 +5,6 @@ export { compareArray } from './compareArray';
export { compareMap } from './compareMap';
export { compareSet } from './compareSet';
export { decodeU8a, decodeU8aVec } from './decodeU8a';
export { defineProperties, defineProperty } from './defineProperty';
export { mapToTypeMap } from './mapToTypeMap';
export { typeToConstructor } from './typeToConstructor';
export { sortAsc, sortSet, sortMap } from './sortValues';
+3 -5
View File
@@ -10,10 +10,9 @@ import type { AnyJson, AnyTuple, AnyU8a, ArgsDef, CallBase, ExtrinsicPayloadValu
import type { GenericExtrinsicEra } from './ExtrinsicEra';
import type { ExtrinsicValueV4 } from './v4/Extrinsic';
import { assert, compactAddLength, compactFromU8a, isHex, isU8a, objectSpread, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
import { assert, compactAddLength, compactFromU8a, isHex, isU8a, objectProperty, objectSpread, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
import { Base } from '../codec/Base';
import { defineProperty } from '../codec/utils';
import { BIT_SIGNED, BIT_UNSIGNED, DEFAULT_VERSION, UNMASK_VERSION } from './constants';
interface CreateOptions {
@@ -41,13 +40,12 @@ abstract class ExtrinsicBase<A extends AnyTuple> extends Base<ExtrinsicVx | Extr
super(registry, value, initialU8aLength);
const signKeys = Object.keys(registry.getSignedExtensionTypes());
const getter = (key: string) => (this._raw as ExtrinsicVx).signature[key as 'signer'];
// This is on the abstract class, ensuring that hasOwnProperty operates
// correctly, i.e. it needs to be on the base class exposing it
for (let i = 0; i < signKeys.length; i++) {
const key = signKeys[i];
defineProperty(this, key, () => (this._raw as ExtrinsicVx).signature[key as 'signer']);
objectProperty(this, signKeys[i], getter);
}
}
@@ -5,12 +5,11 @@ import type { HexString } from '@polkadot/util/types';
import type { Address, Balance, BlockNumber, Call, ExtrinsicEra, Hash, Index, RuntimeVersion } from '../interfaces';
import type { Codec, ISignerPayload, Registry, SignerPayloadJSON, SignerPayloadRaw } from '../types';
import { objectSpread, u8aToHex } from '@polkadot/util';
import { objectProperty, objectSpread, u8aToHex } from '@polkadot/util';
import { Compact } from '../codec/Compact';
import { Option } from '../codec/Option';
import { Struct } from '../codec/Struct';
import { defineProperty } from '../codec/utils';
import { Vec } from '../codec/Vec';
import { Text } from '../primitive/Text';
import { u8 } from '../primitive/U8';
@@ -57,6 +56,7 @@ export class GenericSignerPayload extends Struct implements ISignerPayload, Sign
super(registry, objectSpread<Record<string, string>>({}, extensionTypes, knownTypes), value);
this._extraTypes = {};
const getter = (key: string) => this.get(key);
// add all extras that are not in the base types
for (const [key, type] of Object.entries(extensionTypes)) {
@@ -64,7 +64,7 @@ export class GenericSignerPayload extends Struct implements ISignerPayload, Sign
this._extraTypes[key] = type;
}
defineProperty(this, key, () => this.get(key));
objectProperty(this, key, getter);
}
}
@@ -7,12 +7,11 @@ import type { Address, Balance, Call, Index } from '../../interfaces/runtime';
import type { ExtrinsicPayloadValue, IExtrinsicSignature, IKeyringPair, Registry, SignatureOptions } from '../../types';
import type { ExtrinsicSignatureOptions } from '../types';
import { assert, isU8a, isUndefined, objectSpread, stringify, u8aConcat, u8aToHex } from '@polkadot/util';
import { assert, isU8a, isUndefined, objectProperties, objectSpread, stringify, u8aConcat, u8aToHex } from '@polkadot/util';
import { Compact } from '../../codec/Compact';
import { Enum } from '../../codec/Enum';
import { Struct } from '../../codec/Struct';
import { defineProperty } from '../../codec/utils';
import { EMPTY_U8A, IMMORTAL_ERA } from '../constants';
import { GenericExtrinsicPayloadV4 } from './ExtrinsicPayload';
@@ -50,11 +49,7 @@ export class GenericExtrinsicSignatureV4 extends Struct implements IExtrinsicSig
: FAKE_NONE;
this.#signKeys = Object.keys(signTypes);
for (let i = 0; i < this.#signKeys.length; i++) {
const key = this.#signKeys[i];
defineProperty(this, key, () => this.get(key));
}
objectProperties(this, this.#signKeys, (k) => this.get(k));
}
/** @internal */