import { RediError } from './error' ; import { DependencyIdentifier } from './dependencyIdentifier' ; import { DependencyItem , prettyPrintIdentifier } from './dependencyItem' ; let singletonFetchedLock = false ; const singletonDependencies = [ ] ; class DuplicatedRegistrationError extends RediError { constructor ( id , item1 , item2 ) { super ( `Duplicated registration of ${ prettyPrintIdentifier ( id ) }, 1. ${ item1 } 2. ${ item2 }.` ) ; } } export function registerSingleton < T > ( id , item ) { const index = singletonDependencies . findIndex ( ( r ) => r [ 0 ] === id ) ; if ( index !== - 1 ) { throw new DuplicatedRegistrationError ( id , singletonDependencies [ index ] [ 1 ] , item ) ; } singletonDependencies . push ( [ id , item ] ) ; } export function getSingletonDependencies ( ) { if ( singletonFetchedLock ) { console . warn ( '[redi]: Singleton dependencies has been fetched before by an other injector. ' + 'Please avoid fetching singleton dependencies twice.' ) ; } singletonFetchedLock = true ; return singletonDependencies ; } export function TEST_ONLY_clearSingletonDependencies ( ) { singletonFetchedLock = false ; singletonDependencies . length = 0 ; }