import * as React from 'react' ; import { useContext , useState } from 'react' ; import { interfaces } from 'inversify' ; import { InversifyReactContext } from './internal' ; type ProviderProps = Readonly < { container : interfaces . Container | ( ( ) => interfaces . Container ) ; standalone ? : boolean ; } > ; function isContainer ( x ) { return 'resolve' in x ; } const Provider = ( { children , container : containerProp , standalone : standaloneProp = false } ) => { const [ container ] = useState ( containerProp ) ; if ( isContainer ( containerProp ) && containerProp !== container ) { throw new Error ( 'Changing `container` prop (swapping container in runtime) is not supported.\n' + 'If you\'re rendering Provider in some list, try adding `key={container.id}` to the Provider.\n' + 'More info on React lists:\n' + 'https://reactjs.org/docs/lists-and-keys.html#keys\n' + 'https://reactjs.org/docs/reconciliation.html#recursing-on-children' ) ; } const [ standalone ] = useState ( standaloneProp ) ; if ( standaloneProp !== standalone ) { throw new Error ( 'Changing `standalone` prop is not supported.' ) ; } const parentContainer = useContext ( InversifyReactContext ) ; useState ( function prepareContainer ( ) { if ( ! standalone && parentContainer ) { if ( parentContainer === container ) { throw new Error ( 'Provider has found a parent container (on surrounding React Context), ' + 'yet somehow it\'s the same as container specified in props. It doesn\'t make sense.\n' + 'Perhaps you meant to configure Provider as \`standalone={true}\`?' ) ; } if ( container . parent ) { throw new Error ( 'Ambiguous containers hierarchy.\n' + 'Provider has found a parent for specified `container`, but it already has a parent.\n' + 'Learn more at https://github.com/Kukkimonsuta/inversify-react/blob/v0.5.0/src/provider.tsx' ) ; } container . parent = parentContainer ; } } ) ; return ( < InversifyReactContext . Provider value = { container } > { children } < /InversifyReactContext.Provider>); } ; export { ProviderProps , Provider } ; export default Provider ;