|
|
|
@ -1,26 +1,34 @@
|
|
|
|
|
/**
|
|
|
|
|
* @module flitter-di/src/Container
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/** Manages service definitions, instances, and deferred injection. */
|
|
|
|
|
class Container {
|
|
|
|
|
/**
|
|
|
|
|
* Instantiates the container.
|
|
|
|
|
* @param {object} definitions - mapping of service name to static service CLASS definition
|
|
|
|
|
*/
|
|
|
|
|
constructor(definitions = {}) {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Static service definitions from which instances are created when
|
|
|
|
|
* the services are requested. Should be in service name -> service
|
|
|
|
|
* definition pairs.
|
|
|
|
|
* @type object
|
|
|
|
|
* @type {object}
|
|
|
|
|
*/
|
|
|
|
|
this.definitions = definitions
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Instantiated services. If a service has already been requested, it is
|
|
|
|
|
* stored here so that the single instance can be reused.
|
|
|
|
|
* @type object
|
|
|
|
|
* @type {object}
|
|
|
|
|
*/
|
|
|
|
|
this.instances = {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Already injected static service definitions. These are used to resolve
|
|
|
|
|
* circular dependencies.
|
|
|
|
|
* @type object
|
|
|
|
|
* @type {object}
|
|
|
|
|
*/
|
|
|
|
|
this.statics = {}
|
|
|
|
|
|
|
|
|
@ -28,7 +36,7 @@ class Container {
|
|
|
|
|
* Instance of the dependency injector this container is associated with.
|
|
|
|
|
* If this is specified, services will be injected with other services when
|
|
|
|
|
* they are instantiated.
|
|
|
|
|
* @type {boolean|DependencyInjector}
|
|
|
|
|
* @type {boolean|module:flitter-di/src/DependencyInjector~DependencyInjector}
|
|
|
|
|
*/
|
|
|
|
|
this.di = false
|
|
|
|
|
|
|
|
|
@ -36,7 +44,7 @@ class Container {
|
|
|
|
|
* Array of static class definitions with deferred services. These static
|
|
|
|
|
* definitions are waiting for a service to be registered with this container
|
|
|
|
|
* so it can be injected into the prototype and instances.
|
|
|
|
|
* @type {*[]}
|
|
|
|
|
* @type {Array<*>}
|
|
|
|
|
*/
|
|
|
|
|
this.deferred_classes = []
|
|
|
|
|
}
|
|
|
|
@ -55,7 +63,7 @@ class Container {
|
|
|
|
|
* proxy container. This container has getters for all the services by
|
|
|
|
|
* name.
|
|
|
|
|
* @param {string} service - the name of the service
|
|
|
|
|
* @returns {Service|*} - the service instance of service container proxy
|
|
|
|
|
* @returns {module:flitter-di/src/Service~Service|undefined} - the service instance or service container proxy
|
|
|
|
|
*/
|
|
|
|
|
service(service = false) {
|
|
|
|
|
if ( service === false ) {
|
|
|
|
@ -103,7 +111,7 @@ class Container {
|
|
|
|
|
* instance will be returned as the instance of the service. The instance's
|
|
|
|
|
* constructor is saved as the service definition.
|
|
|
|
|
* @param {string} service_name - the referential name of the service
|
|
|
|
|
* @param {Service} service_instance - the service class instance
|
|
|
|
|
* @param {module:flitter-di/src/Service~Service} service_instance - the service class instance
|
|
|
|
|
*/
|
|
|
|
|
register_as_instance(service_name, service_instance) {
|
|
|
|
|
this.definitions[service_name] = service_instance.constructor
|
|
|
|
@ -114,7 +122,7 @@ class Container {
|
|
|
|
|
/**
|
|
|
|
|
* Process deferred classes that need the provided service name and instance.
|
|
|
|
|
* @param {string} service_name - the referential name of the service
|
|
|
|
|
* @param {Service} service_instance - the instance of the service
|
|
|
|
|
* @param {module:flitter-di/src/Service~Service} service_instance - the instance of the service
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
_process_deferral(service_name, service_instance) {
|
|
|
|
|