Skip to content

Commit 0cc8276

Browse files
committed
fix memoize and useCallbackFactory
1 parent 54fcacd commit 0cc8276

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/lib/tools/memoize.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
export function memoize<F extends (...args: (string | number | boolean)[]) => any>(
2-
fn: F,
3-
options: {
4-
argsLength: number;
1+
export function memoize<Args extends (number | boolean | string)[], R>(
2+
fn: (...args: Args) => R,
3+
options?: {
4+
argsLength?: number;
55
}
6-
): F {
7-
const cache: Record<string, ReturnType<F>> = {};
6+
): (...args: Args) => R {
7+
const cache: Record<string, R> = {};
88

9-
const { argsLength } = options;
9+
const { argsLength = fn.length } = options ?? {};
1010

11-
return ((...args: Parameters<F>) => {
11+
return ((...args: Args) => {
1212
const key = JSON.stringify(args.slice(0, argsLength).join("-sIs9sAslOdeWlEdIos3-"));
1313

1414
console.log(key, JSON.stringify({ argsLength, args }));

src/lib/tools/powerhooks/useCallbackFactory.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ export function useCallbackFactory<
3636
id<Out>((...factoryArgs) => {
3737
if (memoizedRef.current === undefined) {
3838
memoizedRef.current = memoize(
39-
((...factoryArgs: FactoryArgs) =>
39+
(...factoryArgs: FactoryArgs) =>
4040
(...args: Args) =>
41-
callbackRef.current(factoryArgs, args)) as any,
41+
callbackRef.current(factoryArgs, args),
4242
{ "argsLength": factoryArgs.length }
4343
);
4444
}
4545

46-
return (memoizedRef.current as any)(...factoryArgs);
46+
return memoizedRef.current(...factoryArgs);
4747
})
4848
)[0];
4949
}

0 commit comments

Comments
 (0)