import { Ctor , DependencyKey , DependencyMeta , Identifier , isIdentifier } from './typings' ; export const dependencyIds = new Map < string , Identifier < any > > ( ) ; export const DEPENDENCIES = '$$WEDI_DEPENDENCIES' ; export const TARGET = '$$WEDI_TARGET' ; export function getDependencies < T > ( ctor ) { return ( ctor as any ) [ DEPENDENCIES ] || [ ] ; } export function setDependencies < T > ( ctor , id , index , optional ) { const meta = { id , index , optional } ; if ( ( ctor as any ) [ TARGET ] === ctor ) { ; ( ctor as any ) [ DEPENDENCIES ] . push ( meta ) ; } else { ; ( ctor as any ) [ DEPENDENCIES ] = [ meta ] ; ( ctor as any ) [ TARGET ] = ctor ; } } const RECURSION_MAX = 10 ; let recursionCounter = 0 ; export function requireInitialization ( ) { recursionCounter += 1 ; } export function completeInitialization ( ) { recursionCounter -= 1 ; } export function resetRecursionCounter ( ) { recursionCounter = 0 ; } export function assertRecursionNotTrappedInACircle ( key ) { if ( recursionCounter > RECURSION_MAX ) { resetRecursionCounter ( ) ; throw new Error ( `[wedi] "createInstance" exceeds the limitation of recursion (${ RECURSION_MAX }x). ` + `There might be a circular dependency among your dependency items. ` + `Last target was "${ getDependencyKeyName ( key ) }".` ) ; } } export function getDependencyKeyName ( key ) { return isIdentifier ( key ) ? key . toString ( ) : key . name ; }