molstar
Version:
A comprehensive macromolecular library.
40 lines (39 loc) • 1.19 kB
JavaScript
/**
* Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.memoizeLatest = memoizeLatest;
exports.memoize1 = memoize1;
/** Cache the latest result from calls to a function with any number of arguments */
function memoizeLatest(f) {
let lastArgs = void 0, value = void 0;
return (...args) => {
if (!lastArgs || lastArgs.length !== args.length) {
lastArgs = args;
value = f.apply(void 0, args);
return value;
}
for (let i = 0, _i = args.length; i < _i; i++) {
if (args[i] !== lastArgs[i]) {
lastArgs = args;
value = f.apply(void 0, args);
return value;
}
}
return value;
};
}
/** Cache all results from calls to a function with a single argument */
function memoize1(f) {
const cache = new Map();
return a => {
if (cache.has(a))
return cache.get(a);
const v = f(a);
cache.set(a, v);
return v;
};
}
;