Comparing version 1.1.0-beta.12 to 1.1.0
import * as revalidateEvents from './constants/revalidate-events'; | ||
export declare type FetcherResponse<Data = unknown> = Data | Promise<Data>; | ||
export declare type BareFetcher<Data = unknown> = (...args: any[]) => FetcherResponse<Data>; | ||
export declare type Fetcher<Data = unknown, SWRKey extends Key = Key> = SWRKey extends (() => readonly [...infer Args] | null | undefined | false) ? ((...args: [...Args]) => FetcherResponse<Data>) : SWRKey extends (readonly [...infer Args]) ? ((...args: [...Args]) => FetcherResponse<Data>) : SWRKey extends (() => infer Arg | null | undefined | false) ? (...args: [Arg]) => FetcherResponse<Data> : SWRKey extends null | undefined | false ? never : SWRKey extends (infer Arg) ? (...args: [Arg]) => FetcherResponse<Data> : never; | ||
export declare type Fetcher<Data = unknown, SWRKey extends Key = Key> = SWRKey extends () => readonly [...infer Args] | null | undefined | false ? (...args: [...Args]) => FetcherResponse<Data> : SWRKey extends readonly [...infer Args] ? (...args: [...Args]) => FetcherResponse<Data> : SWRKey extends () => infer Arg | null | undefined | false ? (...args: [Arg]) => FetcherResponse<Data> : SWRKey extends null | undefined | false ? never : SWRKey extends infer Arg ? (...args: [Arg]) => FetcherResponse<Data> : never; | ||
export interface InternalConfiguration { | ||
@@ -49,20 +49,7 @@ cache: Cache; | ||
<Data = any, Error = any, SWRKey extends Key = null>(key: SWRKey, config: SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>> | undefined): SWRResponse<Data, Error>; | ||
<Data = any, Error = any, SWRKey extends Key = null>(key: SWRKey, fetcher: Fetcher<Data, SWRKey>, config: SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>> | undefined): SWRResponse<Data, Error>; | ||
<Data = any, Error = any, SWRKey extends Key = null>(...args: [SWRKey] | [SWRKey, Fetcher<Data, SWRKey> | null] | [ | ||
SWRKey, | ||
SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>> | undefined | ||
] | [ | ||
SWRKey, | ||
Fetcher<Data, Key> | null, | ||
SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>> | undefined | ||
]): SWRResponse<Data, Error>; | ||
<Data = any, Error = any, SWRKey extends Key = null>(key: SWRKey, fetcher: Fetcher<Data, SWRKey> | null, config: SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>> | undefined): SWRResponse<Data, Error>; | ||
<Data = any, Error = any>(key: Key): SWRResponse<Data, Error>; | ||
<Data = any, Error = any>(key: Key, fetcher: BareFetcher<Data> | null): SWRResponse<Data, Error>; | ||
<Data = any, Error = any>(key: Key, config: SWRConfiguration<Data, Error, BareFetcher<Data>> | undefined): SWRResponse<Data, Error>; | ||
<Data = any, Error = any>(key: Key, fetcher: BareFetcher<Data>, config: SWRConfiguration<Data, Error, BareFetcher<Data>> | undefined): SWRResponse<Data, Error>; | ||
<Data = any, Error = any>(...args: [Key] | [Key, BareFetcher<Data> | null] | [Key, SWRConfiguration<Data, Error, BareFetcher<Data>> | undefined] | [ | ||
Key, | ||
BareFetcher<Data> | null, | ||
SWRConfiguration<Data, Error, BareFetcher<Data>> | undefined | ||
]): SWRResponse<Data, Error>; | ||
<Data = any, Error = any>(key: Key, fetcher: BareFetcher<Data> | null, config: SWRConfiguration<Data, Error, BareFetcher<Data>> | undefined): SWRResponse<Data, Error>; | ||
} | ||
@@ -69,0 +56,0 @@ export declare type Middleware = (useSWRNext: SWRHook) => <Data = any, Error = any>(key: Key, fetcher: BareFetcher<Data> | null, config: SWRConfiguration<Data, Error, BareFetcher<Data>>) => SWRResponse<Data, Error>; |
@@ -225,184 +225,186 @@ import { useEffect, useLayoutEffect, useState, useRef, useCallback } from 'react'; | ||
}; | ||
var infinite = (function (useSWRNext) { return function (getKey, fn, config) { | ||
var rerender = useState({})[1]; | ||
var didMountRef = useRef(false); | ||
var dataRef = useRef(); | ||
var cache = config.cache, _a = config.initialSize, initialSize = _a === void 0 ? 1 : _a, _b = config.revalidateAll, revalidateAll = _b === void 0 ? false : _b, _c = config.persistSize, persistSize = _c === void 0 ? false : _c, _d = config.revalidateFirstPage, revalidateFirstPage = _d === void 0 ? true : _d; | ||
// The serialized key of the first page. | ||
var firstPageKey = null; | ||
try { | ||
firstPageKey = getFirstPageKey(getKey); | ||
} | ||
catch (err) { | ||
// not ready | ||
} | ||
// We use cache to pass extra info (context) to fetcher so it can be globally | ||
// shared. The key of the context data is based on the first page key. | ||
var contextCacheKey = null; | ||
// Page size is also cached to share the page data between hooks with the | ||
// same key. | ||
var pageSizeCacheKey = null; | ||
if (firstPageKey) { | ||
contextCacheKey = '$ctx$' + firstPageKey; | ||
pageSizeCacheKey = '$len$' + firstPageKey; | ||
} | ||
var resolvePageSize = useCallback(function () { | ||
var cachedPageSize = cache.get(pageSizeCacheKey); | ||
return isUndefined(cachedPageSize) ? initialSize : cachedPageSize; | ||
// `cache` isn't allowed to change during the lifecycle | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [pageSizeCacheKey, initialSize]); | ||
// keep the last page size to restore it with the persistSize option | ||
var lastPageSizeRef = useRef(resolvePageSize()); | ||
// When the page key changes, we reset the page size if it's not persisted | ||
useIsomorphicLayoutEffect(function () { | ||
if (!didMountRef.current) { | ||
didMountRef.current = true; | ||
return; | ||
var infinite = (function (useSWRNext) { | ||
return function (getKey, fn, config) { | ||
var rerender = useState({})[1]; | ||
var didMountRef = useRef(false); | ||
var dataRef = useRef(); | ||
var cache = config.cache, _a = config.initialSize, initialSize = _a === void 0 ? 1 : _a, _b = config.revalidateAll, revalidateAll = _b === void 0 ? false : _b, _c = config.persistSize, persistSize = _c === void 0 ? false : _c, _d = config.revalidateFirstPage, revalidateFirstPage = _d === void 0 ? true : _d; | ||
// The serialized key of the first page. | ||
var firstPageKey = null; | ||
try { | ||
firstPageKey = getFirstPageKey(getKey); | ||
} | ||
catch (err) { | ||
// not ready | ||
} | ||
// We use cache to pass extra info (context) to fetcher so it can be globally | ||
// shared. The key of the context data is based on the first page key. | ||
var contextCacheKey = null; | ||
// Page size is also cached to share the page data between hooks with the | ||
// same key. | ||
var pageSizeCacheKey = null; | ||
if (firstPageKey) { | ||
// If the key has been changed, we keep the current page size if persistSize is enabled | ||
cache.set(pageSizeCacheKey, persistSize ? lastPageSizeRef.current : initialSize); | ||
contextCacheKey = '$ctx$' + firstPageKey; | ||
pageSizeCacheKey = '$len$' + firstPageKey; | ||
} | ||
// `initialSize` isn't allowed to change during the lifecycle | ||
var resolvePageSize = useCallback(function () { | ||
var cachedPageSize = cache.get(pageSizeCacheKey); | ||
return isUndefined(cachedPageSize) ? initialSize : cachedPageSize; | ||
// `cache` isn't allowed to change during the lifecycle | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [pageSizeCacheKey, initialSize]); | ||
// keep the last page size to restore it with the persistSize option | ||
var lastPageSizeRef = useRef(resolvePageSize()); | ||
// When the page key changes, we reset the page size if it's not persisted | ||
useIsomorphicLayoutEffect(function () { | ||
if (!didMountRef.current) { | ||
didMountRef.current = true; | ||
return; | ||
} | ||
if (firstPageKey) { | ||
// If the key has been changed, we keep the current page size if persistSize is enabled | ||
cache.set(pageSizeCacheKey, persistSize ? lastPageSizeRef.current : initialSize); | ||
} | ||
// `initialSize` isn't allowed to change during the lifecycle | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [firstPageKey]); | ||
// Actual SWR hook to load all pages in one fetcher. | ||
var swr = useSWRNext(firstPageKey ? INFINITE_PREFIX + firstPageKey : null, function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, forceRevalidateAll, originalData, data, pageSize, previousPageData, i, _b, pageKey, pageArgs, pageData, shouldFetchPage; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
_a = cache.get(contextCacheKey) || [], forceRevalidateAll = _a[0], originalData = _a[1]; | ||
data = []; | ||
pageSize = resolvePageSize(); | ||
previousPageData = null; | ||
i = 0; | ||
_c.label = 1; | ||
case 1: | ||
if (!(i < pageSize)) return [3 /*break*/, 5]; | ||
_b = serialize(getKey ? getKey(i, previousPageData) : null), pageKey = _b[0], pageArgs = _b[1]; | ||
if (!pageKey) { | ||
// `pageKey` is falsy, stop fetching new pages. | ||
return [3 /*break*/, 5]; | ||
} | ||
pageData = cache.get(pageKey); | ||
shouldFetchPage = revalidateAll || | ||
forceRevalidateAll || | ||
isUndefined(pageData) || | ||
(revalidateFirstPage && !i && !isUndefined(dataRef.current)) || | ||
(originalData && | ||
!isUndefined(originalData[i]) && | ||
!config.compare(originalData[i], pageData)); | ||
if (!(fn && shouldFetchPage)) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, fn.apply(void 0, pageArgs)]; | ||
case 2: | ||
pageData = _c.sent(); | ||
cache.set(pageKey, pageData); | ||
_c.label = 3; | ||
case 3: | ||
data.push(pageData); | ||
previousPageData = pageData; | ||
_c.label = 4; | ||
case 4: | ||
++i; | ||
return [3 /*break*/, 1]; | ||
case 5: | ||
// once we executed the data fetching based on the context, clear the context | ||
cache.delete(contextCacheKey); | ||
// return the data | ||
return [2 /*return*/, data]; | ||
} | ||
}); | ||
}); }, config); | ||
// update dataRef | ||
useIsomorphicLayoutEffect(function () { | ||
dataRef.current = swr.data; | ||
}, [swr.data]); | ||
var mutate = useCallback(function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var data = args[0]; | ||
// Default to true. | ||
var shouldRevalidate = args[1] !== false; | ||
// It is possible that the key is still falsy. | ||
if (!contextCacheKey) | ||
return; | ||
if (shouldRevalidate) { | ||
if (!isUndefined(data)) { | ||
// We only revalidate the pages that are changed | ||
var originalData = dataRef.current; | ||
cache.set(contextCacheKey, [false, originalData]); | ||
} | ||
else { | ||
// Calling `mutate()`, we revalidate all pages | ||
cache.set(contextCacheKey, [true]); | ||
} | ||
} | ||
return args.length ? swr.mutate(data, shouldRevalidate) : swr.mutate(); | ||
}, | ||
// swr.mutate is always the same reference | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [firstPageKey]); | ||
// Actual SWR hook to load all pages in one fetcher. | ||
var swr = useSWRNext(firstPageKey ? INFINITE_PREFIX + firstPageKey : null, function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, forceRevalidateAll, originalData, data, pageSize, previousPageData, i, _b, pageKey, pageArgs, pageData, shouldFetchPage; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
_a = cache.get(contextCacheKey) || [], forceRevalidateAll = _a[0], originalData = _a[1]; | ||
data = []; | ||
pageSize = resolvePageSize(); | ||
previousPageData = null; | ||
i = 0; | ||
_c.label = 1; | ||
case 1: | ||
if (!(i < pageSize)) return [3 /*break*/, 5]; | ||
_b = serialize(getKey ? getKey(i, previousPageData) : null), pageKey = _b[0], pageArgs = _b[1]; | ||
if (!pageKey) { | ||
// `pageKey` is falsy, stop fetching new pages. | ||
return [3 /*break*/, 5]; | ||
} | ||
pageData = cache.get(pageKey); | ||
shouldFetchPage = revalidateAll || | ||
forceRevalidateAll || | ||
isUndefined(pageData) || | ||
(revalidateFirstPage && !i && !isUndefined(dataRef.current)) || | ||
(originalData && | ||
!isUndefined(originalData[i]) && | ||
!config.compare(originalData[i], pageData)); | ||
if (!(fn && shouldFetchPage)) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, fn.apply(void 0, pageArgs)]; | ||
case 2: | ||
pageData = _c.sent(); | ||
cache.set(pageKey, pageData); | ||
_c.label = 3; | ||
case 3: | ||
data.push(pageData); | ||
previousPageData = pageData; | ||
_c.label = 4; | ||
case 4: | ||
++i; | ||
return [3 /*break*/, 1]; | ||
case 5: | ||
// once we executed the data fetching based on the context, clear the context | ||
cache.delete(contextCacheKey); | ||
// return the data | ||
return [2 /*return*/, data]; | ||
[contextCacheKey]); | ||
// Function to load pages data from the cache based on the page size. | ||
var resolvePagesFromCache = function (pageSize) { | ||
// return an array of page data | ||
var data = []; | ||
var previousPageData = null; | ||
for (var i = 0; i < pageSize; ++i) { | ||
var pageKey = serialize(getKey ? getKey(i, previousPageData) : null)[0]; | ||
// Get the cached page data. | ||
var pageData = pageKey ? cache.get(pageKey) : UNDEFINED; | ||
// Return the current data if we can't get it from the cache. | ||
if (isUndefined(pageData)) | ||
return dataRef.current; | ||
data.push(pageData); | ||
previousPageData = pageData; | ||
} | ||
}); | ||
}); }, config); | ||
// update dataRef | ||
useIsomorphicLayoutEffect(function () { | ||
dataRef.current = swr.data; | ||
}, [swr.data]); | ||
var mutate = useCallback(function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var data = args[0]; | ||
// Default to true. | ||
var shouldRevalidate = args[1] !== false; | ||
// It is possible that the key is still falsy. | ||
if (!contextCacheKey) | ||
return; | ||
if (shouldRevalidate) { | ||
if (!isUndefined(data)) { | ||
// We only revalidate the pages that are changed | ||
var originalData = dataRef.current; | ||
cache.set(contextCacheKey, [false, originalData]); | ||
// Return the data | ||
return data; | ||
}; | ||
// Extend the SWR API | ||
var setSize = useCallback(function (arg) { | ||
// It is possible that the key is still falsy. | ||
if (!pageSizeCacheKey) | ||
return; | ||
var size; | ||
if (isFunction(arg)) { | ||
size = arg(resolvePageSize()); | ||
} | ||
else { | ||
// Calling `mutate()`, we revalidate all pages | ||
cache.set(contextCacheKey, [true]); | ||
else if (typeof arg == 'number') { | ||
size = arg; | ||
} | ||
} | ||
return args.length ? swr.mutate(data, shouldRevalidate) : swr.mutate(); | ||
}, | ||
// swr.mutate is always the same reference | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[contextCacheKey]); | ||
// Function to load pages data from the cache based on the page size. | ||
var resolvePagesFromCache = function (pageSize) { | ||
// return an array of page data | ||
var data = []; | ||
var previousPageData = null; | ||
for (var i = 0; i < pageSize; ++i) { | ||
var pageKey = serialize(getKey ? getKey(i, previousPageData) : null)[0]; | ||
// Get the cached page data. | ||
var pageData = pageKey ? cache.get(pageKey) : UNDEFINED; | ||
// Return the current data if we can't get it from the cache. | ||
if (isUndefined(pageData)) | ||
return dataRef.current; | ||
data.push(pageData); | ||
previousPageData = pageData; | ||
} | ||
// Return the data | ||
return data; | ||
if (typeof size != 'number') | ||
return; | ||
cache.set(pageSizeCacheKey, size); | ||
lastPageSizeRef.current = size; | ||
rerender({}); | ||
return mutate(resolvePagesFromCache(size)); | ||
}, | ||
// `cache` and `rerender` isn't allowed to change during the lifecycle | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[pageSizeCacheKey, resolvePageSize, mutate]); | ||
// Use getter functions to avoid unnecessary re-renders caused by triggering | ||
// all the getters of the returned swr object. | ||
return { | ||
size: resolvePageSize(), | ||
setSize: setSize, | ||
mutate: mutate, | ||
get error() { | ||
return swr.error; | ||
}, | ||
get data() { | ||
return swr.data; | ||
}, | ||
get isValidating() { | ||
return swr.isValidating; | ||
} | ||
}; | ||
}; | ||
// Extend the SWR API | ||
var setSize = useCallback(function (arg) { | ||
// It is possible that the key is still falsy. | ||
if (!pageSizeCacheKey) | ||
return; | ||
var size; | ||
if (isFunction(arg)) { | ||
size = arg(resolvePageSize()); | ||
} | ||
else if (typeof arg == 'number') { | ||
size = arg; | ||
} | ||
if (typeof size != 'number') | ||
return; | ||
cache.set(pageSizeCacheKey, size); | ||
lastPageSizeRef.current = size; | ||
rerender({}); | ||
return mutate(resolvePagesFromCache(size)); | ||
}, | ||
// `cache` and `rerender` isn't allowed to change during the lifecycle | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[pageSizeCacheKey, resolvePageSize, mutate]); | ||
// Use getter functions to avoid unnecessary re-renders caused by triggering | ||
// all the getters of the returned swr object. | ||
return { | ||
size: resolvePageSize(), | ||
setSize: setSize, | ||
mutate: mutate, | ||
get error() { | ||
return swr.error; | ||
}, | ||
get data() { | ||
return swr.data; | ||
}, | ||
get isValidating() { | ||
return swr.isValidating; | ||
} | ||
}; | ||
}; }); | ||
}); | ||
var index = withMiddleware(useSWR, infinite); | ||
export { index as default, infinite, unstable_serialize }; |
@@ -231,182 +231,184 @@ Object.defineProperty(exports, '__esModule', { value: true }); | ||
}; | ||
var infinite = (function (useSWRNext) { return function (getKey, fn, config) { | ||
var rerender = react.useState({})[1]; | ||
var didMountRef = react.useRef(false); | ||
var dataRef = react.useRef(); | ||
var cache = config.cache, _a = config.initialSize, initialSize = _a === void 0 ? 1 : _a, _b = config.revalidateAll, revalidateAll = _b === void 0 ? false : _b, _c = config.persistSize, persistSize = _c === void 0 ? false : _c, _d = config.revalidateFirstPage, revalidateFirstPage = _d === void 0 ? true : _d; | ||
// The serialized key of the first page. | ||
var firstPageKey = null; | ||
try { | ||
firstPageKey = getFirstPageKey(getKey); | ||
} | ||
catch (err) { | ||
// not ready | ||
} | ||
// We use cache to pass extra info (context) to fetcher so it can be globally | ||
// shared. The key of the context data is based on the first page key. | ||
var contextCacheKey = null; | ||
// Page size is also cached to share the page data between hooks with the | ||
// same key. | ||
var pageSizeCacheKey = null; | ||
if (firstPageKey) { | ||
contextCacheKey = '$ctx$' + firstPageKey; | ||
pageSizeCacheKey = '$len$' + firstPageKey; | ||
} | ||
var resolvePageSize = react.useCallback(function () { | ||
var cachedPageSize = cache.get(pageSizeCacheKey); | ||
return isUndefined(cachedPageSize) ? initialSize : cachedPageSize; | ||
// `cache` isn't allowed to change during the lifecycle | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [pageSizeCacheKey, initialSize]); | ||
// keep the last page size to restore it with the persistSize option | ||
var lastPageSizeRef = react.useRef(resolvePageSize()); | ||
// When the page key changes, we reset the page size if it's not persisted | ||
useIsomorphicLayoutEffect(function () { | ||
if (!didMountRef.current) { | ||
didMountRef.current = true; | ||
return; | ||
var infinite = (function (useSWRNext) { | ||
return function (getKey, fn, config) { | ||
var rerender = react.useState({})[1]; | ||
var didMountRef = react.useRef(false); | ||
var dataRef = react.useRef(); | ||
var cache = config.cache, _a = config.initialSize, initialSize = _a === void 0 ? 1 : _a, _b = config.revalidateAll, revalidateAll = _b === void 0 ? false : _b, _c = config.persistSize, persistSize = _c === void 0 ? false : _c, _d = config.revalidateFirstPage, revalidateFirstPage = _d === void 0 ? true : _d; | ||
// The serialized key of the first page. | ||
var firstPageKey = null; | ||
try { | ||
firstPageKey = getFirstPageKey(getKey); | ||
} | ||
catch (err) { | ||
// not ready | ||
} | ||
// We use cache to pass extra info (context) to fetcher so it can be globally | ||
// shared. The key of the context data is based on the first page key. | ||
var contextCacheKey = null; | ||
// Page size is also cached to share the page data between hooks with the | ||
// same key. | ||
var pageSizeCacheKey = null; | ||
if (firstPageKey) { | ||
// If the key has been changed, we keep the current page size if persistSize is enabled | ||
cache.set(pageSizeCacheKey, persistSize ? lastPageSizeRef.current : initialSize); | ||
contextCacheKey = '$ctx$' + firstPageKey; | ||
pageSizeCacheKey = '$len$' + firstPageKey; | ||
} | ||
// `initialSize` isn't allowed to change during the lifecycle | ||
var resolvePageSize = react.useCallback(function () { | ||
var cachedPageSize = cache.get(pageSizeCacheKey); | ||
return isUndefined(cachedPageSize) ? initialSize : cachedPageSize; | ||
// `cache` isn't allowed to change during the lifecycle | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [pageSizeCacheKey, initialSize]); | ||
// keep the last page size to restore it with the persistSize option | ||
var lastPageSizeRef = react.useRef(resolvePageSize()); | ||
// When the page key changes, we reset the page size if it's not persisted | ||
useIsomorphicLayoutEffect(function () { | ||
if (!didMountRef.current) { | ||
didMountRef.current = true; | ||
return; | ||
} | ||
if (firstPageKey) { | ||
// If the key has been changed, we keep the current page size if persistSize is enabled | ||
cache.set(pageSizeCacheKey, persistSize ? lastPageSizeRef.current : initialSize); | ||
} | ||
// `initialSize` isn't allowed to change during the lifecycle | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [firstPageKey]); | ||
// Actual SWR hook to load all pages in one fetcher. | ||
var swr = useSWRNext(firstPageKey ? INFINITE_PREFIX + firstPageKey : null, function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, forceRevalidateAll, originalData, data, pageSize, previousPageData, i, _b, pageKey, pageArgs, pageData, shouldFetchPage; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
_a = cache.get(contextCacheKey) || [], forceRevalidateAll = _a[0], originalData = _a[1]; | ||
data = []; | ||
pageSize = resolvePageSize(); | ||
previousPageData = null; | ||
i = 0; | ||
_c.label = 1; | ||
case 1: | ||
if (!(i < pageSize)) return [3 /*break*/, 5]; | ||
_b = serialize(getKey ? getKey(i, previousPageData) : null), pageKey = _b[0], pageArgs = _b[1]; | ||
if (!pageKey) { | ||
// `pageKey` is falsy, stop fetching new pages. | ||
return [3 /*break*/, 5]; | ||
} | ||
pageData = cache.get(pageKey); | ||
shouldFetchPage = revalidateAll || | ||
forceRevalidateAll || | ||
isUndefined(pageData) || | ||
(revalidateFirstPage && !i && !isUndefined(dataRef.current)) || | ||
(originalData && | ||
!isUndefined(originalData[i]) && | ||
!config.compare(originalData[i], pageData)); | ||
if (!(fn && shouldFetchPage)) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, fn.apply(void 0, pageArgs)]; | ||
case 2: | ||
pageData = _c.sent(); | ||
cache.set(pageKey, pageData); | ||
_c.label = 3; | ||
case 3: | ||
data.push(pageData); | ||
previousPageData = pageData; | ||
_c.label = 4; | ||
case 4: | ||
++i; | ||
return [3 /*break*/, 1]; | ||
case 5: | ||
// once we executed the data fetching based on the context, clear the context | ||
cache.delete(contextCacheKey); | ||
// return the data | ||
return [2 /*return*/, data]; | ||
} | ||
}); | ||
}); }, config); | ||
// update dataRef | ||
useIsomorphicLayoutEffect(function () { | ||
dataRef.current = swr.data; | ||
}, [swr.data]); | ||
var mutate = react.useCallback(function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var data = args[0]; | ||
// Default to true. | ||
var shouldRevalidate = args[1] !== false; | ||
// It is possible that the key is still falsy. | ||
if (!contextCacheKey) | ||
return; | ||
if (shouldRevalidate) { | ||
if (!isUndefined(data)) { | ||
// We only revalidate the pages that are changed | ||
var originalData = dataRef.current; | ||
cache.set(contextCacheKey, [false, originalData]); | ||
} | ||
else { | ||
// Calling `mutate()`, we revalidate all pages | ||
cache.set(contextCacheKey, [true]); | ||
} | ||
} | ||
return args.length ? swr.mutate(data, shouldRevalidate) : swr.mutate(); | ||
}, | ||
// swr.mutate is always the same reference | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [firstPageKey]); | ||
// Actual SWR hook to load all pages in one fetcher. | ||
var swr = useSWRNext(firstPageKey ? INFINITE_PREFIX + firstPageKey : null, function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, forceRevalidateAll, originalData, data, pageSize, previousPageData, i, _b, pageKey, pageArgs, pageData, shouldFetchPage; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
_a = cache.get(contextCacheKey) || [], forceRevalidateAll = _a[0], originalData = _a[1]; | ||
data = []; | ||
pageSize = resolvePageSize(); | ||
previousPageData = null; | ||
i = 0; | ||
_c.label = 1; | ||
case 1: | ||
if (!(i < pageSize)) return [3 /*break*/, 5]; | ||
_b = serialize(getKey ? getKey(i, previousPageData) : null), pageKey = _b[0], pageArgs = _b[1]; | ||
if (!pageKey) { | ||
// `pageKey` is falsy, stop fetching new pages. | ||
return [3 /*break*/, 5]; | ||
} | ||
pageData = cache.get(pageKey); | ||
shouldFetchPage = revalidateAll || | ||
forceRevalidateAll || | ||
isUndefined(pageData) || | ||
(revalidateFirstPage && !i && !isUndefined(dataRef.current)) || | ||
(originalData && | ||
!isUndefined(originalData[i]) && | ||
!config.compare(originalData[i], pageData)); | ||
if (!(fn && shouldFetchPage)) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, fn.apply(void 0, pageArgs)]; | ||
case 2: | ||
pageData = _c.sent(); | ||
cache.set(pageKey, pageData); | ||
_c.label = 3; | ||
case 3: | ||
data.push(pageData); | ||
previousPageData = pageData; | ||
_c.label = 4; | ||
case 4: | ||
++i; | ||
return [3 /*break*/, 1]; | ||
case 5: | ||
// once we executed the data fetching based on the context, clear the context | ||
cache.delete(contextCacheKey); | ||
// return the data | ||
return [2 /*return*/, data]; | ||
[contextCacheKey]); | ||
// Function to load pages data from the cache based on the page size. | ||
var resolvePagesFromCache = function (pageSize) { | ||
// return an array of page data | ||
var data = []; | ||
var previousPageData = null; | ||
for (var i = 0; i < pageSize; ++i) { | ||
var pageKey = serialize(getKey ? getKey(i, previousPageData) : null)[0]; | ||
// Get the cached page data. | ||
var pageData = pageKey ? cache.get(pageKey) : UNDEFINED; | ||
// Return the current data if we can't get it from the cache. | ||
if (isUndefined(pageData)) | ||
return dataRef.current; | ||
data.push(pageData); | ||
previousPageData = pageData; | ||
} | ||
}); | ||
}); }, config); | ||
// update dataRef | ||
useIsomorphicLayoutEffect(function () { | ||
dataRef.current = swr.data; | ||
}, [swr.data]); | ||
var mutate = react.useCallback(function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var data = args[0]; | ||
// Default to true. | ||
var shouldRevalidate = args[1] !== false; | ||
// It is possible that the key is still falsy. | ||
if (!contextCacheKey) | ||
return; | ||
if (shouldRevalidate) { | ||
if (!isUndefined(data)) { | ||
// We only revalidate the pages that are changed | ||
var originalData = dataRef.current; | ||
cache.set(contextCacheKey, [false, originalData]); | ||
// Return the data | ||
return data; | ||
}; | ||
// Extend the SWR API | ||
var setSize = react.useCallback(function (arg) { | ||
// It is possible that the key is still falsy. | ||
if (!pageSizeCacheKey) | ||
return; | ||
var size; | ||
if (isFunction(arg)) { | ||
size = arg(resolvePageSize()); | ||
} | ||
else { | ||
// Calling `mutate()`, we revalidate all pages | ||
cache.set(contextCacheKey, [true]); | ||
else if (typeof arg == 'number') { | ||
size = arg; | ||
} | ||
} | ||
return args.length ? swr.mutate(data, shouldRevalidate) : swr.mutate(); | ||
}, | ||
// swr.mutate is always the same reference | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[contextCacheKey]); | ||
// Function to load pages data from the cache based on the page size. | ||
var resolvePagesFromCache = function (pageSize) { | ||
// return an array of page data | ||
var data = []; | ||
var previousPageData = null; | ||
for (var i = 0; i < pageSize; ++i) { | ||
var pageKey = serialize(getKey ? getKey(i, previousPageData) : null)[0]; | ||
// Get the cached page data. | ||
var pageData = pageKey ? cache.get(pageKey) : UNDEFINED; | ||
// Return the current data if we can't get it from the cache. | ||
if (isUndefined(pageData)) | ||
return dataRef.current; | ||
data.push(pageData); | ||
previousPageData = pageData; | ||
} | ||
// Return the data | ||
return data; | ||
if (typeof size != 'number') | ||
return; | ||
cache.set(pageSizeCacheKey, size); | ||
lastPageSizeRef.current = size; | ||
rerender({}); | ||
return mutate(resolvePagesFromCache(size)); | ||
}, | ||
// `cache` and `rerender` isn't allowed to change during the lifecycle | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[pageSizeCacheKey, resolvePageSize, mutate]); | ||
// Use getter functions to avoid unnecessary re-renders caused by triggering | ||
// all the getters of the returned swr object. | ||
return { | ||
size: resolvePageSize(), | ||
setSize: setSize, | ||
mutate: mutate, | ||
get error() { | ||
return swr.error; | ||
}, | ||
get data() { | ||
return swr.data; | ||
}, | ||
get isValidating() { | ||
return swr.isValidating; | ||
} | ||
}; | ||
}; | ||
// Extend the SWR API | ||
var setSize = react.useCallback(function (arg) { | ||
// It is possible that the key is still falsy. | ||
if (!pageSizeCacheKey) | ||
return; | ||
var size; | ||
if (isFunction(arg)) { | ||
size = arg(resolvePageSize()); | ||
} | ||
else if (typeof arg == 'number') { | ||
size = arg; | ||
} | ||
if (typeof size != 'number') | ||
return; | ||
cache.set(pageSizeCacheKey, size); | ||
lastPageSizeRef.current = size; | ||
rerender({}); | ||
return mutate(resolvePagesFromCache(size)); | ||
}, | ||
// `cache` and `rerender` isn't allowed to change during the lifecycle | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
[pageSizeCacheKey, resolvePageSize, mutate]); | ||
// Use getter functions to avoid unnecessary re-renders caused by triggering | ||
// all the getters of the returned swr object. | ||
return { | ||
size: resolvePageSize(), | ||
setSize: setSize, | ||
mutate: mutate, | ||
get error() { | ||
return swr.error; | ||
}, | ||
get data() { | ||
return swr.data; | ||
}, | ||
get isValidating() { | ||
return swr.isValidating; | ||
} | ||
}; | ||
}; }); | ||
}); | ||
var index = withMiddleware(useSWR__default['default'], infinite); | ||
@@ -413,0 +415,0 @@ |
import { SWRConfiguration, SWRResponse, Arguments, BareFetcher } from 'swr'; | ||
declare type FetcherResponse<Data = unknown> = Data | Promise<Data>; | ||
export declare type InfiniteFetcher<Data = any, KeyLoader extends InfiniteKeyLoader = InfiniteKeyLoader> = KeyLoader extends (...args: any[]) => any ? ReturnType<KeyLoader> extends (readonly [...infer K] | null | false | undefined) ? ((...args: [...K]) => FetcherResponse<Data>) : ReturnType<KeyLoader> extends (infer T | null | false | undefined) ? (...args: [T]) => FetcherResponse<Data> : never : never; | ||
export declare type InfiniteFetcher<Data = any, KeyLoader extends InfiniteKeyLoader = InfiniteKeyLoader> = KeyLoader extends (...args: any[]) => any ? ReturnType<KeyLoader> extends readonly [...infer K] | null | false | undefined ? (...args: [...K]) => FetcherResponse<Data> : ReturnType<KeyLoader> extends infer T | null | false | undefined ? (...args: [T]) => FetcherResponse<Data> : never : never; | ||
export declare type InfiniteKeyLoader = (index: number, previousPageData: any | null) => Arguments; | ||
@@ -5,0 +5,0 @@ export interface SWRInfiniteConfiguration<Data = any, Error = any, Fn extends InfiniteFetcher<Data> = BareFetcher<Data>> extends SWRConfiguration<Data[], Error> { |
{ | ||
"name": "swr", | ||
"version": "1.1.0-beta.12", | ||
"version": "1.1.0", | ||
"description": "React Hooks library for remote data fetching", | ||
@@ -78,11 +78,11 @@ "main": "./dist/index.js", | ||
"@types/react": "17.0.20", | ||
"@typescript-eslint/eslint-plugin": "4.31.0", | ||
"@typescript-eslint/parser": "4.31.0", | ||
"@typescript-eslint/eslint-plugin": "5.4.0", | ||
"@typescript-eslint/parser": "5.4.0", | ||
"bunchee": "1.7.1", | ||
"eslint": "7.32.0", | ||
"eslint-config-prettier": "6.5.0", | ||
"eslint-plugin-jest-dom": "3.6.5", | ||
"eslint-plugin-react": "7.20.6", | ||
"eslint-plugin-react-hooks": "4.1.2", | ||
"eslint-plugin-testing-library": "3.10.1", | ||
"eslint": "8.3.0", | ||
"eslint-config-prettier": "8.3.0", | ||
"eslint-plugin-jest-dom": "3.9.2", | ||
"eslint-plugin-react": "7.27.1", | ||
"eslint-plugin-react-hooks": "4.3.0", | ||
"eslint-plugin-testing-library": "5.0.0", | ||
"husky": "2.4.1", | ||
@@ -92,3 +92,3 @@ "jest": "27.0.6", | ||
"npm-run-all": "4.1.5", | ||
"prettier": "1.18.2", | ||
"prettier": "2.5.0", | ||
"react": "17.0.1", | ||
@@ -95,0 +95,0 @@ "react-dom": "17.0.1", |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
158590
0
3135