-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemoizeDecorator.ts
More file actions
30 lines (29 loc) · 1.64 KB
/
memoizeDecorator.ts
File metadata and controls
30 lines (29 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { executeMemoize } from './memoize';
import { FunctionMetadata } from '../common/models/executionFunction.model';
import { MemoizationHandler } from '../common/models/executionMemoization.model';
import { attachFunctionMetadata, extractClassMethodMetadata } from '../common/utils/functionMetadata';
/**
* Decorator to memoize method executions and prevent redundant calls.
*
* @param memoizationHandler - Optional callback triggered after checking memory
* @param expirationMs - Duration (in milliseconds) before clearing the stored result,
* capped at 1000ms to prevent excessive retention.
* @returns A method decorator for applying memoization.
*
* @remarks
* Uses `executeMemoize` internally to store and reuse results.
* A short delay (e.g., 100ms) ensures that multiple rapid calls can reuse the stored result.
*/
export function memoize<O>(memoizationHandler?: MemoizationHandler<O>, expirationMs?: number): MethodDecorator {
return function <T extends Record<string, unknown>>(target: T, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
descriptor.value = function (...args: unknown[]): ReturnType<typeof originalMethod> {
const thisMethodMetadata: FunctionMetadata = extractClassMethodMetadata(target.constructor.name, propertyKey, originalMethod);
return (executeMemoize.bind(this) as typeof executeMemoize<O>)(originalMethod.bind(this), args, {
functionId: thisMethodMetadata.methodSignature,
memoizationHandler: attachFunctionMetadata.bind(this)(memoizationHandler, thisMethodMetadata),
expirationMs
});
};
};
}