From 93f86a71541f55b697c087762b0f34914377bee5 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Sun, 15 May 2022 02:49:23 +0200 Subject: [PATCH] Bug fixes (#1968) * type fixes * remove unused code * cast Promise * remove ts-ignore --- _internal/utils/helper.ts | 3 ++- _internal/utils/resolve-args.ts | 2 +- infinite/index.ts | 31 ++++++++++++++----------------- test/use-swr-infinite.test.tsx | 15 +++++++++++++++ 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/_internal/utils/helper.ts b/_internal/utils/helper.ts index 06d9db5a8..2d3694f6d 100644 --- a/_internal/utils/helper.ts +++ b/_internal/utils/helper.ts @@ -1,5 +1,7 @@ import { SWRGlobalState } from './global-state' import { Key, Cache, State, GlobalState } from '../types' + +const EMPTY_CACHE = {} export const noop = () => {} // Using noop() as the undefined value as undefined can possibly be replaced @@ -23,7 +25,6 @@ export const isDocumentDefined = typeof document != STR_UNDEFINED export const hasRequestAnimationFrame = () => isWindowDefined && typeof window['requestAnimationFrame'] != STR_UNDEFINED -const EMPTY_CACHE = {} export const createCacheHelper = >( cache: Cache, key: Key diff --git a/_internal/utils/resolve-args.ts b/_internal/utils/resolve-args.ts index c24aee9c3..e33412e66 100644 --- a/_internal/utils/resolve-args.ts +++ b/_internal/utils/resolve-args.ts @@ -19,7 +19,7 @@ export const withArgs = (hook: any) => { let next = hook const { use } = config if (use) { - for (let i = use.length; i-- > 0; ) { + for (let i = use.length; i--; ) { next = use[i](next) } } diff --git a/infinite/index.ts b/infinite/index.ts index 9a5b55bfa..9ed00cae4 100644 --- a/infinite/index.ts +++ b/infinite/index.ts @@ -30,6 +30,7 @@ import type { import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js' const INFINITE_PREFIX = '$inf$' +const EMPTY_PROMISE = Promise.resolve() as Promise const getFirstPageKey = (getKey: SWRInfiniteKeyLoader) => { return serialize(getKey ? getKey(0, null) : null)[0] @@ -192,27 +193,21 @@ export const infinite = ((useSWRNext: SWRHook) => }, [swr.data]) const mutate = useCallback( - ( - ...args: - | [] - | [undefined | Data[] | Promise | MutatorCallback] - | [ - undefined | Data[] | Promise | MutatorCallback, - undefined | boolean | MutatorOptions - ] - ) => { - const data = args[0] - + // eslint-disable-next-line func-names + function ( + data?: undefined | Data[] | Promise | MutatorCallback, + opts?: undefined | boolean | MutatorOptions + ) { // When passing as a boolean, it's explicitly used to disable/enable // revalidation. const options = - typeof args[1] === 'boolean' ? { revalidate: args[1] } : args[1] || {} + typeof opts === 'boolean' ? { revalidate: opts } : opts || {} // Default to true. const shouldRevalidate = options.revalidate !== false // It is possible that the key is still falsy. - if (!infiniteKey) return + if (!infiniteKey) return EMPTY_PROMISE if (shouldRevalidate) { if (!isUndefined(data)) { @@ -225,7 +220,9 @@ export const infinite = ((useSWRNext: SWRHook) => } } - return args.length ? swr.mutate(data, shouldRevalidate) : swr.mutate() + return arguments.length + ? swr.mutate(data, shouldRevalidate) + : swr.mutate() }, // swr.mutate is always the same reference // eslint-disable-next-line react-hooks/exhaustive-deps @@ -259,7 +256,7 @@ export const infinite = ((useSWRNext: SWRHook) => const setSize = useCallback( (arg: number | ((size: number) => number)) => { // It is possible that the key is still falsy. - if (!infiniteKey) return + if (!infiniteKey) return EMPTY_PROMISE let size if (isFunction(arg)) { @@ -267,7 +264,7 @@ export const infinite = ((useSWRNext: SWRHook) => } else if (typeof arg == 'number') { size = arg } - if (typeof size != 'number') return + if (typeof size != 'number') return EMPTY_PROMISE set({ $len: size }) lastPageSizeRef.current = size @@ -296,7 +293,7 @@ export const infinite = ((useSWRNext: SWRHook) => get isLoading() { return swr.isLoading } - } as SWRInfiniteResponse + } }) as unknown as Middleware export default withMiddleware(useSWR, infinite) as SWRInfiniteHook diff --git a/test/use-swr-infinite.test.tsx b/test/use-swr-infinite.test.tsx index 304008ea9..195a0e42c 100644 --- a/test/use-swr-infinite.test.tsx +++ b/test/use-swr-infinite.test.tsx @@ -925,6 +925,21 @@ describe('useSWRInfinite', () => { await screen.findByText('size: 3') }) + it('setSize should return a promise', async () => { + let _setSize + function Comp() { + const { setSize } = useSWRInfinite( + () => null, + () => createResponse('') + ) + + _setSize = setSize + return null + } + renderWithConfig() + expect(_setSize()).toBeInstanceOf(Promise) + }) + // https://github.com/vercel/swr/issues/908 //TODO: This test trigger act warning it('should revalidate first page after mutating', async () => {