execution-engine
Version:
A TypeScript library for tracing and visualizing code execution workflows.
31 lines (30 loc) • 1.44 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.memoize = memoize;
const memoize_1 = require("./memoize");
const functionMetadata_1 = require("../common/utils/functionMetadata");
/**
* Decorator to memoize method executions and prevent redundant calls.
*
* @param onMemoizeEvent - Optional callback triggered after checking memory
* @param ttl - Small 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.
*/
function memoize(onMemoizeEvent, ttl) {
return function (target, propertyKey, descriptor) {
const originalMethod = descriptor.value;
descriptor.value = function (...args) {
const thisMethodMetadata = (0, functionMetadata_1.extractClassMethodMetadata)(target.constructor.name, propertyKey, originalMethod);
return memoize_1.executeMemoize.bind(this)(originalMethod.bind(this), args, {
functionId: thisMethodMetadata.methodSignature,
onMemoizeEvent: functionMetadata_1.attachFunctionMetadata.bind(this)(onMemoizeEvent, thisMethodMetadata),
ttl
});
};
};
}
;