Skip to content

Commit 137949e

Browse files
clydindgp1130
authored andcommitted
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.
1 parent 17e1683 commit 137949e

File tree

2 files changed

+20
-28
lines changed

2 files changed

+20
-28
lines changed

packages/angular/cli/src/utilities/memoize.ts

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,34 @@
1313
*
1414
* @see https://en.wikipedia.org/wiki/Memoization
1515
*/
16-
export function memoize<T>(
17-
target: Object,
18-
propertyKey: string | symbol,
19-
descriptor: TypedPropertyDescriptor<T>,
20-
): TypedPropertyDescriptor<T> {
21-
const descriptorPropertyName = descriptor.get ? 'get' : 'value';
22-
const originalMethod: unknown = descriptor[descriptorPropertyName];
23-
24-
if (typeof originalMethod !== 'function') {
16+
export function memoize<This, Args extends unknown[], Return>(
17+
target: (this: This, ...args: Args) => Return,
18+
context: ClassMemberDecoratorContext,
19+
) {
20+
if (context.kind !== 'method' && context.kind !== 'getter') {
2521
throw new Error('Memoize decorator can only be used on methods or get accessors.');
2622
}
2723

28-
const cache = new Map<string, unknown>();
24+
const cache = new Map<string, Return>();
2925

30-
return {
31-
...descriptor,
32-
[descriptorPropertyName]: function (this: unknown, ...args: unknown[]) {
33-
for (const arg of args) {
34-
if (!isJSONSerializable(arg)) {
35-
throw new Error(
36-
`Argument ${isNonPrimitive(arg) ? arg.toString() : arg} is JSON serializable.`,
37-
);
38-
}
26+
return function (this: This, ...args: Args): Return {
27+
for (const arg of args) {
28+
if (!isJSONSerializable(arg)) {
29+
throw new Error(
30+
`Argument ${isNonPrimitive(arg) ? arg.toString() : arg} is JSON serializable.`,
31+
);
3932
}
33+
}
4034

41-
const key = JSON.stringify(args);
42-
if (cache.has(key)) {
43-
return cache.get(key);
44-
}
35+
const key = JSON.stringify(args);
36+
if (cache.has(key)) {
37+
return cache.get(key) as Return;
38+
}
4539

46-
const result = originalMethod.apply(this, args);
47-
cache.set(key, result);
40+
const result = target.apply(this, args);
41+
cache.set(key, result);
4842

49-
return result;
50-
},
43+
return result;
5144
};
5245
}
5346

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"module": "commonjs",
66
"moduleResolution": "node",
77
"noEmitOnError": true,
8-
"experimentalDecorators": true,
98
"noFallthroughCasesInSwitch": true,
109
"noImplicitOverride": true,
1110
"isolatedModules": true,

0 commit comments

Comments
 (0)