ts-ioc-container
Version:
Fast, lightweight TypeScript dependency injection container with a clean API, scoped lifecycles, decorators, tokens, hooks, lazy injection, customizable providers, and no global container objects.
24 lines (23 loc) • 766 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.shallowCache = void 0;
const shallowCache = (getKeyByArgs) => (target, propertyKey, descriptor) => {
const originalMethod = descriptor.value;
const cacheMap = new WeakMap();
descriptor.value = function (...args) {
let cache = cacheMap.get(this);
if (cache === undefined) {
cache = new Map();
cacheMap.set(this, cache);
}
const key = getKeyByArgs(...args);
if (cache.has(key)) {
return cache.get(key);
}
const result = originalMethod.apply(this, args);
cache.set(key, result);
return result;
};
return descriptor;
};
exports.shallowCache = shallowCache;