2016-01-27 10:29:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Definition of the Verb class.
|
|
|
|
@module verbs/verb
|
|
|
|
@license MIT. See LICENSE.md for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function() {
|
2016-02-02 04:16:49 +00:00
|
|
|
var EVENTS, HMEVENT, Promise, Verb;
|
2016-01-27 10:29:26 +00:00
|
|
|
|
|
|
|
EVENTS = require('events');
|
|
|
|
|
2016-01-31 13:37:12 +00:00
|
|
|
HMEVENT = require('../core/event-codes');
|
|
|
|
|
2016-02-02 02:14:36 +00:00
|
|
|
Promise = require('pinkie-promise');
|
|
|
|
|
2016-01-27 10:29:26 +00:00
|
|
|
|
|
|
|
/**
|
2016-02-03 00:02:56 +00:00
|
|
|
An abstract invokable verb.
|
|
|
|
Provides base class functionality for verbs. Provide common services such as
|
|
|
|
error handling, event management, and promise support.
|
2016-01-27 10:29:26 +00:00
|
|
|
@class Verb
|
|
|
|
*/
|
|
|
|
|
2016-02-02 04:16:49 +00:00
|
|
|
module.exports = Verb = (function() {
|
2016-01-27 10:29:26 +00:00
|
|
|
|
|
|
|
/** Constructor. Automatically called at creation. */
|
2016-02-02 04:16:49 +00:00
|
|
|
function Verb(moniker, workhorse) {
|
2016-01-27 10:29:26 +00:00
|
|
|
this.moniker = moniker;
|
2016-01-31 13:37:12 +00:00
|
|
|
this.workhorse = workhorse;
|
2016-02-02 02:14:36 +00:00
|
|
|
this.emitter = new EVENTS.EventEmitter();
|
2016-02-02 04:16:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-31 13:37:12 +00:00
|
|
|
|
|
|
|
/** Invoke the command. */
|
2016-02-02 04:16:49 +00:00
|
|
|
|
|
|
|
Verb.prototype.invoke = function() {
|
2016-02-02 02:14:36 +00:00
|
|
|
var argsArray, that;
|
2016-01-31 13:37:12 +00:00
|
|
|
this.stat(HMEVENT.begin, {
|
|
|
|
cmd: this.moniker
|
|
|
|
});
|
2016-02-02 02:14:36 +00:00
|
|
|
argsArray = Array.prototype.slice.call(arguments);
|
|
|
|
that = this;
|
|
|
|
return this.promise = new Promise(function(res, rej) {
|
|
|
|
that.resolve = res;
|
|
|
|
that.reject = rej;
|
|
|
|
that.workhorse.apply(that, argsArray);
|
|
|
|
});
|
2016-02-02 04:16:49 +00:00
|
|
|
};
|
|
|
|
|
2016-01-27 10:29:26 +00:00
|
|
|
|
|
|
|
/** Forward subscriptions to the event emitter. */
|
2016-02-02 04:16:49 +00:00
|
|
|
|
|
|
|
Verb.prototype.on = function() {
|
2016-01-27 10:29:26 +00:00
|
|
|
return this.emitter.on.apply(this.emitter, arguments);
|
2016-02-02 04:16:49 +00:00
|
|
|
};
|
|
|
|
|
2016-01-27 10:29:26 +00:00
|
|
|
|
|
|
|
/** Fire an arbitrary event, scoped to "hmr:". */
|
2016-02-02 04:16:49 +00:00
|
|
|
|
|
|
|
Verb.prototype.fire = function(evtName, payload) {
|
2016-01-27 10:29:26 +00:00
|
|
|
payload = payload || {};
|
|
|
|
payload.cmd = this.moniker;
|
|
|
|
this.emitter.emit('hmr:' + evtName, payload);
|
|
|
|
return true;
|
2016-02-02 04:16:49 +00:00
|
|
|
};
|
|
|
|
|
2016-01-27 10:29:26 +00:00
|
|
|
|
|
|
|
/** Handle an error condition. */
|
2016-02-02 04:16:49 +00:00
|
|
|
|
|
|
|
Verb.prototype.err = function(errorCode, payload, hot) {
|
2016-01-27 10:29:26 +00:00
|
|
|
payload = payload || {};
|
|
|
|
payload.sub = payload.fluenterror = errorCode;
|
|
|
|
payload["throw"] = hot;
|
2016-02-02 02:14:36 +00:00
|
|
|
this.setError(errorCode, payload);
|
|
|
|
if (payload.quit) {
|
2016-02-02 03:56:08 +00:00
|
|
|
this.reject(errorCode);
|
2016-02-02 02:14:36 +00:00
|
|
|
}
|
2016-01-27 10:29:26 +00:00
|
|
|
this.fire('error', payload);
|
|
|
|
if (hot) {
|
|
|
|
throw payload;
|
|
|
|
}
|
|
|
|
return true;
|
2016-02-02 04:16:49 +00:00
|
|
|
};
|
|
|
|
|
2016-01-27 10:29:26 +00:00
|
|
|
|
|
|
|
/** Fire the 'hmr:status' error event. */
|
2016-02-02 04:16:49 +00:00
|
|
|
|
|
|
|
Verb.prototype.stat = function(subEvent, payload) {
|
2016-01-27 10:29:26 +00:00
|
|
|
payload = payload || {};
|
|
|
|
payload.sub = subEvent;
|
|
|
|
this.fire('status', payload);
|
|
|
|
return true;
|
2016-02-02 04:16:49 +00:00
|
|
|
};
|
|
|
|
|
2016-02-03 00:02:56 +00:00
|
|
|
|
|
|
|
/** Has an error occurred during this verb invocation? */
|
|
|
|
|
2016-02-02 04:16:49 +00:00
|
|
|
Verb.prototype.hasError = function() {
|
2016-02-02 02:14:36 +00:00
|
|
|
return this.errorCode || this.errorObj;
|
2016-02-02 04:16:49 +00:00
|
|
|
};
|
|
|
|
|
2016-01-27 10:29:26 +00:00
|
|
|
|
|
|
|
/** Associate error info with the invocation. */
|
2016-02-02 04:16:49 +00:00
|
|
|
|
|
|
|
Verb.prototype.setError = function(code, obj) {
|
2016-01-27 10:29:26 +00:00
|
|
|
this.errorCode = code;
|
|
|
|
this.errorObj = obj;
|
2016-02-02 04:16:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return Verb;
|
|
|
|
|
|
|
|
})();
|
2016-01-27 10:29:26 +00:00
|
|
|
|
|
|
|
}).call(this);
|
2016-02-02 02:14:36 +00:00
|
|
|
|
|
|
|
//# sourceMappingURL=verb.js.map
|