Module: core

A lightweight Ports and Adapters Container for Node

Methods

(static) start(adapters, jobs, terminators, endCb)

Run an npac application

First setup the adapters, then run the jobs if they are available. If any of the errors returns with error, then immediately stops the execution. If endCb defined, then calls it, with the list of results of every jobs. In case of error occured, and no endCb defined then it throws and error instead of returning.

Parameters:
Name Type Description
adapters Array

The list of adapters, (including the executives) that make the context. Default: [].

jobs Array

The list of job functions to execute. Default: []. Every job function must have the following signature: (ctx: {Object}, cb: {Function}), where ctx is the context object, and cb is an error-first callback, with the results of the call as a second parameters.

terminators Array

The list of terminator functions to execute during the shutdown phase. Default: []. Every terminator function must have the following signature: (ctx: {Object}, cb: {Function}), where ctx is the context object, and cb is an error-first callback, with the results of the call as a second parameters.

endCb function

An error-first callback to call with the result of the call, when the function finished. Default: null.

(inner) prepareForTermination(terminators, endCb)

Prepare for termination

Parameters:
Name Type Description
terminators Array

The array of terminators functions

endCb function

An error-first callback function to call when the shutdown process finished.

(inner) runJobs(jobs) → {function}

Prepare jobs to run in series.

Creates a function that will map through the list of jobs, and executes them one ofter the other, in series. The results will be collected into an array, that will be returned as a whole, after the execution of the last job.

Parameters:
Name Type Description
jobs Array

An array of job functions.

Returns:
  • An asynchronous function that has two parameters: ctx: {Object} that is the context object, and endCb: {Function}, that is an error-first callback to return with the results of the job execution.
Type
function

(inner) setupAdapters(ctx, adapters, endCb)

Setup the adapters

The function calls the adapter functions in the order they are defined by the adapters argument. Each adapter function gets the context object, and can use all the APIs that are available through it. It means, each adapter that provides an API that is required by an other adapter function, must be called earlier. It can be achieved by placing them into the adapters array before the function that is willing to use that.

Each adapter function must call the next callback function, when it finishes its job. The next function has two arguments: next(err: {Object}, ctxExtension: {Object})

  • err is an error object, or null, in case of successful execution,
  • ctxExtension is a partial context object, that the adapter would like to merge with the context. The start() function will merge the original context object with the ctxExtension, and the next adapter function in the pipeline will be called with this extended context object.
Parameters:
Name Type Description
ctx Object

The initial context

adapters Array

The array of adapter functions

endCb function

An error-first callback function to call when the start process finished.