mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-17 19:13:34 +08:00
refactor(@angular/cli): use non-experimental decorators for internal memoize
With standard decorator support now available for use, the memoize decorator has been updated to be a standard decorator instead of a TypeScript experimental decorator. This change also removes the only usage of decorators within the Angular CLI code itself. This change does not affect application code.
This commit is contained in:
parent
17e168379e
commit
137949e8ff
@ -13,23 +13,17 @@
|
|||||||
*
|
*
|
||||||
* @see https://en.wikipedia.org/wiki/Memoization
|
* @see https://en.wikipedia.org/wiki/Memoization
|
||||||
*/
|
*/
|
||||||
export function memoize<T>(
|
export function memoize<This, Args extends unknown[], Return>(
|
||||||
target: Object,
|
target: (this: This, ...args: Args) => Return,
|
||||||
propertyKey: string | symbol,
|
context: ClassMemberDecoratorContext,
|
||||||
descriptor: TypedPropertyDescriptor<T>,
|
) {
|
||||||
): TypedPropertyDescriptor<T> {
|
if (context.kind !== 'method' && context.kind !== 'getter') {
|
||||||
const descriptorPropertyName = descriptor.get ? 'get' : 'value';
|
|
||||||
const originalMethod: unknown = descriptor[descriptorPropertyName];
|
|
||||||
|
|
||||||
if (typeof originalMethod !== 'function') {
|
|
||||||
throw new Error('Memoize decorator can only be used on methods or get accessors.');
|
throw new Error('Memoize decorator can only be used on methods or get accessors.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const cache = new Map<string, unknown>();
|
const cache = new Map<string, Return>();
|
||||||
|
|
||||||
return {
|
return function (this: This, ...args: Args): Return {
|
||||||
...descriptor,
|
|
||||||
[descriptorPropertyName]: function (this: unknown, ...args: unknown[]) {
|
|
||||||
for (const arg of args) {
|
for (const arg of args) {
|
||||||
if (!isJSONSerializable(arg)) {
|
if (!isJSONSerializable(arg)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@ -40,14 +34,13 @@ export function memoize<T>(
|
|||||||
|
|
||||||
const key = JSON.stringify(args);
|
const key = JSON.stringify(args);
|
||||||
if (cache.has(key)) {
|
if (cache.has(key)) {
|
||||||
return cache.get(key);
|
return cache.get(key) as Return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = originalMethod.apply(this, args);
|
const result = target.apply(this, args);
|
||||||
cache.set(key, result);
|
cache.set(key, result);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
"noEmitOnError": true,
|
"noEmitOnError": true,
|
||||||
"experimentalDecorators": true,
|
|
||||||
"noFallthroughCasesInSwitch": true,
|
"noFallthroughCasesInSwitch": true,
|
||||||
"noImplicitOverride": true,
|
"noImplicitOverride": true,
|
||||||
"isolatedModules": true,
|
"isolatedModules": true,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user