xcatalog
Simple dependency injection container for nodejs4+
Installation
$ npm install xcatalog --save
Setting up
There are to different ways to load references into the xcatalog:
Direct configuration
xcatalog.set(id, type, ref, deps)
"use strict"; //A.js { thisbar = str; } { return a + " " + b;} //config.jsconst xcatalog = ;xcatalog ;; console; // "BANANA"console; // "BANANA BANANA"
Annotations
"use strict"; //A.js { thisfoo = "BANANA"; } A$xcatalog = id: "a" type: "singleton" ; //B.js { thisbar = afoo; } B$xcatalog = id: "b" type: "singleton" inject: "a" ; //config.jsconst xcatalog = ; //xcatalog.load(require("./A")).load(require("./B"));xcatalog; console; // "BANANA"
Load directories
Use xcatalog.loaddir(path)
to load all resouces with $catalog
annotation in the given path
.
This function will require
every .js
file in the path
and sub directories.
Getting instances
"use strict";const xcatalog = ; //Get service instance already built with dependenciesconst myService = ;
Dealing with Promises
If any reference returns a promise the xcatalog
cannot be used until is ready.
xcatalog.ready()
returns a promise, that promise is fulfilled when the xcatalog is fully available or rejected is any pending promise es rejected.
"use strict";const xcatalog = ;const promise = Promise; xcatalog; //Get service instance already builded with dependenciesxcatalog;