Utility for loading and initializing minioc aware modules (nodejs).
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
fulfill logic will call the
$init function as soon as it is able to inject the dependencies indicated by the
$init function's signature.
var loader = basePath: __dirnameminioc = loaderminioc // minioc is exported for convenience;loader;loader;// standard minioc here, our startup function has two// dependencies that will be injected by minioc...minioc;
npm install minioc-loader
basePath- required base directory path, used to resolve relative paths during
loadSync- loads a module or a directory according to the minioc-loader convention
container(argument 0) - a minioc container used when initializing modules
path(argument 1) - a path (relative to loader's
basePath) to be loaded. Can be a .js module or a directory.
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
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
this is bound to the IoC container previously given to
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.
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
fulfill operation. When the loader calls minioc's
fulfill, it always provides two options:
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.
$init function fails to bind and call the
next callback, it effectively cancels the loading process.
You may also find these other projects useful...