@devexperts/dxcharts-lite
Version:
74 lines (73 loc) • 2.31 kB
JavaScript
/*
* Copyright (C) 2019 - 2025 Devexperts Solutions IE Limited
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// eslint-disable-next-line no-restricted-syntax
export const MEMOIZE_CLEAR_FUNCTION = Symbol('MEMOIZE_CLEAR_FUNCTION');
/**
* Memoizes function for passed arguments
* @doc-tags utility
*/
export function memoize(fn) {
const storage = {};
const result = function () {
const args = Array.prototype.slice.call(arguments);
const key = serialize(args);
if (typeof storage[key] === 'undefined') {
// eslint-disable-next-line no-restricted-syntax
storage[key] = fn.apply(this, args);
}
return storage[key];
}.bind(this);
// inject clearer
// @ts-ignore
result[MEMOIZE_CLEAR_FUNCTION] = function () {
const args = Array.prototype.slice.call(arguments);
const key = serialize(args);
delete storage[key];
};
// eslint-disable-next-line no-restricted-syntax
return result;
}
export const memoOnce = (E) => (f) => {
let hasValue = false;
let cachedR;
// eslint-disable-next-line no-restricted-syntax
let cachedArgs = [];
const update = (args) => {
cachedR = f(...args);
hasValue = true;
cachedArgs = args;
};
return (...args) => {
const length = args.length;
if (hasValue && length === 0) {
return cachedR;
}
if (!hasValue || cachedArgs.length !== length) {
update(args);
return cachedR;
}
for (let i = 0; i < length; i++) {
if (!E.equals(cachedArgs[i], args[i])) {
update(args);
return cachedR;
}
}
return cachedR;
};
};
/**
* @param {Array.<*>} args
* @returns {String}
*/
export function serialize(args) {
const argsAreValid = args.every(arg => {
return typeof arg === 'number' || typeof arg === 'string' || typeof arg === 'boolean';
});
if (!argsAreValid) {
throw Error('Arguments to memoized function can only be strings or numbers');
}
return JSON.stringify(args);
}