1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-09-29 04:29:12 +01:00
HackMyResume/src/verbs/verb.coffee

100 lines
2.0 KiB
CoffeeScript
Raw Normal View History

###*
Definition of the Verb class.
@module verbs/verb
@license MIT. See LICENSE.md for details.
###
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'
###*
An abstract invokable verb.
Provides base class functionality for verbs. Provide common services such as
error handling, event management, and promise support.
@class Verb
###
module.exports = class Verb
###* Constructor. Automatically called at creation. ###
constructor: ( @moniker, @workhorse ) ->
2016-02-02 02:14:36 +00:00
@emitter = new EVENTS.EventEmitter()
return
2016-01-31 13:37:12 +00:00
###* Invoke the command. ###
invoke: ->
2018-01-30 07:34:58 +00:00
# Sent the 'begin' notification for this verb
2016-01-31 13:37:12 +00:00
@stat HMEVENT.begin, cmd: @moniker
2018-01-30 07:34:58 +00:00
# Prepare command arguments
2016-02-02 02:14:36 +00:00
argsArray = Array::slice.call arguments
2018-01-30 07:34:58 +00:00
# Create a promise for this verb instance
2016-02-02 02:14:36 +00:00
that = @
@promise = new Promise (res, rej) ->
2018-01-30 07:34:58 +00:00
that.resolve = res
that.reject = rej
that.workhorse.apply that, argsArray
return
2016-01-31 13:37:12 +00:00
###* Forward subscriptions to the event emitter. ###
on: -> @emitter.on.apply @emitter, arguments
###* 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
true
###* Handle an error condition. ###
err: ( errorCode, payload, hot ) ->
payload = payload || { }
payload.sub = payload.fluenterror = errorCode
payload.throw = hot
2016-02-02 02:14:36 +00:00
@setError errorCode, payload
if payload.quit
2016-02-02 03:56:08 +00:00
@reject errorCode
2016-02-02 02:14:36 +00:00
@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
true
2016-01-31 13:37:12 +00:00
###* Has an error occurred during this verb invocation? ###
2016-02-02 02:14:36 +00:00
hasError: -> @errorCode || @errorObj
###* Associate error info with the invocation. ###
setError: ( code, obj ) ->
@errorCode = code
@errorObj = obj
return