Class: Command

Command

Each command processed by the macro-command package, should extend this superclass. All subclasses must override the 'execute()' method, with their specific implementations.

Constructor

(abstract) new Command()

Properties:
Name Type Description
this.result CommandResult
this.ctxt CommandContext
Author:
  • ruckola
Source:

Methods

abort(err) → {Promise}

Will be removed in the next major version. If the execution fails before the command task() has returned a promise, we have to return a rejected promise manually to the client. Since there is no data to return yet, we pass an error object as the value.
Parameters:
Name Type Description
err Error An error object that will be set as the value for the rejected promise.
Deprecated:
  • Yes
Source:
Returns:
- Allways rejects with an error object as its value.
Type
Promise

execute(context) → {Promise}

The execute method is used to run the command. Normally this is taken care of by the controller and you don't have to worry about it at all. When you start developing your own subcommands the following is very important:

1) Each subcommand that extends this abstract Command-class must override the execute()-method. This is the entry and exit point, for your own implementation.
2) The execute() method must have a 'context'-parameter.
3) The execute() method must return a new Promise.
4) The promise must resolve/reject with a value of type CommandResult.
Parameters:
Name Type Description
context CommandContext The context object is used to inject the data that is required to successfully execute the command. It contains all configurations and all commandResult-objects of any previous subcommands in the same macro. So you don need any other parameters. You can even add extra parameters to the context on-the-fly, and the new parameters will be available to any subcommand running after this one. (in the same macro)
Source:
Returns:
- The value of the promise should be this.result in most cases.
Type
Promise
Example

Sample subcommand extending this abstract Command class and overriding the 'execute()' method

Command = require('macro-command').Command;
class Example extends Command {

 execute(context) {

          var key = this.constructor.name;
          this.context = context;
          var self = this;
          var cmdConf = context.parameters.cmdConfiguration;

          return new Promise(function (resolve, reject) {

              //CHECK REQUIRED INPUT PARAMETERS ...

              //DO SOMETHING HERE ...

              self.result.cmd = key;
              self.result.data.push({ 'some example key': 'some example value' });
              self.result.msg = 'The result from ' + key;
              resolve(self.result);

          });
      }
  }
module.exports = Example;

findPrevResult(commandName, contextopt) → {commandResult}

Finds and returns the commandResult of a previous command.
Parameters:
Name Type Attributes Default Description
commandName string The name of the previous command.
context commandContext <optional>
false A context other than the current context.
Source:
Returns:
- The result of the previous command.
Type
commandResult