Skip to content

chore: fix ts comments #1540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2021
Merged
Show file tree
Hide file tree
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
21 changes: 10 additions & 11 deletions src/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const useSWRHandler = <Data = any, Error = any>(
}

let newData: Data
let startAt: number
let startAt = -1
let loading = true
const opts = revalidateOpts || {}

Expand All @@ -135,11 +135,11 @@ export const useSWRHandler = <Data = any, Error = any>(
const shouldStartNewRequest =
isUndefined(CONCURRENT_PROMISES[key]) || !opts.dedupe

// Do unmount check for callbacks:
// Do unmount check for calls:
// If key has changed during the revalidation, or the component has been
// unmounted, old dispatch and old event callbacks should not take any
// effect.
const isCallbackSafe = () =>
const isCurrentKeyMounted = () =>
!unmountedRef.current &&
key === keyRef.current &&
initialMountedRef.current
Expand All @@ -158,7 +158,7 @@ export const useSWRHandler = <Data = any, Error = any>(
const finishRequestAndUpdateState = () => {
cache.set(keyValidating, false)
// We can only set state if it's safe (still mounted with the same key).
if (isCallbackSafe()) {
if (isCurrentKeyMounted()) {
setState(newState)
}
}
Expand All @@ -182,7 +182,7 @@ export const useSWRHandler = <Data = any, Error = any>(
// we trigger the loading slow event.
if (config.loadingTimeout && !cache.get(key)) {
setTimeout(() => {
if (loading && isCallbackSafe()) {
if (loading && isCurrentKeyMounted()) {
getConfig().onLoadingSlow(key, config)
}
}, config.loadingTimeout)
Expand Down Expand Up @@ -212,7 +212,7 @@ export const useSWRHandler = <Data = any, Error = any>(
// CONCURRENT_PROMISES_TS[key] maybe be `undefined` or a number
if (CONCURRENT_PROMISES_TS[key] !== startAt) {
if (shouldStartNewRequest) {
if (isCallbackSafe()) {
if (isCurrentKeyMounted()) {
getConfig().onDiscarded(key)
}
}
Expand Down Expand Up @@ -246,7 +246,7 @@ export const useSWRHandler = <Data = any, Error = any>(
) {
finishRequestAndUpdateState()
if (shouldStartNewRequest) {
if (isCallbackSafe()) {
if (isCurrentKeyMounted()) {
getConfig().onDiscarded(key)
}
}
Expand All @@ -267,12 +267,11 @@ export const useSWRHandler = <Data = any, Error = any>(

// Trigger the successful callback if it's the original request.
if (shouldStartNewRequest) {
if (isCallbackSafe()) {
if (isCurrentKeyMounted()) {
getConfig().onSuccess(newData, key, config)
}
}
} catch (err) {
// @ts-ignore
cleanupState(startAt)

// Not paused, we continue handling the error. Otherwise discard it.
Expand All @@ -283,7 +282,7 @@ export const useSWRHandler = <Data = any, Error = any>(

// Error event and retry logic. Only for the actual request, not
// deduped ones.
if (shouldStartNewRequest && isCallbackSafe()) {
if (shouldStartNewRequest && isCurrentKeyMounted()) {
getConfig().onError(err, key, config)
if (config.shouldRetryOnError) {
// When retrying, dedupe is always enabled
Expand All @@ -304,7 +303,7 @@ export const useSWRHandler = <Data = any, Error = any>(

// Here is the source of the request, need to tell all other hooks to
// update their states.
if (isCallbackSafe() && shouldStartNewRequest) {
if (isCurrentKeyMounted() && shouldStartNewRequest) {
broadcastState(cache, key, newState.data, newState.error, false)
}

Expand Down
5 changes: 2 additions & 3 deletions src/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defaultConfigOptions } from './web-preset'
import { IS_SERVER } from './env'
import { UNDEFINED, mergeObjects, noop } from './helper'
import { internalMutate } from './mutate'
import { SWRGlobalState } from './global-state'
import { GlobalState, SWRGlobalState } from './global-state'
import * as revalidateEvents from '../constants/revalidate-events'
import { RevalidateEvent } from '../types'

Expand Down Expand Up @@ -81,7 +81,6 @@ export const initCache = <Data = any>(
// When un-mounting, we need to remove the cache provider from the state
// storage too because it's a side-effect. Otherwise when re-mounting we
// will not re-register those event listeners.
// @ts-ignore
SWRGlobalState.delete(provider)
}
}
Expand All @@ -92,5 +91,5 @@ export const initCache = <Data = any>(
return [provider, mutate, unmount]
}

return [provider, SWRGlobalState.get(provider)![6]]
return [provider, (SWRGlobalState.get(provider) as GlobalState)[6]]
}