new Context(name)
Creates new context instance.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
name |
string
|
The context's name. This can be later used to fetch the instance from the repository. See Context.get, and Context.delete |
Methods
(static) delete(name)
Removes a context instance with the given name from the repository.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
name |
string
|
The context's name. |
(static) get(name) → {Context|undefined}
Finds a context instance with the given name and return it.
Returns undefined
if not found.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
name |
string
|
The context's name. |
provide(key) → {Provider}
Creates and returns a new provider instance.
The instance will be used as a provider for the dependency with the key
.
That is, when resolving dependency with the key
, the returned provider will be used.
See Provider API to customize provision settings.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
key |
string
|
The key to register the provider. |
Example
context.provide('logger').as(console.log);
resolve(key) → {Promise.<any>}
Finds and returns the depedency with the given key
.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
key |
string
|
The dependency's key. |
Throws:
-
Throws when no valid provider has been registered for the
key
. - Type
-
DependencyError
Returns:
- Type:
-
Promise.<any>
The resolved dependency.
Example
context.resolve('logger').then(log => log('Hello world'));
resolveAll(condopt) → {Promise.<Array.<any>>}
Resolves any dependencies that match the given condition.
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
cond |
Condition
|
<optional> |
The condition to match. |
Returns:
- Type:
-
Promise.<Array.<any>>
The dependencies matching the condition.
Example
context.resolveAll(['log', 'debug']).then(([log, debug]) => {
debug('Print greeting message');
log('Hello world');
});
using(cond, consumer)
Wraps function to automatically resolve dependencies and inject into arguments.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
cond |
Condition
|
|
consumer |
function
|
Example
const greet = context.using(['log', 'debug'], (log, debug, name) => {
debug('Print greeting message');
log(`Hello ${name}`);
});
greet('world');