garrettmills преди 4 години
родител 6379077b25
ревизия f80496402c

@ -1,3 +1,7 @@
/**
* @module flitter-di
*/
const DependencyInjector = require('./src/DependencyInjector')
const Injectable = require('./src/Injectable')
const Container = require('./src/Container')

@ -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) {

@ -1,3 +1,7 @@
/**
* @module flitter-di/src/DependencyInjector
*/
const Container = require('./Container')
/** Manages services and injects classes from its service container. */
@ -5,7 +9,7 @@ class DependencyInjector {
constructor(container = new Container()) {
/**
* The service container used by this dependency injector.
* @type {Container}
* @type {module:flitter-di/src/Container~Container}
*/
this.container = container
this.container.di = this
@ -31,7 +35,7 @@ class DependencyInjector {
* proxy container. This container has getters for all the services by
* name.
* @param {string} service - the name of the service
* @returns {{}|*} - the service instance of service container proxy
* @returns {module:flitter-di/src/Service~Service|undefined|Proxy} - the service instance or service container proxy
*/
service(name) {
return this.container.service(name)
@ -39,7 +43,7 @@ class DependencyInjector {
/**
* Verify that the injector's container has a service or set of services.
* @param {string|string[]} name - service name or array of service names
* @param {string|Array<string>} name - service name or array of service names
* @returns {boolean} - true if the container has the service(s)
*/
has(name) {

@ -1,3 +1,7 @@
/**
* @module flitter-di/src/Injectable
*/
/** Base class for classes that support service injection. */
class Injectable {
/**
@ -12,7 +16,7 @@ class Injectable {
/**
* List of services that were deferred and not provided at the time of injection.
* @type {string[]}
* @type {Array<string>}
* @private
*/
static _di_deferred_services = []
@ -20,14 +24,14 @@ class Injectable {
/**
* Collection of instances of this class that need to have the deferred service
* instances injected into them when the deferred services are finally provided.
* @type {*[]}
* @type {Array<module:flitter-di/src/Injectable~Injectable>}
* @private
*/
static _di_deferred_instances = []
/**
* Get the names of services required by this class.
* @returns {string[]}
* @returns {Array<string>}
*/
static get services() {
return []
@ -47,7 +51,7 @@ class Injectable {
* services are injected directly into this class' prototype. If deferral
* is enabled, services not in the container will be stored and the class
* will be deferred.
* @param {Container} container
* @param {module:flitter-di/src/Container~Container} container
* @private
*/
static __inject(container) {
@ -75,7 +79,7 @@ class Injectable {
* Injects the missing service into the prototype and any instances of this
* class.
* @param {string} service_name - the deferred service name
* @param {Service} service_instance - the instance of the service
* @param {module:flitter-di/src/Service~Service} service_instance - the instance of the service
* @private
*/
static __deferral_callback(service_name, service_instance) {

@ -1,5 +1,12 @@
/**
* @module flitter-di/src/Service
*/
const Injectable = require('./Injectable')
/** A service that can be registered with a container and used in a single-instance format. */
/**
* A service that can be registered with a container and used in a single-instance format.
* @extends module:flitter-di/src/Injectable~Injectable
*/
class Service extends Injectable {
}

Зареждане…
Отказ
Запис