Comparing version 2.0.4 to 2.1.0
@@ -202,8 +202,8 @@ import react, { FC, PropsWithChildren, MutableRefObject } from 'react'; | ||
<Data = any, Error = any, SWRKey extends Key = StrictKey>(key: SWRKey, fetcher: Fetcher<Data, SWRKey> | null): SWRResponse<Data, Error>; | ||
<Data = any, Error = any, SWRKey extends Key = StrictKey, SWROptions extends SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>> | undefined = SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>> | undefined>(key: SWRKey, config: SWROptions): SWRResponse<Data, Error, SWROptions>; | ||
<Data = any, Error = any, SWRKey extends Key = StrictKey, SWROptions extends SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>> | undefined = SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>> | undefined>(key: SWRKey, fetcher: Fetcher<Data, SWRKey> | null, config: SWROptions): SWRResponse<Data, Error, SWROptions>; | ||
<Data = any, Error = any, SWRKey extends Key = StrictKey, SWROptions extends SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>> | undefined = SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>>>(key: SWRKey, config: SWROptions | undefined): SWRResponse<Data, Error, Required<SWROptions>>; | ||
<Data = any, Error = any, SWRKey extends Key = StrictKey, SWROptions extends SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>> | undefined = SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>>>(key: SWRKey, fetcher: Fetcher<Data, SWRKey> | null, config: SWROptions | undefined): SWRResponse<Data, Error, Required<SWROptions>>; | ||
<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, SWROptions extends SWRConfiguration<Data, Error, BareFetcher<Data>> | undefined = SWRConfiguration<Data, Error, BareFetcher<Data>> | undefined>(key: Key, config: SWROptions): SWRResponse<Data, Error, SWROptions>; | ||
<Data = any, Error = any, SWROptions extends SWRConfiguration<Data, Error, BareFetcher<Data>> | undefined = SWRConfiguration<Data, Error, BareFetcher<Data>> | undefined>(key: Key, fetcher: BareFetcher<Data> | null, config: SWROptions): SWRResponse<Data, Error, SWROptions>; | ||
<Data = any, Error = any, SWROptions extends SWRConfiguration<Data, Error, BareFetcher<Data>> | undefined = SWRConfiguration<Data, Error, BareFetcher<Data>>>(key: Key, config: SWROptions | undefined): SWRResponse<Data, Error, Required<SWROptions>>; | ||
<Data = any, Error = any, SWROptions extends SWRConfiguration<Data, Error, BareFetcher<Data>> | undefined = SWRConfiguration<Data, Error, BareFetcher<Data>>>(key: Key, fetcher: BareFetcher<Data> | null, config: SWROptions | undefined): SWRResponse<Data, Error, Required<SWROptions>>; | ||
} | ||
@@ -210,0 +210,0 @@ type Middleware = (useSWRNext: SWRHook) => <Data = any, Error = any>(key: Key, fetcher: BareFetcher<Data> | null, config: typeof defaultConfig & SWRConfiguration<Data, Error, BareFetcher<Data>>) => SWRResponse<Data, Error>; |
@@ -1,2 +0,2 @@ | ||
import React, { useEffect, useLayoutEffect, createContext, useContext, useMemo, useState, createElement, useRef, useCallback } from 'react'; | ||
import React, { useEffect, useLayoutEffect, createContext, useContext, useMemo, useRef, createElement, useState, useCallback } from 'react'; | ||
@@ -251,4 +251,4 @@ // Global state used to deduplicate requests and store listeners | ||
const key = keyIt.value; | ||
if (// Skip the special useSWRInfinite keys. | ||
!key.startsWith('$inf$') && keyFilter(cache.get(key)._k)) { | ||
if (// Skip the special useSWRInfinite and useSWRSubscription keys. | ||
!/^\$(inf|sub)\$/.test(key) && keyFilter(cache.get(key)._k)) { | ||
matchedKeys.push(key); | ||
@@ -535,4 +535,8 @@ } | ||
const provider = config && config.provider; | ||
// Use a lazy initialized state to create the cache on first access. | ||
const [cacheContext] = useState(()=>provider ? initCache(provider(extendedConfig.cache || cache), config) : UNDEFINED); | ||
// initialize the cache only on first access. | ||
const cacheContextRef = useRef(UNDEFINED); | ||
if (provider && !cacheContextRef.current) { | ||
cacheContextRef.current = initCache(provider(extendedConfig.cache || cache), config); | ||
} | ||
const cacheContext = cacheContextRef.current; | ||
// Override the cache if a new provider is given. | ||
@@ -539,0 +543,0 @@ if (cacheContext) { |
@@ -257,4 +257,4 @@ Object.defineProperty(exports, '__esModule', { value: true }); | ||
const key = keyIt.value; | ||
if (// Skip the special useSWRInfinite keys. | ||
!key.startsWith('$inf$') && keyFilter(cache.get(key)._k)) { | ||
if (// Skip the special useSWRInfinite and useSWRSubscription keys. | ||
!/^\$(inf|sub)\$/.test(key) && keyFilter(cache.get(key)._k)) { | ||
matchedKeys.push(key); | ||
@@ -541,4 +541,8 @@ } | ||
const provider = config && config.provider; | ||
// Use a lazy initialized state to create the cache on first access. | ||
const [cacheContext] = React.useState(()=>provider ? initCache(provider(extendedConfig.cache || cache), config) : UNDEFINED); | ||
// initialize the cache only on first access. | ||
const cacheContextRef = React.useRef(UNDEFINED); | ||
if (provider && !cacheContextRef.current) { | ||
cacheContextRef.current = initCache(provider(extendedConfig.cache || cache), config); | ||
} | ||
const cacheContext = cacheContextRef.current; | ||
// Override the cache if a new provider is given. | ||
@@ -545,0 +549,0 @@ if (cacheContext) { |
@@ -80,7 +80,10 @@ import { useRef, useMemo, useCallback, useDebugValue } from 'react'; | ||
}; | ||
const cachedData = getCache(); | ||
const initialData = getInitialCache(); | ||
const clientSnapshot = getSelectedCache(cachedData); | ||
const serverSnapshot = cachedData === initialData ? clientSnapshot : getSelectedCache(initialData); | ||
// To make sure that we are returning the same object reference to avoid | ||
// unnecessary re-renders, we keep the previous snapshot and use deep | ||
// comparison to check if we need to return a new one. | ||
let memorizedSnapshot = getSelectedCache(getCache()); | ||
const memorizedInitialSnapshot = getSelectedCache(getInitialCache()); | ||
let memorizedSnapshot = clientSnapshot; | ||
return [ | ||
@@ -91,3 +94,3 @@ ()=>{ | ||
}, | ||
()=>memorizedInitialSnapshot | ||
()=>serverSnapshot | ||
]; | ||
@@ -94,0 +97,0 @@ // eslint-disable-next-line react-hooks/exhaustive-deps |
@@ -81,7 +81,10 @@ Object.defineProperty(exports, '__esModule', { value: true }); | ||
}; | ||
const cachedData = getCache(); | ||
const initialData = getInitialCache(); | ||
const clientSnapshot = getSelectedCache(cachedData); | ||
const serverSnapshot = cachedData === initialData ? clientSnapshot : getSelectedCache(initialData); | ||
// To make sure that we are returning the same object reference to avoid | ||
// unnecessary re-renders, we keep the previous snapshot and use deep | ||
// comparison to check if we need to return a new one. | ||
let memorizedSnapshot = getSelectedCache(getCache()); | ||
const memorizedInitialSnapshot = getSelectedCache(getInitialCache()); | ||
let memorizedSnapshot = clientSnapshot; | ||
return [ | ||
@@ -92,3 +95,3 @@ ()=>{ | ||
}, | ||
()=>memorizedInitialSnapshot | ||
()=>serverSnapshot | ||
]; | ||
@@ -95,0 +98,0 @@ // eslint-disable-next-line react-hooks/exhaustive-deps |
@@ -19,3 +19,3 @@ import { useRef, useCallback } from 'react'; | ||
const dataRef = useRef(); | ||
const { cache , initialSize =1 , revalidateAll =false , persistSize =false , revalidateFirstPage =true , revalidateOnMount =false } = config; | ||
const { cache , initialSize =1 , revalidateAll =false , persistSize =false , revalidateFirstPage =true , revalidateOnMount =false , parallel =false } = config; | ||
// The serialized key of the first page. This key will be used to store | ||
@@ -89,5 +89,6 @@ // metadata of this SWR infinite hook. | ||
const pageSize = resolvePageSize(); | ||
const revalidators = []; | ||
let previousPageData = null; | ||
for(let i = 0; i < pageSize; ++i){ | ||
const [pageKey, pageArg] = serialize(getKey(i, previousPageData)); | ||
const [pageKey, pageArg] = serialize(getKey(i, parallel ? null : previousPageData)); | ||
if (!pageKey) { | ||
@@ -108,11 +109,26 @@ break; | ||
if (fn && shouldFetchPage) { | ||
pageData = await fn(pageArg); | ||
setSWRCache({ | ||
data: pageData, | ||
_k: pageArg | ||
}); | ||
const revalidate = async ()=>{ | ||
pageData = await fn(pageArg); | ||
setSWRCache({ | ||
data: pageData, | ||
_k: pageArg | ||
}); | ||
data[i] = pageData; | ||
}; | ||
if (parallel) { | ||
revalidators.push(revalidate); | ||
} else { | ||
await revalidate(); | ||
} | ||
} else { | ||
data[i] = pageData; | ||
} | ||
data.push(pageData); | ||
previousPageData = pageData; | ||
if (!parallel) { | ||
previousPageData = pageData; | ||
} | ||
} | ||
// flush all revalidateions in parallel | ||
if (parallel) { | ||
await Promise.all(revalidators.map((r)=>r())); | ||
} | ||
// once we executed the data fetching based on the context, clear the context | ||
@@ -119,0 +135,0 @@ set({ |
@@ -25,3 +25,3 @@ Object.defineProperty(exports, '__esModule', { value: true }); | ||
const dataRef = react.useRef(); | ||
const { cache , initialSize =1 , revalidateAll =false , persistSize =false , revalidateFirstPage =true , revalidateOnMount =false } = config; | ||
const { cache , initialSize =1 , revalidateAll =false , persistSize =false , revalidateFirstPage =true , revalidateOnMount =false , parallel =false } = config; | ||
// The serialized key of the first page. This key will be used to store | ||
@@ -95,5 +95,6 @@ // metadata of this SWR infinite hook. | ||
const pageSize = resolvePageSize(); | ||
const revalidators = []; | ||
let previousPageData = null; | ||
for(let i = 0; i < pageSize; ++i){ | ||
const [pageKey, pageArg] = _internal.serialize(getKey(i, previousPageData)); | ||
const [pageKey, pageArg] = _internal.serialize(getKey(i, parallel ? null : previousPageData)); | ||
if (!pageKey) { | ||
@@ -114,11 +115,26 @@ break; | ||
if (fn && shouldFetchPage) { | ||
pageData = await fn(pageArg); | ||
setSWRCache({ | ||
data: pageData, | ||
_k: pageArg | ||
}); | ||
const revalidate = async ()=>{ | ||
pageData = await fn(pageArg); | ||
setSWRCache({ | ||
data: pageData, | ||
_k: pageArg | ||
}); | ||
data[i] = pageData; | ||
}; | ||
if (parallel) { | ||
revalidators.push(revalidate); | ||
} else { | ||
await revalidate(); | ||
} | ||
} else { | ||
data[i] = pageData; | ||
} | ||
data.push(pageData); | ||
previousPageData = pageData; | ||
if (!parallel) { | ||
previousPageData = pageData; | ||
} | ||
} | ||
// flush all revalidateions in parallel | ||
if (parallel) { | ||
await Promise.all(revalidators.map((r)=>r())); | ||
} | ||
// once we executed the data fetching based on the context, clear the context | ||
@@ -125,0 +141,0 @@ set({ |
@@ -11,2 +11,3 @@ import { Arguments, BareFetcher, SWRConfiguration, SWRResponse, StrictTupleKey, Middleware } from 'swr/_internal'; | ||
revalidateFirstPage?: boolean; | ||
parallel?: boolean; | ||
fetcher?: Fn; | ||
@@ -13,0 +14,0 @@ } |
@@ -10,2 +10,3 @@ import { useRef, useCallback } from 'react'; | ||
const fetcherRef = useRef(fetcher); | ||
const configRef = useRef(config); | ||
// Ditch all mutation results that happened earlier than this timestamp. | ||
@@ -31,3 +32,3 @@ const ditchMutationsUntilRef = useRef(0); | ||
throwOnError: true | ||
}, config), opts); | ||
}, configRef.current), opts); | ||
// Trigger a mutation, and also track the timestamp. Any mutation that happened | ||
@@ -85,2 +86,3 @@ // earlier this timestamp should be ignored. | ||
fetcherRef.current = fetcher; | ||
configRef.current = config; | ||
}); | ||
@@ -87,0 +89,0 @@ // We don't return `mutate` here as it can be pretty confusing (e.g. people |
@@ -16,2 +16,3 @@ Object.defineProperty(exports, '__esModule', { value: true }); | ||
const fetcherRef = react.useRef(fetcher); | ||
const configRef = react.useRef(config); | ||
// Ditch all mutation results that happened earlier than this timestamp. | ||
@@ -37,3 +38,3 @@ const ditchMutationsUntilRef = react.useRef(0); | ||
throwOnError: true | ||
}, config), opts); | ||
}, configRef.current), opts); | ||
// Trigger a mutation, and also track the timestamp. Any mutation that happened | ||
@@ -91,2 +92,3 @@ // earlier this timestamp should be ignored. | ||
fetcherRef.current = fetcher; | ||
configRef.current = config; | ||
}); | ||
@@ -93,0 +95,0 @@ // We don't return `mutate` here as it can be pretty confusing (e.g. people |
{ | ||
"name": "swr", | ||
"version": "2.0.4", | ||
"version": "2.1.0", | ||
"description": "React Hooks library for remote data fetching", | ||
@@ -37,2 +37,8 @@ "keywords": [ | ||
}, | ||
"./subscription": { | ||
"types": "./subscription/dist/index.d.ts", | ||
"import": "./subscription/dist/index.mjs", | ||
"module": "./subscription/dist/index.esm.js", | ||
"require": "./subscription/dist/index.js" | ||
}, | ||
"./mutation": { | ||
@@ -57,2 +63,3 @@ "types": "./mutation/dist/mutation/index.d.ts", | ||
"_internal/dist/**/*.{js,d.ts,mjs}", | ||
"subscription/dist/*.{js,d.ts,mjs}", | ||
"core/package.json", | ||
@@ -62,3 +69,4 @@ "infinite/package.json", | ||
"mutation/package.json", | ||
"_internal/package.json" | ||
"_internal/package.json", | ||
"subscription/package.json" | ||
], | ||
@@ -68,24 +76,2 @@ "repository": "github:vercel/swr", | ||
"license": "MIT", | ||
"scripts": { | ||
"prepare": "husky install", | ||
"csb:install": "pnpm install", | ||
"csb:build": "pnpm build", | ||
"clean": "pnpm -r run clean && rimraf playwright-report test-result", | ||
"watch": "pnpm -r run watch", | ||
"build": "pnpm build-package _internal && pnpm build-package core && pnpm build-package infinite && pnpm build-package immutable && pnpm build-package mutation", | ||
"build:e2e": "pnpm next build e2e/site", | ||
"build-package": "bunchee index.ts --cwd", | ||
"types:check": "pnpm -r run types:check", | ||
"prepublishOnly": "pnpm clean && pnpm build", | ||
"publish-beta": "pnpm publish --tag beta", | ||
"format": "prettier --write ./**/*.{ts,tsx}", | ||
"lint": "eslint . --ext .ts,.tsx --cache", | ||
"lint:fix": "pnpm lint --fix", | ||
"coverage": "jest --coverage", | ||
"test-typing": "tsc --noEmit -p test/type/tsconfig.json && tsc --noEmit -p test/tsconfig.json", | ||
"test-packaging": "publint", | ||
"test": "jest", | ||
"test:e2e": "playwright test", | ||
"run-all-checks": "pnpm types:check && pnpm lint && pnpm test && pnpm test-typing" | ||
}, | ||
"lint-staged": { | ||
@@ -126,5 +112,5 @@ "*.{ts,tsx}": [ | ||
"rimraf": "4.1.2", | ||
"swr": "workspace:*", | ||
"typescript": "4.9.4", | ||
"@playwright/test": "^1.30.0" | ||
"@playwright/test": "^1.30.0", | ||
"swr": "2.1.0" | ||
}, | ||
@@ -147,3 +133,23 @@ "peerDependencies": { | ||
"use-sync-external-store": "^1.2.0" | ||
}, | ||
"scripts": { | ||
"csb:install": "pnpm install", | ||
"csb:build": "pnpm build", | ||
"clean": "pnpm -r run clean && rimraf playwright-report test-result", | ||
"watch": "pnpm -r run watch", | ||
"build": "pnpm build-package _internal && pnpm build-package core && pnpm build-package infinite && pnpm build-package immutable && pnpm build-package mutation && pnpm build-package subscription", | ||
"build:e2e": "pnpm next build e2e/site -- --profile", | ||
"build-package": "bunchee index.ts --cwd", | ||
"types:check": "pnpm -r run types:check", | ||
"publish-beta": "pnpm publish --tag beta", | ||
"format": "prettier --write ./**/*.{ts,tsx}", | ||
"lint": "eslint . --ext .ts,.tsx --cache", | ||
"lint:fix": "pnpm lint --fix", | ||
"coverage": "jest --coverage", | ||
"test-typing": "tsc --noEmit -p test/type/tsconfig.json && tsc --noEmit -p test/tsconfig.json", | ||
"test-packaging": "publint", | ||
"test": "jest", | ||
"test:e2e": "playwright test", | ||
"run-all-checks": "pnpm types:check && pnpm lint && pnpm test && pnpm test-typing" | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
247667
33
5570