Comparing version 1.2.2 to 1.3.0
@@ -281,3 +281,3 @@ import { useEffect, useLayoutEffect, createContext, useContext, useState, createElement, useRef, useCallback, useDebugValue } from 'react'; | ||
return __awaiter(void 0, void 0, void 0, function () { | ||
var cache, _key, _data, _opts, options, populateCache, revalidate, rollbackOnError, optimisticData, _a, key, keyInfo, _b, MUTATION, data, error, beforeMutationTs, hasOptimisticData, rollbackData, res; | ||
var cache, _key, _data, _opts, options, populateCache, revalidate, rollbackOnError, customOptimisticData, _a, key, keyInfo, _b, MUTATION, data, error, beforeMutationTs, hasCustomOptimisticData, rollbackData, optimisticData, res; | ||
return __generator(this, function (_c) { | ||
@@ -293,3 +293,3 @@ switch (_c.label) { | ||
rollbackOnError = options.rollbackOnError !== false; | ||
optimisticData = options.optimisticData; | ||
customOptimisticData = options.optimisticData; | ||
_a = serialize(_key), key = _a[0], keyInfo = _a[2]; | ||
@@ -307,6 +307,9 @@ if (!key) | ||
MUTATION[key] = [beforeMutationTs, 0]; | ||
hasOptimisticData = !isUndefined(optimisticData); | ||
hasCustomOptimisticData = !isUndefined(customOptimisticData); | ||
rollbackData = cache.get(key); | ||
// Do optimistic data update. | ||
if (hasOptimisticData) { | ||
if (hasCustomOptimisticData) { | ||
optimisticData = isFunction(customOptimisticData) | ||
? customOptimisticData(rollbackData) | ||
: customOptimisticData; | ||
cache.set(key, optimisticData); | ||
@@ -345,3 +348,3 @@ broadcastState(cache, key, optimisticData); | ||
} | ||
else if (error && hasOptimisticData && rollbackOnError) { | ||
else if (error && hasCustomOptimisticData && rollbackOnError) { | ||
// Rollback. Always populate the cache in this case but without | ||
@@ -675,9 +678,10 @@ // transforming the data. | ||
return false; | ||
return suspense | ||
? // Under suspense mode, it will always fetch on render if there is no | ||
// stale data so no need to revalidate immediately on mount again. | ||
!isUndefined(data) | ||
: // If there is no stale data, we need to revalidate on mount; | ||
// If `revalidateIfStale` is set to true, we will always revalidate. | ||
isUndefined(data) || config.revalidateIfStale; | ||
// Under suspense mode, it will always fetch on render if there is no | ||
// stale data so no need to revalidate immediately on mount again. | ||
// If data exists, only revalidate if `revalidateIfStale` is true. | ||
if (suspense) | ||
return isUndefined(data) ? false : config.revalidateIfStale; | ||
// If there is no stale data, we need to revalidate on mount; | ||
// If `revalidateIfStale` is set to true, we will always revalidate. | ||
return isUndefined(data) || config.revalidateIfStale; | ||
}; | ||
@@ -684,0 +688,0 @@ // Resolve the current validating state. |
@@ -283,3 +283,3 @@ Object.defineProperty(exports, '__esModule', { value: true }); | ||
return __awaiter(void 0, void 0, void 0, function () { | ||
var cache, _key, _data, _opts, options, populateCache, revalidate, rollbackOnError, optimisticData, _a, key, keyInfo, _b, MUTATION, data, error, beforeMutationTs, hasOptimisticData, rollbackData, res; | ||
var cache, _key, _data, _opts, options, populateCache, revalidate, rollbackOnError, customOptimisticData, _a, key, keyInfo, _b, MUTATION, data, error, beforeMutationTs, hasCustomOptimisticData, rollbackData, optimisticData, res; | ||
return __generator(this, function (_c) { | ||
@@ -295,3 +295,3 @@ switch (_c.label) { | ||
rollbackOnError = options.rollbackOnError !== false; | ||
optimisticData = options.optimisticData; | ||
customOptimisticData = options.optimisticData; | ||
_a = serialize(_key), key = _a[0], keyInfo = _a[2]; | ||
@@ -309,6 +309,9 @@ if (!key) | ||
MUTATION[key] = [beforeMutationTs, 0]; | ||
hasOptimisticData = !isUndefined(optimisticData); | ||
hasCustomOptimisticData = !isUndefined(customOptimisticData); | ||
rollbackData = cache.get(key); | ||
// Do optimistic data update. | ||
if (hasOptimisticData) { | ||
if (hasCustomOptimisticData) { | ||
optimisticData = isFunction(customOptimisticData) | ||
? customOptimisticData(rollbackData) | ||
: customOptimisticData; | ||
cache.set(key, optimisticData); | ||
@@ -347,3 +350,3 @@ broadcastState(cache, key, optimisticData); | ||
} | ||
else if (error && hasOptimisticData && rollbackOnError) { | ||
else if (error && hasCustomOptimisticData && rollbackOnError) { | ||
// Rollback. Always populate the cache in this case but without | ||
@@ -677,9 +680,10 @@ // transforming the data. | ||
return false; | ||
return suspense | ||
? // Under suspense mode, it will always fetch on render if there is no | ||
// stale data so no need to revalidate immediately on mount again. | ||
!isUndefined(data) | ||
: // If there is no stale data, we need to revalidate on mount; | ||
// If `revalidateIfStale` is set to true, we will always revalidate. | ||
isUndefined(data) || config.revalidateIfStale; | ||
// Under suspense mode, it will always fetch on render if there is no | ||
// stale data so no need to revalidate immediately on mount again. | ||
// If data exists, only revalidate if `revalidateIfStale` is true. | ||
if (suspense) | ||
return isUndefined(data) ? false : config.revalidateIfStale; | ||
// If there is no stale data, we need to revalidate on mount; | ||
// If `revalidateIfStale` is set to true, we will always revalidate. | ||
return isUndefined(data) || config.revalidateIfStale; | ||
}; | ||
@@ -686,0 +690,0 @@ // Resolve the current validating state. |
@@ -63,3 +63,3 @@ import * as revalidateEvents from './constants/revalidate-events'; | ||
populateCache?: boolean | ((result: any, currentData: Data) => Data); | ||
optimisticData?: Data; | ||
optimisticData?: Data | ((currentData?: Data) => Data); | ||
rollbackOnError?: boolean; | ||
@@ -66,0 +66,0 @@ }; |
@@ -5,7 +5,7 @@ /// <reference types="react" /> | ||
export declare const useSWRHandler: <Data = any, Error_1 = any>(_key: Key, fetcher: ((args_0: string) => import("./types").FetcherResponse<Data>) | ((args_0: any, ...args_1: unknown[]) => import("./types").FetcherResponse<Data>) | ((args_0: Record<any, any>) => import("./types").FetcherResponse<Data>) | ((args_0: string | [any, ...unknown[]] | readonly [any, ...unknown[]] | Record<any, any>) => import("./types").FetcherResponse<Data>) | null, config: import("./types").InternalConfiguration & import("./types").PublicConfiguration<any, any, import("./types").BareFetcher<unknown>> & Partial<import("./types").PublicConfiguration<Data, Error_1, import("./types").BareFetcher<any>>>) => SWRResponse<Data, Error_1>; | ||
export declare const SWRConfig: import("react").FC<{ | ||
export declare const SWRConfig: import("react").FC<import("react").PropsWithChildren<{ | ||
value?: (Partial<import("./types").PublicConfiguration<any, any, import("./types").BareFetcher<any>>> & Partial<import("./types").ProviderConfiguration> & { | ||
provider?: ((cache: Readonly<import("./types").Cache<any>>) => import("./types").Cache<any>) | undefined; | ||
}) | undefined; | ||
}> & { | ||
}>> & { | ||
default: FullConfiguration; | ||
@@ -12,0 +12,0 @@ }; |
@@ -1,9 +0,9 @@ | ||
import { FC } from 'react'; | ||
import { FC, PropsWithChildren } from 'react'; | ||
import { SWRConfiguration, FullConfiguration, ProviderConfiguration, Cache } from '../types'; | ||
export declare const SWRConfigContext: import("react").Context<Partial<FullConfiguration>>; | ||
declare const SWRConfig: FC<{ | ||
declare const SWRConfig: FC<PropsWithChildren<{ | ||
value?: SWRConfiguration & Partial<ProviderConfiguration> & { | ||
provider?: (cache: Readonly<Cache>) => Cache; | ||
}; | ||
}>; | ||
}>>; | ||
export default SWRConfig; |
{ | ||
"name": "swr", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "React Hooks library for remote data fetching", | ||
@@ -105,4 +105,4 @@ "keywords": [ | ||
"react-dom": "17.0.1", | ||
"react-dom-experimental": "npm:react-dom@alpha", | ||
"react-experimental": "npm:react@alpha", | ||
"react-dom-18": "npm:react-dom@18", | ||
"react-18": "npm:react@18", | ||
"rimraf": "3.0.2", | ||
@@ -109,0 +109,0 @@ "swr": "link:./", |
Sorry, the diff of this file is not supported yet
230689
4654