Skip to content
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

Chore: refactor code related to unsubscribing global listeners #1451

Merged
merged 1 commit into from Sep 13, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/utils/cache.ts
@@ -1,6 +1,6 @@
import { defaultConfigOptions } from './web-preset'
import { IS_SERVER } from './env'
import { UNDEFINED, mergeObjects } from './helper'
import { UNDEFINED, mergeObjects, noop } from './helper'
import { internalMutate } from './mutate'
import { SWRGlobalState } from './global-state'
import * as revalidateEvents from '../constants/revalidate-events'
Expand Down Expand Up @@ -41,6 +41,7 @@ export const initCache = <Data = any>(
const mutate = internalMutate.bind(UNDEFINED, provider) as ScopedMutator<
Data
>
let unsubscribe = noop

// Update the state if it's new, or the provider has been extended.
SWRGlobalState.set(provider, [
Expand All @@ -55,7 +56,6 @@ export const initCache = <Data = any>(

// This is a new provider, we need to initialize it and setup DOM events
// listeners for `focus` and `reconnect` actions.
let unscubscibe = () => {}
if (!IS_SERVER) {
const releaseFocus = opts.initFocus(
revalidateAllKeys.bind(
Expand All @@ -71,7 +71,7 @@ export const initCache = <Data = any>(
revalidateEvents.RECONNECT_EVENT
)
)
unscubscibe = () => {
unsubscribe = () => {
releaseFocus && releaseFocus()
releaseReconnect && releaseReconnect()
}
Expand All @@ -80,6 +80,6 @@ export const initCache = <Data = any>(
// We might want to inject an extra layer on top of `provider` in the future,
// such as key serialization, auto GC, etc.
// For now, it's just a `Map` interface without any modifications.
return [provider, mutate, unscubscibe]
return [provider, mutate, unsubscribe]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch 😂 thx

}
}
31 changes: 12 additions & 19 deletions src/utils/config-context.ts
@@ -1,15 +1,9 @@
import {
createContext,
createElement,
useContext,
useState,
FC,
useEffect
} from 'react'
import { createContext, createElement, useContext, useState, FC } from 'react'
import { cache as defaultCache } from './config'
import { initCache } from './cache'
import { mergeConfigs } from './merge-config'
import { isFunction, UNDEFINED } from './helper'
import { UNDEFINED } from './helper'
import { useIsomorphicLayoutEffect } from './env'
import {
SWRConfiguration,
FullConfiguration,
Expand All @@ -32,24 +26,23 @@ const SWRConfig: FC<{
const provider = value && value.provider

// Use a lazy initialized state to create the cache on first access.
const [cacheHandle] = useState(() =>
const [cacheContext] = useState(() =>
provider
? initCache(provider(extendedConfig.cache || defaultCache), value)
: UNDEFINED
)

// Override the cache if a new provider is given.
if (cacheHandle) {
extendedConfig.cache = cacheHandle[0]
extendedConfig.mutate = cacheHandle[1]
if (cacheContext) {
extendedConfig.cache = cacheContext[0]
extendedConfig.mutate = cacheContext[1]
}

useEffect(() => {
return () => {
const unsubscribe = cacheHandle ? cacheHandle[2] : UNDEFINED
isFunction(unsubscribe) && unsubscribe()
}
}, [])
// Unsubscribe events.
useIsomorphicLayoutEffect(
() => (cacheContext ? cacheContext[2] : UNDEFINED),
[]
)

return createElement(
SWRConfigContext.Provider,
Expand Down