import * as React from 'react' ; import { Context , ContextListener , ContextValue , CreateContextOptions } from './types' ; const calculateChangedBits = ( ) => 0 ; const createProvider = < Value > ( Original ) => { const Provider = props => { const listeners = React . useRef < ContextListener < Value > [ ] > ( [ ] ) ; listeners . current . forEach ( listener => listener ( props . value ) ) ; const subscribe = React . useCallback ( ( listener ) => { listeners . current . push ( listener ) ; const unsubscribe = ( ) => { const index = listeners . current . indexOf ( listener ) ; listeners . current . splice ( index , 1 ) ; } ; return unsubscribe ; } , [ ] ) ; return React . createElement ( Original , { value : { subscribe , value : props . value } } , props . children ) ; } ; if ( process . env . NODE_ENV !== 'production' ) { Provider . displayName = 'ContextSelector.Provider' ; } return Provider ; } ; const createContext = < Value > ( defaultValue , options = { } ) : Context < Value > => { const { strict = true } = options ; const context = React . createContext < ContextValue < Value > > ( { get subscribe ( ) { if ( strict ) { throw new Error ( process . env . NODE_ENV === 'production' ? '' : `Please use <Provider /> component from "@fluentui/react-context-selector"` ) ; } return ( ) => ( ) => { } ; } , value : defaultValue , } , calculateChangedBits ) ; context . Provider = createProvider < Value > ( context . Provider ) as any ; delete context . Consumer ; return ( context as unknown ) as Context < Value > ; } ; export default createContext ;