Class: Injector

Injector

Dependencies are registered and maintained in memory by the Injector.

Registering a dependency on the Injector validates it and determines which other dependencies it requires. This does not invoke the fn property, if one is provided.

injector.register({
  name: 'server',
  type: 'factory',
  plugin: '<plugin.name>',
  fn: function (express, logger) {
    let server = express()
    server.use(logger)
    return server
  }
})

Getting a dependency from the Injector invokes the dependency's fn property as a function and caches the return value on the value property for future calls to get. This recursively satisfies the requirements of fn.

injector.get('server')
=> server
// this caches the return value of `fn` as `value`
=> Dependency {
     name: 'server',
     type: 'factory',
     plugin: '<plugin.name>',
     fn: function (express, logger) { ... },
     value: server
   }

Invoking a dependency calls it's fn property as a function, without caching or returning a result.

injector.register({
  name: 'server:starter',
  type: 'callback',
  plugin: 'server',
  fn: function (server) {
    server.listen()
  }
})

injector.invoke('server:starter')

Filtering a dependency returns a DependencyCollection instance filtered by a predicate.

injector.filter({ type: 'factory', plugin: 'MyPlugin' })
=> DependencyCollection [
     Dependency { type: 'factory', plugin: 'MyPlugin', ... },
     Dependency { type: 'factory', plugin: 'MyPlugin', ... },
     Dependency { type: 'factory', plugin: 'MyPlugin', ... },
     // ...
   ]

Constructor

new Injector()

Initialize the injector with a reference to itself as a dependency.

Source:

Methods

filter(predicate) → {DependencyCollection}

Query the injector for dependencies matching a predicate

Parameters:
Name Type Description
predicate Object | function

Description or function for matching dependencies

Source:
Returns:
Type
DependencyCollection

get(name) → {*}

Get a dependency from the injector by invoking it's function or value property.

Parameters:
Name Type Description
name string

Dependency name

Source:
Returns:
Type
*

invoke(name)

Lifecycle callbacks which use dependencies on the injector, but return no values can be registered on the injector for event handling. This method invokes such callbacks by name, providing any required dependencies as arguments. It fails silently if no callback is found.

Parameters:
Name Type Description
name string

Dependency name

Source:
To Do:
  • consider consolidating repeated code in invoke and get into a single 'internal' #todo function and wrap it for get and invoke functionality

register(descriptor)

Register a Dependency instance on the injector without invoking its factory function.

Parameters:
Name Type Description
descriptor Object

Dependency properties

Source: