/** * @author adufilie http://github.com/adufilie */ const UNDEFINED = {} as any; export type AnyFunction = (...args:any[])=>any; function isPrimitive(value:any):boolean { return value === null || typeof value !== 'object'; } /** * Provides Map-like interface that uses Map for primitive keys and WeakMap for non-primitive keys. */ export class MeekMap { private map = new Map(); private weakMap = new WeakMap(); has(key:K):boolean { return isPrimitive(key) ? this.map.has(key) : this.weakMap.has(key); } get(key:K):V|undefined { return isPrimitive(key) ? this.map.get(key) : this.weakMap.get(key); } set(key:K, value:V):this { if (isPrimitive(key)) this.map.set(key, value); else this.weakMap.set(key, value); return this; } } /** * Provides a multi-dimensional Map-like interface */ export class HyperMap { has(args:any[]):boolean { let cache:MeekMap|undefined = this.getCache(args.length); if (args.length > 1) cache = this.traverse(cache, args.slice(0, args.length - 1)) as any; return cache !== undefined && cache.has(args[args.length - 1]); } get(args:any[]):T | undefined { return this.traverse(this.getCache(args.length), args); } set(args:any[], value:T):void { this.traverse(this.getCache(args.length), args, value); } // gets the Cache designated for a specific key length private getCache(numArgs:number) { let cache = this.map_numArgs_cache.get(numArgs); if (!cache) this.map_numArgs_cache.set(numArgs, cache = new MeekMap()); return cache; } // used to avoiding the varying-key-length limitation of the traverse() function below private map_numArgs_cache = new Map>(); // dual-purpose setter/getter // note: does not work if subsequent calls vary the length of the keys array for the same cache param private traverse(cache:MeekMap, keys:any[], value?:T):T|undefined { if (keys.length == 0) return undefined; if (keys.length == 1) { if (value === undefined) return cache.get(keys[0]); return void cache.set(keys[0], value); } let nextCache = cache.get(keys[0]); if (nextCache === undefined) cache.set(keys[0], nextCache = new MeekMap()); return this.traverse(nextCache, keys.slice(1), value); } } class MemoizerResult> { private func:Func; private cache = new HyperMap(); constructor(func:Func, getAdditionalArgs?:()=>any[], fixedArgsLength?:number) { this.func = func; let cache = this.cache; this.get = function(...args:any[]) { if (fixedArgsLength !== undefined) args.length = fixedArgsLength; let thisArgs = [this]; if (getAdditionalArgs) thisArgs = thisArgs.concat(getAdditionalArgs.call(this), args); else thisArgs = thisArgs.concat(args); let result = cache.get(thisArgs); if (result === undefined && !cache.has(thisArgs)) { cache.set(thisArgs, UNDEFINED); result = func.apply(this, args); if (result !== undefined) cache.set(thisArgs, result); } return result === UNDEFINED ? undefined : result; } as Func; } get:Func; } export type MemoizeParams = { function: F, getAdditionalArgs?: ()=>any[], fixedArgsLength?: number, }; /** * Creates a memoized version of a function. */ export default function memoize(params:MemoizeParams):F; /** * A method decorator that creates a memoized version of a method. */ export default function memoize(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor):TypedPropertyDescriptor | void; export default function memoize():TypedPropertyDescriptor | void { // called as decorator, target is prototype; return modified descriptor if (arguments.length == 3) { let [target, propertyKey, descriptor] = Array.from(arguments) as [Object, string|symbol, TypedPropertyDescriptor]; return decorate(descriptor); } // called as function let params = arguments[0] as MemoizeParams; if (typeof params === 'function') params = {function: params}; if (arguments.length == 1 && params && typeof params.function === 'function') { return new Memoizer(params.function, params.getAdditionalArgs, params.fixedArgsLength).get; } else { throw new Error("Usage: memoize(params:{ function:Function, getAdditionalArgs?:()=>any[], fixedArgsLength?:number })"); } } type DecorateMemoizeParams = Partial, 'getAdditionalArgs' | 'fixedArgsLength'>>; function decorate(descriptor:TypedPropertyDescriptor, params?:DecorateMemoizeParams) { if (descriptor && typeof descriptor.value === 'function') descriptor.value = new Memoizer(descriptor.value, params && params.getAdditionalArgs, params && params.fixedArgsLength).get; else if (descriptor && (descriptor.set || descriptor.get)) throw new Error('memoize cannot be used as a decorator for a setter or getter'); return descriptor; } // /** // * Generates a method decorator that creates a memoized version of a function with additional args to control memoization // * @param getAdditionalMemoizeArgs A function that returns additional arguments for controlling memoization. // * If this is an inline function, it must be defined like // * function() { return [this.a, this.b]; } // * rather than // * () => [this.a, this.b] // * because when decorators are evaluated the 'this' context is undefined. // */ // export function memoizeWith(getAdditionalMemoizeArgs:(this:TARGET, self:TARGET)=>any[]) // :( // target: TARGET, // propertyKey: string | symbol, // descriptor: TypedPropertyDescriptor // ) => TypedPropertyDescriptor | void // { // return (target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor):TypedPropertyDescriptor => { // return decorate(descriptor, getAdditionalMemoizeArgs); // }; // } /** * Generates a method decorator that creates a memoized version of a function with additional args to control memoization * @param getMemoizeParams_propName The name of another class method that returns additional arguments for controlling memoization. */ export function memoizeWithany[]}>(getMemoizeParams_propName:P) :( target: TARGET, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor ) => TypedPropertyDescriptor | void { return (target: TARGET, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor):TypedPropertyDescriptor => { return decorate( descriptor, { getAdditionalArgs: function(this:TARGET) { let fn:()=>any[] = this[getMemoizeParams_propName] as any; return fn.call(this); } } ); }; }