UNPKG

ffbt

Version:

Build a Typescript app without pain

47 lines (46 loc) 1.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.dropCacheFor = exports.Memoize = void 0; /** * Decorates method/getter call * @param descriptor - PropertyDescriptor. See details in Object.defineProperty docs (Link below) * @param type - Specifies which property to decorate * @param propertyKey - method/getter name * * @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty */ function decoratePropertyDescriptor(descriptor, type, propertyKey) { const originalFunc = descriptor[type]; descriptor[type] = function (...args) { if (!this.__memoizeDecoratorData) { this.__memoizeDecoratorData = {}; } const hasCachedValue = this.__memoizeDecoratorData.hasOwnProperty(propertyKey); if (!hasCachedValue) { this.__memoizeDecoratorData[propertyKey] = originalFunc.apply(this, args); } return this.__memoizeDecoratorData[propertyKey]; }; } /** * Caches function execution result between calls */ function Memoize() { return function (target, propertyKey, descriptor) { if (descriptor.set) { throw new Error("Can't memoize descriptor with setter"); } const decorationType = descriptor.get ? "get" : "value"; decoratePropertyDescriptor(descriptor, decorationType, propertyKey); return descriptor; }; } exports.Memoize = Memoize; function dropCacheFor(obj, method) { const cachedValues = obj.__memoizeDecoratorData; if (!cachedValues) { return; } delete cachedValues[method]; } exports.dropCacheFor = dropCacheFor;