Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/index/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ type DefinitelyTruthy<T> = false extends T
? never
: T

function triggerRevalidators(
key: string,
revalidators: ((type: RevalidateEvent, opts?: { retryCount?: number; dedupe?: boolean }) => void)[] | undefined,
type: RevalidateEvent,
opts?: { retryCount?: number; dedupe?: boolean }
) {
if (!Array.isArray(revalidators)) return

for (const revalidate of revalidators) {
try {
revalidate(type, opts)
} catch (err) {
if (process.env.NODE_ENV !== 'production') {
console.error(`SWR: Error triggering revalidator for key "${key}"`, err)
}
}
}
}

export const useSWRHandler = <Data = any, Error = any>(
_key: Key,
fetcher: Fetcher<Data> | null,
Expand Down Expand Up @@ -521,12 +540,12 @@ export const useSWRHandler = <Data = any, Error = any>(
currentConfig,
_opts => {
const revalidators = EVENT_REVALIDATORS[key]
if (revalidators && revalidators[0]) {
revalidators[0](
revalidateEvents.ERROR_REVALIDATE_EVENT,
_opts
)
}
triggerRevalidators(
key,
revalidators,
revalidateEvents.ERROR_REVALIDATE_EVENT,
_opts
)
},
{
retryCount: (opts.retryCount || 0) + 1,
Expand Down