refactor: 重构buildCachedRegFn的实现

pull/13/head
moonrailgun 4 years ago
parent 342baf8683
commit c9801d9e59

@ -110,4 +110,17 @@ describe('buildCachedRegFn should be ok', () => {
get();
expect(fn.mock.calls.length).toBe(3);
});
test('should cache promise fn', () => {
const [get, set] = buildCachedRegFn('test');
const fn = jest.fn(async () => undefined);
set(fn);
get();
get();
get();
expect(fn.mock.calls.length).toBe(1);
get(1);
expect(fn.mock.calls.length).toBe(2);
});
});

@ -1,5 +1,6 @@
import _isFunction from 'lodash/isFunction';
import _isEqual from 'lodash/isEqual';
import { isPromise } from '../utils/is-promise';
/**
* get set
@ -53,7 +54,7 @@ export function buildRegFnWithEvent<F extends (...args: any[]) => any>(
/**
* buildRegFn
*/
export function buildCachedRegFn<F extends (...args: any) => Promise<any>>(
export function buildCachedRegFn<F extends (...args: any) => any>(
name: string,
defaultFunc?: F
) {
@ -62,17 +63,33 @@ export function buildCachedRegFn<F extends (...args: any) => Promise<any>>(
let _result: any = null; // 缓存的返回值
let _lastArgs: any;
const cachedGet = async (...args: any) => {
if (_result !== null && _isEqual(args, _lastArgs)) {
// 当有缓存的返回值且两次参数一致
return _result;
} else {
const result = await get(...args);
_result = result ?? null;
_lastArgs = args;
return result;
}
};
function isSame(args: any[]) {
// 当有缓存的返回值且两次参数一致
return _result !== null && _isEqual(args, _lastArgs);
}
// 根据是否为 promise 做区分
const cachedGet: any = isPromise(get)
? async (...args: any) => {
if (isSame(args)) {
return _result;
} else {
const result = await get(...args);
_result = result ?? null;
_lastArgs = args;
return result;
}
}
: (...args: any) => {
if (isSame(args)) {
return _result;
} else {
const result = get(...args);
_result = result ?? null;
_lastArgs = args;
return result;
}
};
const refreshCache = () => {
_result = null;

@ -0,0 +1,7 @@
export function isPromise(obj: any): obj is Promise<unknown> {
return (
!!obj &&
(typeof obj === 'object' || typeof obj === 'function') &&
typeof obj.then === 'function'
);
}
Loading…
Cancel
Save