import { useContext , useEffect , useRef } from 'react' ; import { DependencyCollection } from '../collection' ; import { DependencyItem , DependencyKey } from '../typings' ; import { getDependencyKeyName } from '../utils' ; import { InjectionContext } from './context' ; /**
 * when providing dependencies in a functional component, it would be expensive
 * (not to mention logic incorrectness)
 */ export function useCollection ( entries ? ) { const collectionRef = useRef ( new DependencyCollection ( entries ) ) ; useEffect ( ( ) => ( ) => collectionRef . current . dispose ( ) , [ ] ) ; return collectionRef . current ; } /**
 * this function support using dependency injection in a function component
 * with the help of React Hooks
 */ export function useDependency < T > ( key ) ; export function useDependency < T > ( key , optional ) ; export function useDependency < T > ( key , optional ? ) { const { injector } = useContext ( InjectionContext ) ; const thing = injector ?. getOrInit ( key , true ) ; if ( ! optional && ! thing ) { throw Error ( `[wedi] Cannot get an instance of "${ getDependencyKeyName ( key ) }".` ) ; } return thing || null ; } type Nullable < T > = T | null ; export function useMultiDependencies < D1 , D2 > ( keys ) ; export function useMultiDependencies < D1 , D2 , D3 > ( keys ) ; export function useMultiDependencies < D1 , D2 , D3 , D4 > ( keys ) ; export function useMultiDependencies < D1 , D2 , D3 , D4 , D5 > ( keys ) ; export function useMultiDependencies < D1 , D2 , D3 , D4 , D5 , D6 > ( keys ) ; export function useMultiDependencies ( keys ) { const ret = new Array ( keys . length ) . fill ( null ) ; const { injector } = useContext ( InjectionContext ) ; keys . forEach ( ( key , index ) => { ret [ index ] = injector ?. getOrInit ( key ) ?? null ; } ) ; return ret ; }