minioc-loader
Utility for loading and initializing minioc aware modules (nodejs).
minioc is a miniature IoC container for nodejs - see the github repo for more info.
Loader
The loader loads either an individual .js files or a directory. Loader treats each .js file as a module, using node's require
function. Loader works by convention; it looks for a top-level exported function with the name $init
. If a module makes such an export, loader simply hands the function to minioc
to fulfill
.
minioc
's fulfill
logic will call the $init
function as soon as it is able to inject the dependencies indicated by the $init
function's signature.
Usage
var loader = basePath: __dirname minioc = loaderminioc // minioc is exported for convenience; loader;loader; // standard minioc here, our startup function has two// dependencies that will be injected by minioc...minioc;
Install
npm install minioc-loader
Options
basePath
- required base directory path, used to resolve relative paths duringloadSync
calls
Operations
loadSync
- loads a module or a directory according to the minioc-loader conventioncontainer
(argument 0) - a minioc container used when initializing modulespath
(argument 1) - a path (relative to loader'sbasePath
) to be loaded. Can be a .js module or a directory.
Conventions
When calling loadSync
...
If path
is a .js file then the loader will treat the file as a module and load it using node's require
function. If the module exports a function with the name $init
then the loader will tell minioc
to fulfill
it. Minioc identifies dependencies by convention; any named argument will be injected if there is a matching registration in the container. Arguments beginning with a dollar sign $
is considered required dependencies and minioc will call the function as soon as minioc is able to inject all of the required dependencies. When minioc calls the exported $init
function, this
is bound to the IoC container previously given to loadSync
.
If path
is a directory, the loader will look for an index.js
file and if present, the index file will be processed. For initializing modules other than index.js
, see the discussion on controlling what gets initialized below.
Study the example code to understand it fully, in particular, notice that the order in which registrations occur on the container is unimportant because minioc will fulfill all requests as soon as their dependencies can be met.
Controlling What Gets Initialized in Directories
The loader will always invoke the first exported $init
function that it finds in a directory. It begins by loading an index.js
file if one exists, otherwise it processes all files in alphabetical (string sort) order.
If the directory contains sub-directories, they are processed in-line as the files are being processed. The loader process is repeated for each sub-directory.
For a given directory, the first exported $init
function is scheduled. The loader does not directly call the $init
function, instead it uses the minioc container
's fulfill
operation. When the loader calls minioc's fulfill
, it always provides two options: next
and loader
.
next -- a callback function used to tell the loader to continue processing the directory.
loader -- the loader itself enabling you to load other files and directories.
When an $init
function fails to bind and call the next
callback, it effectively cancels the loading process.
Building Blocks
You may also find these other projects useful...
- minioc-broadway - Broadway plugin for adding minioc and minioc-loader to broadway/flatiron apps.