Add memoize util (#805)
* Add memoize util * Allow for instanceId & normalize overrides * memoize tests
This commit is contained in:
@@ -9,6 +9,7 @@ export { assert, assertReturn } from './assert';
|
||||
export { detectPackage } from './detectPackage';
|
||||
export { extractTime } from './extractTime';
|
||||
export { logger } from './logger';
|
||||
export { memoize } from './memoize';
|
||||
export { promisify } from './promisify';
|
||||
|
||||
export * from './array';
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { memoize } from '.';
|
||||
|
||||
describe('memoize', (): void => {
|
||||
it('returns actual values', (): void => {
|
||||
const fn = memoize(() => 5);
|
||||
|
||||
expect(fn()).toEqual(5);
|
||||
});
|
||||
|
||||
it('caches executions', (): void => {
|
||||
const spy = jest.fn((a: number, b: number) => a + b);
|
||||
const fn = memoize(spy);
|
||||
|
||||
expect(fn(2, 4)).toEqual(6);
|
||||
expect(fn(2, 4)).toEqual(6);
|
||||
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
|
||||
expect(fn(4, 2)).toEqual(6);
|
||||
|
||||
expect(spy).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('does unmemoize', (): void => {
|
||||
const spy = jest.fn((a: number, b: number) => a + b);
|
||||
const fn = memoize(spy);
|
||||
|
||||
expect(fn(2, 4)).toEqual(6);
|
||||
expect(fn(4, 2)).toEqual(6);
|
||||
expect(spy).toHaveBeenCalledTimes(2);
|
||||
|
||||
fn.unmemoize(2, 4);
|
||||
|
||||
expect(fn(2, 4)).toEqual(6);
|
||||
expect(fn(4, 2)).toEqual(6);
|
||||
expect(spy).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
|
||||
it('manages separate instances', (): void => {
|
||||
let instanceId = 'a';
|
||||
const spy = jest.fn((a: number, b: number) => a + b);
|
||||
const fn = memoize(spy, { getInstanceId: () => instanceId });
|
||||
|
||||
expect(fn(1, 3)).toEqual(4);
|
||||
|
||||
instanceId = 'b';
|
||||
|
||||
expect(fn(1, 3)).toEqual(4);
|
||||
expect(spy).toHaveBeenCalledTimes(2);
|
||||
|
||||
instanceId = 'a';
|
||||
|
||||
expect(fn(1, 3)).toEqual(4);
|
||||
expect(spy).toHaveBeenCalledTimes(2);
|
||||
|
||||
instanceId = 'b';
|
||||
fn.unmemoize(1, 3);
|
||||
|
||||
expect(fn(1, 3)).toEqual(4);
|
||||
expect(spy).toHaveBeenCalledTimes(3);
|
||||
|
||||
instanceId = 'a';
|
||||
fn.unmemoize(1, 3);
|
||||
|
||||
expect(fn(1, 3)).toEqual(4);
|
||||
expect(spy).toHaveBeenCalledTimes(4);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
|
||||
import type { Memoized } from './types';
|
||||
|
||||
import { isUndefined } from './is/undefined';
|
||||
|
||||
interface Options {
|
||||
getInstanceId?: () => string,
|
||||
normalize?: (args: any[]) => string;
|
||||
}
|
||||
|
||||
const INSTANCEID = () => 'none';
|
||||
|
||||
export function memoize <T> (fn: (...args: any[]) => T, { getInstanceId = INSTANCEID }: Options = {}): Memoized<T> {
|
||||
const cache: Record<string, Record<string, T>> = {};
|
||||
|
||||
const memoized = (...args: any[]): T => {
|
||||
const stringParams = JSON.stringify({ args });
|
||||
const instanceId = getInstanceId();
|
||||
|
||||
if (!cache[instanceId]) {
|
||||
cache[instanceId] = {};
|
||||
}
|
||||
|
||||
if (isUndefined(cache[instanceId][stringParams])) {
|
||||
cache[instanceId][stringParams] = fn(...args);
|
||||
}
|
||||
|
||||
return cache[instanceId][stringParams];
|
||||
};
|
||||
|
||||
memoized.unmemoize = (...args: any[]): void => {
|
||||
const stringParams = JSON.stringify({ args });
|
||||
const instanceId = getInstanceId();
|
||||
|
||||
if (cache[instanceId] && !isUndefined(cache[instanceId][stringParams])) {
|
||||
delete cache[instanceId][stringParams];
|
||||
}
|
||||
};
|
||||
|
||||
return memoized;
|
||||
}
|
||||
@@ -47,3 +47,10 @@ export interface Time {
|
||||
seconds: number;
|
||||
milliseconds: number;
|
||||
}
|
||||
|
||||
export interface Memoized <T> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(...args: any[]): T;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
unmemoize: (...args: any[]) => void;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user