Yadic
Yadic is a minimalistic JavaScript implementation of Dependency Injection Container with async components in mind.
Installation
npm install --save yadic
Usage
Basic
/* components/componentA */ { // ...}Constructor'@type' = 'constructor';Constructor'@inject' = 'httpRequest' 'componentB';
/* components/componentB */ { return {};}factory'@type' = 'factory';factory'@singleton' = true;
/* index */const Yadic = ; var yadic = componentA: httpRequest: ; // you can add modules to container dynamicallyyadic; yadic;
Module can be described with annotations.
@type
- indicates if function should be treated as factory or constructor (therefore allowed values are 'constructor' or 'factory')@singleton
- indicates if component should be instantiated only once@inject
- list of components that should be injected into factory/constructor@yadic
- defines local modules, see below
Module that is not a function or is a function without @type
annotation is treated as singleton.
Local modules
You can define local modules by using @yadic
annotation.
Local modules are visible only for module in which they were defined, but they can use modules that are part of their parent's container.
/* components/componentA */{// ...}Constructor'@type' = 'constructor';Constructor'@inject' = 'componentB' 'localComponent';Constructor'@yadic' ='localComponent':;
/* components/componentsA/localComponent */{// ...}LocalFactory'@type' = 'factory';LocalFactory'@inject' = 'componentB';
/* index */const Yadic = ;var yadic =componentA:componentB:;
Yadic chaining
You can create yadic chain, that works similar to prototype chain.
var protoYadic = componentA: ;var yadic = componentB: protoYadic; // will return component from protoYadicyadic;