// TODO(appium server 3.4.1+): Replace local `memoize` with imports from `appium/support` // once this driver declares that minimum server version. /** * Creates a memoized version of a function. * * @param fn - Function to memoize * @param resolver - Optional cache key resolver. If omitted, the first argument is used as the cache key. * @returns Memoized function with a mutable `.cache` map (compatible with lodash-style cache resets in tests). */ export function memoize any>( fn: Fn, resolver?: (...args: Parameters) => unknown, ): Fn & {cache: Map>} { const memoizedFn = function (this: unknown, ...args: Parameters) { const key = resolver ? resolver.apply(this, args) : args[0]; if (memoizedFn.cache.has(key)) { return memoizedFn.cache.get(key) as ReturnType; } const result = fn.apply(this, args); memoizedFn.cache.set(key, result); return result; } as unknown as Fn & {cache: Map>}; memoizedFn.cache = new Map>(); return memoizedFn; }