particle-commands
Version:
Library of UX-neutral commands that provide key functionality for developer tools
119 lines (95 loc) • 3.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Describes the interface expected of objects passed as the command site to a command.
*/
var CommandSite = function () {
function CommandSite() {
_classCallCheck(this, CommandSite);
}
/**
* @abstract
* @param {object} _state Conversation state
* @param {object} _cmd The command that was started
*/
_createClass(CommandSite, [{
key: 'begin',
value: function begin(_state, _cmd) {}
/**
* @abstract
* @param {object} _state Conversation state
* @param {object} _cmd The command that was finished
*/
}, {
key: 'end',
value: function end(_state, _cmd) {}
}, {
key: 'run',
value: async function run(cmd) {
var state = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
try {
await this.begin(state, this);
return await cmd.run(state, this);
} finally {
await this.end(state, this);
}
}
}]);
return CommandSite;
}();
/**
* Encapsulates a single unit of execution. A command is stateless, storing all state
* in the `state` instance passed in.
* The `site` instance is typically a unique interface for each command, that provides the input
* and output required by the command.
*/
var Command = function () {
function Command() {
_classCallCheck(this, Command);
}
_createClass(Command, [{
key: 'run',
/**
* An async function/returns a promise for the command execution.
* @param {object} _state Conversation state
* @param {CommandSite} _site The command interaction site.
* @abstract
*/
value: function run(_state, _site) {}
}]);
return Command;
}();
/**
* wip
*/
var AbstractStatefulCommand = function () {
function AbstractStatefulCommand() {
_classCallCheck(this, AbstractStatefulCommand);
}
_createClass(AbstractStatefulCommand, [{
key: 'create',
value: function create() {
return Promise.reject('not implemented');
}
}, {
key: 'doRun',
value: function doRun() {
// the run method for this command
}
}, {
key: 'run',
value: function run(state, _site) {
if (state === this) {
return this.doRun();
}
}
}]);
return AbstractStatefulCommand;
}();
exports.Command = Command;
exports.AbstractStatefulCommand = AbstractStatefulCommand;
exports.CommandSite = CommandSite;