Class: Phase

Phase

A slice of time in an application. Provides hooks to allow functions to be executed before, during and after, the defined slice.

var Phase = require('loopback-phase').Phase;
var myPhase = new Phase('my-phase');
Arguments
Name Type Description
id String

The name or identifier of the Phase.

Class Properties
Name Type Description
next Phase

The Phase to be executed after this Phase.

id String

The name or identifier of the Phase.

Class: Phase Instance Methods

phase.after(handler)

Register a phase handler to be executed after the phase completes. See register() for an example.

Arguments
Name Type Description
handler Function

phase.before(handler)

Register a phase handler to be executed before the phase begins. See register() for an example.

Arguments
Name Type Description
handler Function

phase.run([context], callback)

Begin the execution of a phase and its handlers. Provide a context object to be passed as the first argument for each handler function.

Arguments
Name Type Description
[context] Object

The scope applied to each handler function.

callback Function
Callback
Name Type Description
err Error

Any Error that occured during the execution of the phase.

phase.toString()

Return the Phase as a string.

phase.use()

Register a phase handler. The handler will be executed once the phase is launched. Handlers will be executed in parralel and must callback once complete. If the handler calls back with an error, the phase will immediately hault execution and call the callback provided to phase.run(callback).

Example

phase.use(function(ctx, next) {
  // specify an error if one occurred...
  var err = null;
  console.log(ctx.message, 'world!'); // => hello world
  next(err);
});

phase.run({message: 'hello'}, function(err) {
  if(err) return console.error('phase has errored', err);
  console.log('phase has finished');
});

Class: PhaseList

PhaseList

An ordered list of phases.

var PhaseList = require('loopback-phase').PhaseList;
var phases = new PhaseList();
phases.add('my-phase');

Class: PhaseList Instance Methods

phaseList.add(phase)

Add one or more phases to the list.

Arguments
Name Type Description
phase Phase or String or Array.<String>

The phase (or phases) to be added.

Returns
Name Type Description
result Phase or Array.<Phase>

The added phase or phases.

phaseList.find(id)

Find a Phase from the list.

Arguments
Name Type Description
id String

The phase identifier

Returns
Name Type Description
result Phase

The Phase with the given id.

phaseList.findOrAdd(id)

Find or add a Phase from/into the list.

Arguments
Name Type Description
id String

The phase identifier

Returns
Name Type Description
result Phase

The Phase with the given id.

phaseList.first()

Get the first Phase in the list.

Returns
Name Type Description
result Phase

The first phase.

phaseList.getPhaseNames()

Get an array of phase identifiers.

Returns
Name Type Description
result Array.<String>

phaseNames

phaseList.last()

Get the last Phase in the list.

Returns
Name Type Description
result Phase

The last phase.

phaseList.remove(phase)

Remove a Phase from the list.

Arguments
Name Type Description
phase Phase or String

The phase to be removed.

Returns
Name Type Description
result Phase

The removed phase.

phaseList.run([context], cb)

Launch the phases contained in the list. If there are no phases in the list process.nextTick is called with the provided callback.

Arguments
Name Type Description
[context] Object

The context of each Phase handler.

cb Function
Callback
Name Type Description
err Error

Any error that occured during a phase contained in the list.

phaseList.toArray()

Get the list of phases as an array of Phase objects.

Returns
Name Type Description
result Array.<Phase>

An array of phases.