Skip to content

Commit

Permalink
feat: rewrite memoize
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Apr 10, 2019
1 parent 0bfe3dd commit 9da18a9
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/memoize.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
import memoize from 'fast-memoize'
import fastMemoize, { Options } from 'fast-memoize'
import { AnyFunction } from './isFunction'

export { memoize }
export interface MemoizeOptions<T extends AnyFunction = AnyFunction> {
createCache?(): {
set(key: string, value: any): void,
get(key: string): any,
has(key: string): boolean,
},
serializer?(args: Parameters<T>): string,
}

/**
* 函数参数结果缓存。
*
* @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 = options.serializer as any
}
}
return fastMemoize(fn, fastMemoizeOptions)
}

0 comments on commit 9da18a9

Please sign in to comment.