From d9866b9aad7bc0f846cc1cd7411edd5858e05055 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Sun, 15 May 2022 01:39:51 +0200 Subject: [PATCH 1/4] type fixes --- _internal/utils/helper.ts | 6 +++++- _internal/utils/resolve-args.ts | 2 +- infinite/index.ts | 35 +++++++++++++++++---------------- test/use-swr-infinite.test.tsx | 15 ++++++++++++++ 4 files changed, 39 insertions(+), 19 deletions(-) diff --git a/_internal/utils/helper.ts b/_internal/utils/helper.ts index 06d9db5a8..98b0228b7 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 @@ -41,3 +42,6 @@ export const createCacheHelper = >( state[5] ] as const } + +// Extend a SWR object without triggering its getters. +export const extendSWR = () => {} 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..3eaf44507 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(UNDEFINED) 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,13 @@ export const infinite = ((useSWRNext: SWRHook) => } } - return args.length ? swr.mutate(data, shouldRevalidate) : swr.mutate() + // We are using `arguments` here because there's no other way around to + // tell if an argument is missing or `undefined`. It will be compiled to + // `arguments` anyways. + // @ts-ignore + 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 +260,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 +268,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 +297,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 () => { From 17acb47aa17450203d843b911a4beeb50f9599cc Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Sun, 15 May 2022 01:41:00 +0200 Subject: [PATCH 2/4] remove unused code --- _internal/utils/helper.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/_internal/utils/helper.ts b/_internal/utils/helper.ts index 98b0228b7..2d3694f6d 100644 --- a/_internal/utils/helper.ts +++ b/_internal/utils/helper.ts @@ -42,6 +42,3 @@ export const createCacheHelper = >( state[5] ] as const } - -// Extend a SWR object without triggering its getters. -export const extendSWR = () => {} From 56ead0636146905b41c1da570ac2e72ba6d2432d Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Sun, 15 May 2022 02:43:13 +0200 Subject: [PATCH 3/4] cast Promise --- infinite/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/infinite/index.ts b/infinite/index.ts index 3eaf44507..a2fc0cb67 100644 --- a/infinite/index.ts +++ b/infinite/index.ts @@ -30,7 +30,7 @@ import type { import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js' const INFINITE_PREFIX = '$inf$' -const EMPTY_PROMISE = Promise.resolve(UNDEFINED) +const EMPTY_PROMISE = Promise.resolve() as Promise const getFirstPageKey = (getKey: SWRInfiniteKeyLoader) => { return serialize(getKey ? getKey(0, null) : null)[0] From 921f93acbade800ba823f362eb0c8260a1f0c566 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Sun, 15 May 2022 02:45:36 +0200 Subject: [PATCH 4/4] remove ts-ignore --- infinite/index.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/infinite/index.ts b/infinite/index.ts index a2fc0cb67..9ed00cae4 100644 --- a/infinite/index.ts +++ b/infinite/index.ts @@ -220,10 +220,6 @@ export const infinite = ((useSWRNext: SWRHook) => } } - // We are using `arguments` here because there's no other way around to - // tell if an argument is missing or `undefined`. It will be compiled to - // `arguments` anyways. - // @ts-ignore return arguments.length ? swr.mutate(data, shouldRevalidate) : swr.mutate()