UNPKG

dino-core

Version:

A dependency injection framework for NodeJS applications

115 lines (114 loc) 4.52 kB
import { ComponentDescriptor } from '../model/ComponentDescriptor'; import { type EnvironmentConfiguration } from '../model/environment.configuration'; import { Injectable } from '../model/injectable'; import { ContextScope } from './ContextScope'; /** * Define the application context * * @public */ export declare class ApplicationContext { private loaded; private contextConfigurationProvider; private readonly executedLifecycles; private container; /** * Create a new application context. * * @constructor */ constructor(); createScope(): ContextScope; /** * Try resolve the requested dependency, if not resolved try require the dependency and if found * registers it on the application context otherwise throw error. * * > **Will not take dependency graph into account!** * * @param {String} name the name of the dependency to require * @throws {Error} when not able to resolve the dependency * * @public */ require(name: string): any; /** * Resolve a component from this context and return a reference. * @param {String} component the name of the component to resolve * * @public` */ resolve(component: string): any; /** * Resolves all components of a particular type. This is a fairly expensive method * as such it should be used with caution. * The current implementation checks the context cache and the registrations skipping already cached * instances, the drawback is that unwanted registration are resolved potentially ahead of time. * * While work is ongoing to resolve the drawback a suitable workaround might be to instantiate a factory via * configuration, this creates a manual overhead but might avoid issues. * * @param {Function} type the type to resolve * @param [onlyCached=false] allows to define is only cached injectables will be resolved, defaults to false meaning that * all registration will be scanned and forcibly loaded. * @returns {Array<any>} * * @public */ resolveAll(type: any, onlyCached?: boolean): any[]; /** * Add, register, a component on this context. This is a replacement of the register function * that will be deprecated on future releases. * * The main difference is that it uses the name provided via ComponentDescriptor avoiding the possibility * of double naming for injectables. * * @param {ComponentDescriptor} componentDescriptor the component descriptor able to resolve this component * @returns {any} the registered instance * * @public */ add(componentDescriptor: ComponentDescriptor): any; /** * Register a component on this context * @param {String} name the component name * @param {ComponentDescriptor} resolver the component descriptor able to resolve this component * @returns {any} the registered instance * * @public * @deprecated use `ApplicationContext#add` instead */ register(name: string, resolver: ComponentDescriptor): any; /** * Try to resolve an injectable instance * * @param {String} name the instance name * @param {Boolean} doExecuteLifecycle a flag indicating if the lifecycle should be executed * @returns {Injectable} the resolved instance * * @private */ protected tryResolveInstance(name: string, doExecuteLifecycle: boolean): Injectable | undefined; /** * Execute an component lifecycle if required * @param {Injectable} instance the instance to execute the lifecycle for * @param {Boolean} doExecuteLifecycle a flag indicating fif the lifecycle should be executed * @returns {void} */ protected executeLifecycleIfNeeded(instance: Injectable | any, doExecuteLifecycle: boolean): void; /** * Load the application context * @param {String} config path, relative to the root directory of the project * of the application context configuration file * @returns {ApplicationContext} the application context instance * * @public */ load(config?: string): Promise<void>; getActiveProfiles(environment: EnvironmentConfiguration): string[]; destroy(): Promise<void>; /** * Register the shutdown handler, the handler will be responsible for invoking the preDestroy method * on all components and dispose the awilix container. */ registerShutdown(): void; }