UNPKG

@fivem-ts/shared

Version:

FiveM Typescript wrapper shared part

63 lines (62 loc) 2.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Cache = Cache; require("reflect-metadata"); /** * A decorator that caches the result of a method for a specified duration. * * The `@Cache` decorator can be applied to methods to store their return values for a given amount * of time. If the method is called again within the cache duration, the cached result is returned * instead of re-executing the method. This can improve performance by reducing the need for repeated * expensive computations or operations. * * @example * * ```ts * class ExampleClass { * @Cache(5000) // Cache the result for 5 seconds * computeExpensiveOperation() { * console.log('Computing...'); * return Math.random(); // Simulate an expensive operation * } * } * * const example = new ExampleClass(); * * // First call will compute and cache the result * console.log(example.computeExpensiveOperation()); // Outputs: Computing... followed by a random number * * // Subsequent calls within 5 seconds will return the cached result * console.log(example.computeExpensiveOperation()); // Outputs: the cached random number * * // After 5 seconds, a new result will be computed and cached again * setTimeout(() => console.log(example.computeExpensiveOperation()), 6000); // Outputs: Computing... followed by a new random number * ``` * * @param duration The duration (in milliseconds) for which the result should be cached. * * @returns A function that can be used as a method decorator. */ function Cache(duration) { return function (_target, propertyKey, descriptor) { const originalMethod = descriptor.value; const cacheKey = Symbol.for(propertyKey); const cacheExpiryKey = Symbol.for(`${propertyKey}_expiry`); descriptor.value = function (...args) { const now = Date.now(); // @ts-ignore if (this[cacheExpiryKey] && this[cacheExpiryKey] > now) { console.log(`Returning cached result for ${propertyKey}`); // @ts-ignore return this[cacheKey]; } const result = originalMethod.apply(this, args); // @ts-ignore this[cacheKey] = result; // @ts-ignore this[cacheExpiryKey] = now + duration; return result; }; console.log(`Registered cache for function: ${propertyKey} with duration: ${duration}ms`); }; }