Files
klz-cables.com/.pnpm-store/v10/files/06/8a4f2d23e909b7b2831ba995f5f075116b77281a0b25dee503047cf37772b7b625c250b0ea0408f122959c92ab04f9531c1ba9624c98fb144a726d63a9650c
Marc Mintel 5397309103
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
fix(products): fix breadcrumbs and product filtering (backport from main)
2026-02-24 16:04:21 +01:00

57 lines
1.6 KiB
Plaintext

import areInputsEqual from './are-inputs-equal';
export type EqualityFn<TFunc extends (...args: any[]) => any> = (
newArgs: Parameters<TFunc>,
lastArgs: Parameters<TFunc>,
) => boolean;
export type MemoizedFn<TFunc extends (this: any, ...args: any[]) => any> = {
clear: () => void;
(this: ThisParameterType<TFunc>, ...args: Parameters<TFunc>): ReturnType<TFunc>;
};
// internal type
type Cache<TFunc extends (this: any, ...args: any[]) => any> = {
lastThis: ThisParameterType<TFunc>;
lastArgs: Parameters<TFunc>;
lastResult: ReturnType<TFunc>;
};
function memoizeOne<TFunc extends (this: any, ...newArgs: any[]) => any>(
resultFn: TFunc,
isEqual: EqualityFn<TFunc> = areInputsEqual,
): MemoizedFn<TFunc> {
let cache: Cache<TFunc> | null = null;
// breaking cache when context (this) or arguments change
function memoized(
this: ThisParameterType<TFunc>,
...newArgs: Parameters<TFunc>
): ReturnType<TFunc> {
if (cache && cache.lastThis === this && isEqual(newArgs, cache.lastArgs)) {
return cache.lastResult;
}
// Throwing during an assignment aborts the assignment: https://codepen.io/alexreardon/pen/RYKoaz
// Doing the lastResult assignment first so that if it throws
// the cache will not be overwritten
const lastResult = resultFn.apply(this, newArgs);
cache = {
lastResult,
lastArgs: newArgs,
lastThis: this,
};
return lastResult;
}
// Adding the ability to clear the cache of a memoized function
memoized.clear = function clear() {
cache = null;
};
return memoized;
}
export default memoizeOne;