Home Reference Source Test Repository

buildDocs/applicators/MemoizeApplicator.js

import { isFunction, isObject } from 'lodash';
import { Applicator } from './Applicator';
import { resolveFunction } from '../utils';
export class MemoizeApplicator extends Applicator {
    apply({ value, instance, config: { execute }, args, target }) {
        let resolver = resolveFunction(isFunction(args[0]) ? args[0] : isObject(args[0]) ? args[0].resolver : args[0], instance, target, false);
        if (resolver && instance) {
            resolver = resolver.bind(instance);
        }
        const memoized = resolver ? execute(value, resolver) : execute(value);
        if (isObject(args[0])) {
            const { cache, type } = args[0];
            if (cache) {
                memoized.cache = cache;
            }
            else if (isFunction(type)) {
                memoized.cache = new type();
            }
        }
        return memoized;
    }
}