From 092dbe285fe020a4ab5a7fbe13dc41fdd40473b4 Mon Sep 17 00:00:00 2001 From: Ivo Maixner Date: Tue, 21 Jan 2025 17:05:08 +0100 Subject: [PATCH] Is loading (#316) * feat(return) add isLoading return value (#208) * feat(return) unset isLoading in cases not covered by tests (#208) --------- Co-authored-by: Adam DeHaven <2229946+adamdehaven@users.noreply.github.com> --- src/types.ts | 1 + src/use-swrv.ts | 10 +++++- tests/use-swrv.spec.tsx | 71 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/src/types.ts b/src/types.ts index 70c0220..2e6798e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -33,6 +33,7 @@ export interface IResponse { data: Ref error: Ref isValidating: Ref + isLoading: Ref mutate: (data?: fetcherFn, opts?: revalidateOptions) => Promise } diff --git a/src/use-swrv.ts b/src/use-swrv.ts index 25666d9..dde7190 100644 --- a/src/use-swrv.ts +++ b/src/use-swrv.ts @@ -34,7 +34,9 @@ import webPreset from './lib/web-preset' import SWRVCache from './cache' import { IConfig, IKey, IResponse, fetcherFn, revalidateOptions } from './types' -type StateRef = { data: Data, error: Error, isValidating: boolean, revalidate: Function, key: any }; +type StateRef = { + data: Data, error: Error, isValidating: boolean, isLoading: boolean, revalidate: Function, key: any +}; const DATA_CACHE = new SWRVCache>() const REF_CACHE = new SWRVCache[]>() @@ -131,6 +133,7 @@ const mutate = async (key: string, res: Promise | Data, cache = DATA } r.error = newData.error r.isValidating = newData.isValidating + r.isLoading = newData.isValidating const isLast = idx === refs.length - 1 if (!isLast) { @@ -228,6 +231,7 @@ function useSWRV (...args): IResponse { data: undefined, error: undefined, isValidating: true, + isLoading: true, key: null }) as StateRef } @@ -244,6 +248,7 @@ function useSWRV (...args): IResponse { const newData = cacheItem && cacheItem.data stateRef.isValidating = true + stateRef.isLoading = !newData if (newData) { stateRef.data = newData.data stateRef.error = newData.error @@ -256,6 +261,7 @@ function useSWRV (...args): IResponse { (opts?.forceRevalidate !== undefined && !opts?.forceRevalidate) ) { stateRef.isValidating = false + stateRef.isLoading = false return } @@ -267,6 +273,7 @@ function useSWRV (...args): IResponse { if (!shouldRevalidate) { stateRef.isValidating = false + stateRef.isLoading = false return } } @@ -282,6 +289,7 @@ function useSWRV (...args): IResponse { await mutate(keyVal, promiseFromCache.data, config.cache, ttl) } stateRef.isValidating = false + stateRef.isLoading = false PROMISES_CACHE.delete(keyVal) if (stateRef.error !== undefined) { const shouldRetryOnError = !unmounted && config.shouldRetryOnError && (opts ? opts.shouldRetryOnError : true) diff --git a/tests/use-swrv.spec.tsx b/tests/use-swrv.spec.tsx index 634db14..a28c441 100755 --- a/tests/use-swrv.spec.tsx +++ b/tests/use-swrv.spec.tsx @@ -799,6 +799,77 @@ describe('useSWRV - loading', () => { expect(wrapper.text()).toBe(':ready') }) + + it('should indicate cached data from another key with isLoading false', async () => { + const key = ref(1) + const wrapper = mount(defineComponent({ + template: `
data: {{ String(data) }}, isValidating: {{ isValidating }}, isLoading: {{ isLoading }}
+