Container

flitter-di/src/Container~ Container

Manages service definitions, instances, and deferred injection.


Constructor

new Container(definitions)

Instantiates the container.

Parameters:
Name Type Description
definitions object

mapping of service name to static service CLASS definition

Members

deferred_classes :Array.<*>

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:
  • Array.<*>

definitions :object

Static service definitions from which instances are created when the services are requested. Should be in service name -> service definition pairs.

Type:
  • object

di :boolean|module:flitter-di/src/DependencyInjector~DependencyInjector

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.

instances :object

Instantiated services. If a service has already been requested, it is stored here so that the single instance can be reused.

Type:
  • object

statics :object

Already injected static service definitions. These are used to resolve circular dependencies.

Type:
  • object

Methods

(private) __is_deferrable(Class) → {boolean}

Checks if a class is deferrable. That is, does it have the requirements for functioning with the defer logic. In almost all cases, these should be satisfied by having the Class extend from Injectable.

Parameters:
Name Type Description
Class *

the static class to check

Returns:
  • true if the class is deferrable
Type
boolean

(private) _process_deferral(service_name, service_instance)

Process deferred classes that need the provided service name and instance.

Parameters:
Name Type Description
service_name string

the referential name of the service

service_instance module:flitter-di/src/Service~Service

the instance of the service

defer(Class)

Defer a static class to have its missing services filled in as they become available in the service container. The class should extend from Injectable.

Parameters:
Name Type Description
Class *

the static class to be deferred

has(service) → {boolean}

Check if a service definition exists in this container.

Parameters:
Name Type Description
service string

the name of the service

Returns:
  • true if the service definition exists in this container
Type
boolean

register(service_name, service_class)

Register a class definition as a service. When requested, the service for this class will be created from the class' instance.

Parameters:
Name Type Description
service_name string

the referential name of the service

service_class *

the service class definition

register_as_instance(service_name, service_instance)

Register a class instance as a service. When requested, the provided instance will be returned as the instance of the service. The instance's constructor is saved as the service definition.

Parameters:
Name Type Description
service_name string

the referential name of the service

service_instance module:flitter-di/src/Service~Service

the service class instance

service(service) → {module:flitter-di/src/Service~Service|undefined}

Fetch a service by name. If no name is provided, return the service proxy container. This container has getters for all the services by name.

Parameters:
Name Type Default Description
service string false

the name of the service

Returns:
  • the service instance or service container proxy
Type
module:flitter-di/src/Service~Service | undefined