HackMyResume/dist/verbs/verb.js

101 lines
2.5 KiB
JavaScript
Raw Normal View History

2016-01-27 10:29:26 +00:00
(function() {
2018-02-12 05:05:29 +00:00
/**
Definition of the Verb class.
@module verbs/verb
@license MIT. See LICENSE.md for details.
*/
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
2018-02-12 05:05:29 +00:00
*/
module.exports = Verb = class Verb {
2016-01-27 10:29:26 +00:00
/** Constructor. Automatically called at creation. */
2018-02-12 05:05:29 +00:00
constructor(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. */
2018-02-12 05:05:29 +00:00
invoke() {
2016-02-02 02:14:36 +00:00
var argsArray, that;
2018-02-12 05:05:29 +00:00
// Sent the 'begin' notification for this verb
2016-01-31 13:37:12 +00:00
this.stat(HMEVENT.begin, {
cmd: this.moniker
});
2018-02-12 05:05:29 +00:00
// Prepare command arguments
2016-02-02 02:14:36 +00:00
argsArray = Array.prototype.slice.call(arguments);
2018-02-12 05:05:29 +00:00
// Create a promise for this verb instance
2016-02-02 02:14:36 +00:00
that = this;
return this.promise = new Promise(function(res, rej) {
that.resolve = res;
that.reject = rej;
that.workhorse.apply(that, argsArray);
});
2018-02-12 05:05:29 +00:00
}
2016-01-27 10:29:26 +00:00
/** Forward subscriptions to the event emitter. */
2018-02-12 05:05:29 +00:00
on() {
2016-01-27 10:29:26 +00:00
return this.emitter.on.apply(this.emitter, arguments);
2018-02-12 05:05:29 +00:00
}
2016-01-27 10:29:26 +00:00
/** Fire an arbitrary event, scoped to "hmr:". */
2018-02-12 05:05:29 +00:00
fire(evtName, payload) {
2016-01-27 10:29:26 +00:00
payload = payload || {};
payload.cmd = this.moniker;
this.emitter.emit('hmr:' + evtName, payload);
return true;
2018-02-12 05:05:29 +00:00
}
2016-01-27 10:29:26 +00:00
/** Handle an error condition. */
2018-02-12 05:05:29 +00:00
err(errorCode, payload, hot) {
2016-01-27 10:29:26 +00:00
payload = payload || {};
payload.sub = payload.fluenterror = errorCode;
2018-02-12 05:05:29 +00:00
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;
2018-02-12 05:05:29 +00:00
}
2016-01-27 10:29:26 +00:00
/** Fire the 'hmr:status' error event. */
2018-02-12 05:05:29 +00:00
stat(subEvent, payload) {
2016-01-27 10:29:26 +00:00
payload = payload || {};
payload.sub = subEvent;
this.fire('status', payload);
return true;
2018-02-12 05:05:29 +00:00
}
2016-02-03 00:02:56 +00:00
/** Has an error occurred during this verb invocation? */
2018-02-12 05:05:29 +00:00
hasError() {
2016-02-02 02:14:36 +00:00
return this.errorCode || this.errorObj;
2018-02-12 05:05:29 +00:00
}
2016-01-27 10:29:26 +00:00
/** Associate error info with the invocation. */
2018-02-12 05:05:29 +00:00
setError(code, obj) {
2016-01-27 10:29:26 +00:00
this.errorCode = code;
this.errorObj = obj;
2018-02-12 05:05:29 +00:00
}
2018-02-12 05:05:29 +00:00
};
2016-01-27 10:29:26 +00:00
}).call(this);
2016-02-02 02:14:36 +00:00
//# sourceMappingURL=verb.js.map