1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-11-26 02:10:09 +00:00
HackMyResume/dist/verbs/verb.js

119 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-01-27 10:29:26 +00:00
/**
Definition of the Verb class.
@module verbs/verb
@license MIT. See LICENSE.md for details.
*/
(function() {
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
*/
module.exports = Verb = (function() {
2016-01-27 10:29:26 +00:00
/** Constructor. Automatically called at creation. */
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();
return;
}
2016-01-31 13:37:12 +00:00
/** Invoke the command. */
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-01-27 10:29:26 +00:00
/** Forward subscriptions to the event emitter. */
Verb.prototype.on = function() {
2016-01-27 10:29:26 +00:00
return this.emitter.on.apply(this.emitter, arguments);
};
2016-01-27 10:29:26 +00:00
/** Fire an arbitrary event, scoped to "hmr:". */
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-01-27 10:29:26 +00:00
/** Handle an error condition. */
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-01-27 10:29:26 +00:00
/** Fire the 'hmr:status' error event. */
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-03 00:02:56 +00:00
/** Has an error occurred during this verb invocation? */
Verb.prototype.hasError = function() {
2016-02-02 02:14:36 +00:00
return this.errorCode || this.errorObj;
};
2016-01-27 10:29:26 +00:00
/** Associate error info with the invocation. */
Verb.prototype.setError = function(code, obj) {
2016-01-27 10:29:26 +00:00
this.errorCode = code;
this.errorObj = obj;
};
return Verb;
})();
2016-01-27 10:29:26 +00:00
}).call(this);
2016-02-02 02:14:36 +00:00
//# sourceMappingURL=verb.js.map