2016-01-29 20:23:57 +00:00
|
|
|
###*
|
|
|
|
Definition of the Verb class.
|
|
|
|
@module verbs/verb
|
|
|
|
@license MIT. See LICENSE.md for details.
|
|
|
|
###
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Use J. Resig's nifty class implementation
|
|
|
|
Class = require '../utils/class'
|
|
|
|
EVENTS = require 'events'
|
2016-01-31 13:37:12 +00:00
|
|
|
HMEVENT = require '../core/event-codes'
|
2016-01-29 20:23:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
###*
|
|
|
|
An instantiation of a HackMyResume command.
|
|
|
|
@class Verb
|
|
|
|
###
|
|
|
|
Verb = module.exports = Class.extend
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
###* Constructor. Automatically called at creation. ###
|
2016-01-31 13:37:12 +00:00
|
|
|
init: ( moniker, workhorse ) ->
|
|
|
|
@moniker = moniker
|
|
|
|
@emitter = new EVENTS.EventEmitter()
|
|
|
|
@workhorse = workhorse
|
2016-01-29 20:23:57 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-01-31 13:37:12 +00:00
|
|
|
###* Invoke the command. ###
|
|
|
|
invoke: ->
|
|
|
|
@stat HMEVENT.begin, cmd: @moniker
|
|
|
|
ret = @workhorse.apply @, arguments
|
|
|
|
@stat HMEVENT.end
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-01-29 20:23:57 +00:00
|
|
|
###* Forward subscriptions to the event emitter. ###
|
|
|
|
on: ->
|
2016-01-31 13:37:12 +00:00
|
|
|
@emitter.on.apply @emitter, arguments
|
2016-01-29 20:23:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
###* Fire an arbitrary event, scoped to "hmr:". ###
|
|
|
|
fire: (evtName, payload) ->
|
|
|
|
payload = payload || { }
|
2016-01-31 13:37:12 +00:00
|
|
|
payload.cmd = @moniker
|
|
|
|
@emitter.emit 'hmr:' + evtName, payload
|
2016-01-29 20:23:57 +00:00
|
|
|
true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
###* Handle an error condition. ###
|
|
|
|
err: ( errorCode, payload, hot ) ->
|
|
|
|
payload = payload || { }
|
|
|
|
payload.sub = payload.fluenterror = errorCode
|
|
|
|
payload.throw = hot
|
|
|
|
this.fire 'error', payload
|
|
|
|
if hot
|
|
|
|
throw payload
|
|
|
|
true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
###* Fire the 'hmr:status' error event. ###
|
|
|
|
stat: ( subEvent, payload ) ->
|
|
|
|
payload = payload || { }
|
|
|
|
payload.sub = subEvent
|
2016-01-31 13:37:12 +00:00
|
|
|
@fire 'status', payload
|
2016-01-29 20:23:57 +00:00
|
|
|
true
|
|
|
|
|
|
|
|
|
2016-01-31 13:37:12 +00:00
|
|
|
|
2016-01-29 20:23:57 +00:00
|
|
|
###* Associate error info with the invocation. ###
|
|
|
|
setError: ( code, obj ) ->
|
|
|
|
@errorCode = code
|
|
|
|
@errorObj = obj
|
|
|
|
return
|