Module: caf_components/gen_component

Generic base component.

CAF component construction is asynchronous, using a standard factory method named newInstance().

To simplify writing asynchronous constructors we wrap the synchronous bits of a constructor in an internal helper, i.e., a method named constructor().

Then, we use the following pattern:

var genXX = require('./gen_XX');
...
exports.newInstance = function($, spec,  cb) {
    try {
        var that = genXX.constructor($, spec);
     // do asynchronous initialization of 'that' and then call 'cb'
        ...
           cb(err, that);
    } catch(err) {
        cb(err);
    }
}

Internal helpers are defined by convention in files named gen_*, and typically just set up data structures and methods to facilitate the (asynchronous) initialization of the component.

They also check inputs, throwing errors if invalid. It is important to catch these exceptions within the asynchronous constructor, and propagate them in the callback, as shown above.

CAF.js creates objects using a pure functional style because closures enable private state, providing strong security properties.

Source:

Members

__ca_isShutdown__ :boolean

True if this component has already been shutdown.

Type:
  • boolean
Source:

Methods

(static) constructor($, spec) → {Object}

Helper constructor method for a generic component.

Description of types in file types.js.

Parameters:
Name Type Description
$ ctxType

A context containing references to other components.

spec specType

Configuration data for this component.

Source:
Throws:

If inputs are invalid.

Type
Error
Returns:

A new generic component.

Type
Object

__ca_checkup__(dataopt, cb)

Checks the health of this component.

A shutdown component always fails the check.

A failed check typically requires that the caller forces a shutdown, and then creates a new replacement component.

Parameters:
Name Type Attributes Description
data Object <optional>

An optional hint on how to perform the checkup.

cb cbType

A callback invoked after the check, with an error if the component is faulty, or optional info to bubble up in the second callback argument.

Source:

__ca_getSpec__() → {specType}

Gets configuration data for this component. This data is read-only.

Typedef for specType is {{name: string, module: string, description: string, env: Object, components= : Array.}}

Source:
Returns:

Read-only configuration data for this component.

Type
specType

__ca_shutdown__(dataopt, cb)

Forces this component to shutdown.

This action is non-recoverable and idempotent.

After a successful shutdown, this component is deregistered from the original local context $.

If failures occur during shutdown, the parent component should also take a recovery action to clean-up, e.g., retry or propagate shutdown to the parent process.

Parameters:
Name Type Attributes Description
data Object <optional>

An optional hint on how to perform the shutdown.

cb cbType

A callback invoked after shutdown, with an error if it failed.

Source: