import { BindingScopeEnum , interfaces } from "../inversify" ; import { isPromise } from "../utils/async" ; export const tryGetFromScope = < T > ( requestScope , binding ) : T | Promise < T > | null => { if ( ( binding . scope === BindingScopeEnum . Singleton ) && binding . activated ) { return binding . cache ! ; } if ( binding . scope === BindingScopeEnum . Request && requestScope . has ( binding . id ) ) { return requestScope . get ( binding . id ) as T | Promise < T > ; } return null ; } ; export const saveToScope = < T > ( requestScope , binding , result ) : void => { if ( binding . scope === BindingScopeEnum . Singleton ) { _saveToSingletonScope ( binding , result ) ; } if ( binding . scope === BindingScopeEnum . Request ) { _saveToRequestScope ( requestScope , binding , result ) ; } } ; const _saveToRequestScope = < T > ( requestScope , binding , result ) : void => { if ( ! requestScope . has ( binding . id ) ) { requestScope . set ( binding . id , result ) ; } } ; const _saveToSingletonScope = < T > ( binding , result ) : void => { binding . cache = result ; binding . activated = true ; if ( isPromise ( result ) ) { void _saveAsyncResultToSingletonScope ( binding , result ) ; } } ; const _saveAsyncResultToSingletonScope = async < T > ( binding , asyncResult ) : Promise < void > => { try { const result = await asyncResult ; binding . cache = result ; } catch ( ex ) { binding . cache = null ; binding . activated = false ; throw ex ; } } ;