import containerItem from './containerItem' ; import { IContainerItem , IContainer } from './types' ; import isVarName from '../helpers/isVarName' ; import IncorrectContainerKeyNameError from '../errors/IncorrectContainerKeyNameError' ; const containerRegister = new Array < IContainerItem > ( ) ; const takeContainerByKey = ( key ) : IContainerItem [ ] => containerRegister . filter ( ( x ) => x . getKey ( ) === key ) ; param key param item param lifecycle const register = ( key , item , lifecycle ? ) : void => { if ( ! isVarName ( key ) ) { throw new IncorrectContainerKeyNameError ( key ) ; } const containerItems = takeContainerByKey ( key ) ; if ( containerItems . length === 0 ) { const ci = containerItem . containerItemFactory ( key , item , lifecycle ) ; containerRegister . push ( ci ) ; } else { containerItems [ 0 ] . Item = item ; } } ; param key return { any } const take = ( key ) : any => { const containerItems = takeContainerByKey ( key ) ; return containerItems . length === 0 ? null : containerItems [ 0 ] . Item ; } ; return const keys = ( ) : string [ ] => containerRegister . map ( ( x ) => x . getKey ( ) ) ; const container = { register , take , keys , } ; export default container as IContainer ;