1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-06-30 23:40:05 +01:00
HackMyResume/dist/verbs/verb.js
hacksalot 63a0c78fc5 Refactor verbs to CoffeeScript classes.
Retire Resig's class implementation.
2016-02-01 23:16:49 -05:00

115 lines
2.4 KiB
JavaScript

/**
Definition of the Verb class.
@module verbs/verb
@license MIT. See LICENSE.md for details.
*/
(function() {
var EVENTS, HMEVENT, Promise, Verb;
EVENTS = require('events');
HMEVENT = require('../core/event-codes');
Promise = require('pinkie-promise');
/**
An instantiation of a HackMyResume command.
@class Verb
*/
module.exports = Verb = (function() {
/** Constructor. Automatically called at creation. */
function Verb(moniker, workhorse) {
this.moniker = moniker;
this.workhorse = workhorse;
this.emitter = new EVENTS.EventEmitter();
return;
}
/** Invoke the command. */
Verb.prototype.invoke = function() {
var argsArray, that;
this.stat(HMEVENT.begin, {
cmd: this.moniker
});
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);
});
};
/** Forward subscriptions to the event emitter. */
Verb.prototype.on = function() {
return this.emitter.on.apply(this.emitter, arguments);
};
/** Fire an arbitrary event, scoped to "hmr:". */
Verb.prototype.fire = function(evtName, payload) {
payload = payload || {};
payload.cmd = this.moniker;
this.emitter.emit('hmr:' + evtName, payload);
return true;
};
/** Handle an error condition. */
Verb.prototype.err = function(errorCode, payload, hot) {
payload = payload || {};
payload.sub = payload.fluenterror = errorCode;
payload["throw"] = hot;
this.setError(errorCode, payload);
if (payload.quit) {
console.log(payload);
this.reject(errorCode);
}
this.fire('error', payload);
if (hot) {
throw payload;
}
return true;
};
/** Fire the 'hmr:status' error event. */
Verb.prototype.stat = function(subEvent, payload) {
payload = payload || {};
payload.sub = subEvent;
this.fire('status', payload);
return true;
};
Verb.prototype.hasError = function() {
return this.errorCode || this.errorObj;
};
/** Associate error info with the invocation. */
Verb.prototype.setError = function(code, obj) {
this.errorCode = code;
this.errorObj = obj;
};
return Verb;
})();
}).call(this);
//# sourceMappingURL=verb.js.map