Skip to content

Commit c399885

Browse files
authored
Merge branch 'master' into optimize-5
2 parents af8256b + a9c6d79 commit c399885

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

src/utils/cache.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defaultConfigOptions } from './web-preset'
22
import { IS_SERVER } from './env'
3-
import { UNDEFINED, mergeObjects } from './helper'
3+
import { UNDEFINED, mergeObjects, noop } from './helper'
44
import { internalMutate } from './mutate'
55
import { SWRGlobalState } from './global-state'
66
import * as revalidateEvents from '../constants/revalidate-events'
@@ -41,6 +41,7 @@ export const initCache = <Data = any>(
4141
const mutate = internalMutate.bind(UNDEFINED, provider) as ScopedMutator<
4242
Data
4343
>
44+
let unsubscribe = noop
4445

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

5657
// This is a new provider, we need to initialize it and setup DOM events
5758
// listeners for `focus` and `reconnect` actions.
58-
let unscubscibe = () => {}
5959
if (!IS_SERVER) {
6060
const releaseFocus = opts.initFocus(
6161
revalidateAllKeys.bind(
@@ -71,7 +71,7 @@ export const initCache = <Data = any>(
7171
revalidateEvents.RECONNECT_EVENT
7272
)
7373
)
74-
unscubscibe = () => {
74+
unsubscribe = () => {
7575
releaseFocus && releaseFocus()
7676
releaseReconnect && releaseReconnect()
7777
}
@@ -80,6 +80,6 @@ export const initCache = <Data = any>(
8080
// We might want to inject an extra layer on top of `provider` in the future,
8181
// such as key serialization, auto GC, etc.
8282
// For now, it's just a `Map` interface without any modifications.
83-
return [provider, mutate, unscubscibe]
83+
return [provider, mutate, unsubscribe]
8484
}
8585
}

src/utils/config-context.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
import {
2-
createContext,
3-
createElement,
4-
useContext,
5-
useState,
6-
FC,
7-
useEffect
8-
} from 'react'
1+
import { createContext, createElement, useContext, useState, FC } from 'react'
92
import { cache as defaultCache } from './config'
103
import { initCache } from './cache'
114
import { mergeConfigs } from './merge-config'
12-
import { isFunction, UNDEFINED } from './helper'
5+
import { UNDEFINED } from './helper'
6+
import { useIsomorphicLayoutEffect } from './env'
137
import {
148
SWRConfiguration,
159
FullConfiguration,
@@ -32,24 +26,23 @@ const SWRConfig: FC<{
3226
const provider = value && value.provider
3327

3428
// Use a lazy initialized state to create the cache on first access.
35-
const [cacheHandle] = useState(() =>
29+
const [cacheContext] = useState(() =>
3630
provider
3731
? initCache(provider(extendedConfig.cache || defaultCache), value)
3832
: UNDEFINED
3933
)
4034

4135
// Override the cache if a new provider is given.
42-
if (cacheHandle) {
43-
extendedConfig.cache = cacheHandle[0]
44-
extendedConfig.mutate = cacheHandle[1]
36+
if (cacheContext) {
37+
extendedConfig.cache = cacheContext[0]
38+
extendedConfig.mutate = cacheContext[1]
4539
}
4640

47-
useEffect(() => {
48-
return () => {
49-
const unsubscribe = cacheHandle ? cacheHandle[2] : UNDEFINED
50-
isFunction(unsubscribe) && unsubscribe()
51-
}
52-
}, [])
41+
// Unsubscribe events.
42+
useIsomorphicLayoutEffect(
43+
() => (cacheContext ? cacheContext[2] : UNDEFINED),
44+
[]
45+
)
5346

5447
return createElement(
5548
SWRConfigContext.Provider,

0 commit comments

Comments
 (0)