-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
94 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,83 @@ | ||
import fastMemoize, { Options } from 'fast-memoize' | ||
import { AnyFunction } from './isFunction' | ||
|
||
export interface MemoizeOptions<T extends AnyFunction = AnyFunction> { | ||
/** | ||
* 创建缓存容器。 | ||
*/ | ||
createCache?(): { | ||
/** 设置缓存 */ | ||
set(key: string, value: any): void, | ||
get(key: string): any, | ||
/** 是否存在缓存 */ | ||
has(key: string): boolean, | ||
/** 获取缓存 */ | ||
get(key: string): any, | ||
/** 删除缓存 */ | ||
delete(key: string): void, | ||
/** 清空缓存 */ | ||
clear(): void, | ||
}, | ||
/** | ||
* 序列化函数实参。 | ||
*/ | ||
serializer?(...args: Parameters<T>): string, | ||
} | ||
|
||
export type MemoizeReturn<T extends AnyFunction = AnyFunction> = T & { | ||
/** 上一次执行函数时的缓存键名 */ | ||
lastCacheKey: string, | ||
/** 缓存容器 */ | ||
cache: ReturnType<MemoizeOptions['createCache']>, | ||
} | ||
|
||
/** | ||
* 函数参数结果缓存。 | ||
* | ||
* @param fn 要缓存的函数 | ||
* @param [options] 选项 | ||
* @returns 缓存化后的函数 | ||
*/ | ||
export function memoize<T extends AnyFunction>(fn: T, options?: MemoizeOptions<T>): T { | ||
let fastMemoizeOptions: Options<T> = undefined | ||
if (options) { | ||
fastMemoizeOptions = {} | ||
if (options.createCache) { | ||
fastMemoizeOptions.cache = { | ||
create: options.createCache, | ||
} as any | ||
} | ||
if (options.serializer) { | ||
fastMemoizeOptions.serializer = args => options.serializer.apply(null, args as any) | ||
export function memoize<T extends AnyFunction>( | ||
fn: T, | ||
{ | ||
createCache = () => { | ||
if (typeof Map === 'function' && Map.prototype.hasOwnProperty('clear')) { | ||
return new Map() | ||
} | ||
const cache = Object.create(null) | ||
return { | ||
set(k, v) { | ||
cache[k] = v | ||
}, | ||
has(k) { | ||
return (k in cache) | ||
}, | ||
get(k) { | ||
return cache[k] | ||
}, | ||
delete(k) { | ||
delete cache[k] | ||
}, | ||
clear() { | ||
Object.keys(cache).forEach(k => { | ||
delete cache[k] | ||
}) | ||
}, | ||
} | ||
}, | ||
serializer = (...args) => { | ||
return args.join(')`(') | ||
}, | ||
}: MemoizeOptions<T> = {}, | ||
): MemoizeReturn<T> { | ||
const cache = createCache() | ||
const memoizedFn: any = (...args: Parameters<T>) => { | ||
const cacheKey = serializer(...args) | ||
memoizedFn.lastCacheKey = cacheKey | ||
if (!cache.has(cacheKey)) { | ||
cache.set(cacheKey, fn(...args)) | ||
} | ||
return cache.get(cacheKey) | ||
} | ||
return fastMemoize(fn, fastMemoizeOptions) | ||
memoizedFn.cache = cache | ||
return memoizedFn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters