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 factory method named create()
.
Then, we use the following pattern:
const genXX = require('./gen_XX');
...
exports.newInstance = async function($, spec) {
try {
const that = genXX.create($, spec);
// do asynchronous initialization of 'that' and then return tuple
...
return [err, that];
} catch(err) {
return [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
- Source:
True if this component has already been shutdown.
Type:
- boolean
Methods
__ca_checkup__(data, cbopt) → {Promise.<Array.<Object>>}
- Source:
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 | A hint on how to perform the checkup or |
|
cb |
cbType |
<optional> |
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. If missing, it returns a
promise with the equivalent array tuple |
Returns:
An optional promise that resolves to
an array tuple [err, data]
when the callback is missing. Rejected
promises only propagate unhandled exception errors.
- Type
- Promise.<Array.<Object>>
__ca_getSpec__() → {specType}
- Source:
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.
Returns:
Read-only configuration data for this component.
- Type
- specType
__ca_shutdown__(data, cbopt) → {Promise.<Array.<Object>>}
- Source:
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 | A hint on how to perform the shutdown or |
|
cb |
cbType |
<optional> |
An optional callback invoked after
shutdown, with an error if it failed. If missing, it returns a
promise with the equivalent array tuple |
Returns:
An optional promise that resolves to
an array tuple [err, data]
when the callback is missing. Rejected
promises only propagate unhandled exception errors.
- Type
- Promise.<Array.<Object>>
(static) create($, spec) → {Object}
- Source:
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. |
Throws:
-
If inputs are invalid.
- Type
- Error
Returns:
A new generic component.
- Type
- Object