import { createContext , createElement , useContext , useState , FC } from 'react' ; import { cache as defaultCache } from './config' ; import { initCache } from './cache' ; import { mergeConfigs } from './merge-config' ; import { UNDEFINED , mergeObjects } from './helper' ; import { useIsomorphicLayoutEffect } from './env' ; import { SWRConfiguration , FullConfiguration , ProviderConfiguration , Cache } from '../types' ; export const SWRConfigContext = createContext < Partial < FullConfiguration > > ( { } ) ; const SWRConfig = props => { const { value } = props ; const extendedConfig = mergeConfigs ( useContext ( SWRConfigContext ) , value ) ; const provider = value && value . provider ; const [ cacheContext ] = useState ( ( ) => provider ? initCache ( provider ( extendedConfig . cache || defaultCache ) , value ) : UNDEFINED ) ; if ( cacheContext ) { extendedConfig . cache = cacheContext [ 0 ] ; extendedConfig . mutate = cacheContext [ 1 ] ; } useIsomorphicLayoutEffect ( ( ) => ( cacheContext ? cacheContext [ 2 ] : UNDEFINED ) , [ ] ) ; return createElement ( SWRConfigContext . Provider , mergeObjects ( props , { value : extendedConfig } ) ) ; } ; export default SWRConfig ;