Import lazy* from api repo (#1159)

This commit is contained in:
Jaco
2021-11-04 14:16:58 +02:00
committed by GitHub
parent 63b38c56ff
commit ff7c086b5b
5 changed files with 63 additions and 6 deletions
+1
View File
@@ -5,6 +5,7 @@
Changes:
- Add `objectClear` and `objectSpread` utility functions
- Add `lazyMethod` & `lazyMethods` utility functions
- Sync with upstream Substrate ss58 registry
+1 -1
View File
@@ -29,7 +29,7 @@
},
"devDependencies": {
"@babel/core": "^7.16.0",
"@polkadot/dev": "^0.63.23",
"@polkadot/dev": "^0.63.24",
"@polkadot/ts": "^0.4.13",
"@types/ed2curve": "^0.2.2",
"@types/jest": "^27.0.2"
+1
View File
@@ -15,6 +15,7 @@ export * from './extractTime';
export * from './format';
export * from './hex';
export * from './is';
export * from './lazy';
export * from './logger';
export * from './memoize';
export * from './number';
+55
View File
@@ -0,0 +1,55 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { isUndefined } from './is/undefined';
type AnyFn = (...args: unknown[]) => unknown;
type WithToString = { toString: () => string };
export function lazyMethod <T, K> (result: Record<string, T> | AnyFn, item: K, creator: (d: K) => T, getName?: (d: K) => string): void {
const name = getName
? getName(item)
: (item as WithToString).toString();
let value: T | undefined;
Object.defineProperty(result, name, {
// This allows for re-configuration with the embedded defineProperty below
// and ensures that on tested browsers and Node, it _will_ be redefined
// and thus short-circuited for future access
configurable: true,
enumerable: true,
// Use a function here, we don't want to capture the outer this, i.e.
// don't use arrow functions in this context since we have a this inside
get: function (): T {
// This check should _always_ be false and unneeded, since we override
// with a value below ... however we ensure we are quire vigilant against
// all environment failures, so we are rather be safe than sorry
if (isUndefined(value)) {
value = creator(item);
try {
// re-define the property as a value, next time around this
// getter will only return the computed value
Object.defineProperty(this, name, { value });
} catch {
// ignore any errors, since this _should_ not happen due to
// the "configurable" property above. But if it ever does
// from here-on we will be the cached value the next time
// around (with a very slight dip in performance)
}
}
return value;
}
});
}
export function lazyMethods <T, K> (result: Record<string, T>, items: K[], creator: (v: K) => T, getName?: (m: K) => string): Record<string, T> {
for (let i = 0; i < items.length; i++) {
lazyMethod(result, items[i], creator, getName);
}
return result;
}
+5 -5
View File
@@ -1930,9 +1930,9 @@ __metadata:
languageName: node
linkType: hard
"@polkadot/dev@npm:^0.63.23":
version: 0.63.23
resolution: "@polkadot/dev@npm:0.63.23"
"@polkadot/dev@npm:^0.63.24":
version: 0.63.24
resolution: "@polkadot/dev@npm:0.63.24"
dependencies:
"@babel/cli": ^7.16.0
"@babel/core": ^7.16.0
@@ -2017,7 +2017,7 @@ __metadata:
polkadot-exec-rollup: scripts/polkadot-exec-rollup.mjs
polkadot-exec-tsc: scripts/polkadot-exec-tsc.mjs
polkadot-exec-webpack: scripts/polkadot-exec-webpack.mjs
checksum: f7c389cea148c32211e3ade061660d98294a017a73b7be0ebd50f1de86e7552f305af6eae36f49ee4df54f587d1638c68260e8c69a638fb5a889100b1ea47938
checksum: fd538da898f7c88dd5e3c939927af4726f75190b39aa299ab2481776eb6569356a7be8da9b32af294cd2d2083ae168c95faf69a207b0c82a0d2e6b282b434630
languageName: node
linkType: hard
@@ -9192,7 +9192,7 @@ resolve@^2.0.0-next.3:
resolution: "root-workspace-0b6124@workspace:."
dependencies:
"@babel/core": ^7.16.0
"@polkadot/dev": ^0.63.23
"@polkadot/dev": ^0.63.24
"@polkadot/ts": ^0.4.13
"@types/ed2curve": ^0.2.2
"@types/jest": ^27.0.2