caf_components/gen_loader

Generic component that loads component descriptions and their implementations.

The strategy for finding these artifacts is straighforward. We configure a sequence of module objects, and we keep on calling the method require in each of them until one returns it.

If none succeeds, we default to the standard require that throws an exception if not found.

How to obtain module objects? Packages export them in a static method, for example:

  exports.getModule = function() {
      return module;
  }

The goal is to allow the application to override anything, but at the same time provide sensible defaults. These defaults may be hidden in the directories of other packages, and packages should export their own module to make them accessable.

A package can export many resources using the # qualifier. For example, caf_components#async first resolves caf_components as above, and then reads the property async on the resolved object.

Source:

Extends

Methods

(static) create($opt, specopt) → {Object}

Source:

Helper constructor method for a loader component.

Description of types in file types.js.

Parameters:
Name Type Attributes Description
$ ctxType <optional>

A context containing references to other components.

spec specType <optional>

Configuration data for this component.

Throws:

If inputs are invalid.

Type
Error
Returns:

A new generic component.

Type
Object

__ca_firstModulePath__() → {string}

Source:

Returns the file path of the top level module.

In a typical CAF application this path shows where the ca_methods.js has been defined, e.g., <app_dir>/lib.

Returns:

The file path of the top level module.

Type
string

__ca_getModuleIndex__() → {Object.<string, Object>}

Source:

Maps names to modules for successfully loaded artifacts.

Returns:

A mapping from artifact names to modules.

Type
Object.<string, Object>

__ca_loadComponent__(compSpec, cb)

Source:

Loads, initializes, and registers a new component.

It uses a convention for compSpec.module of the form module_name#method1#method2 that resolves to the following constructor require("module_name").method1.method2.newInstance()

Parameters:
Name Type Description
compSpec specType

A description of the component.

cb cbType

A callback to return the component or an error.

__ca_loadDescription__(fileName, resolve, specopt) → {Object}

Source:

Loads a JSON component description.

If needed, it also resolves the description by applying templates, following links, reading environment properties, and passing spec optional arguments.

Parameters:
Name Type Attributes Description
fileName string

A file name with a '.json' extension.

resolve boolean

True if a fully resolved description is needed.

spec specDeltaType <optional>

An optional description to be merged-in with the result.

Throws:

If it cannot find the description or parsing error.

Type
Error
Returns:

A parsed component description.

Type
Object

__ca_loadResource__(resourceName) → {Object}

Source:

Loads a resource that could be loaded with require, e.g., a JSON description or a class module.

It keeps an internal cache of resources that clears when the module list changes. See __ca_setModules__ .

Parameters:
Name Type Description
resourceName string

The name of the resource to load.

Throws:

When it cannot find the resource

Type
Error
Returns:

A loaded resource.

Type
Object

__ca_registerLogger__(logger)

Source:

Register the logger component.

The loader is typically the first component that is loaded, i.e., before the logger, but we want the loader to log error messages. Therefore, a explicit late registration is needed.

It can only set once for security reasons.

Parameters:
Name Type Description
logger Object

A logger component.

__ca_setModules__(modulesArray)

Source:

Sets a list of module resolvers that should be used in sequence to require descriptions and implementations.

Parameters:
Name Type Description
modulesArray Array.<Object>

A sequence of module objects to require descriptions and implementations.

__ca_setStaticArtifacts__(staticArtif) → {Object.<string, Object>}

Source:

Sets static values for a given set of artifacts.

The goal is to avoid calling require, enabling tools like browserify that cannot dynamically load resources.

Parameters:
Name Type Description
staticArtif Object.<string, Object>

Static values for a set of artifacts.

Returns:

The previous static values.

Type
Object.<string, Object>