 
     @handleEvents()
 
+  # Private: Routes messages from the child to the appropriate event.
   handleEvents: ->
     @childProcess.removeAllListeners()
     @childProcess.on 'message', ({event, args}) =>
       @trigger(event, args...)
 
+  # Public: Starts the task.
+  #
+  # * args:
+  #   The Array of arguments to pass to the function exported by the script. If
+  #   the last argument is a function, its removed from the array and called
+  #   upon completion (and replaces the complete function on the task instance).
   start: (args...) ->
     throw new Error("Cannot start terminated process") unless @childProcess?
 

# Public: Run a node script in a separate process.
#
# Used by the fuzzy-finder.
module.exports =
class Task
  _.extend @prototype, EventEmitter

  @once: (taskPath, args...) ->
    task = new Task(taskPath)
    task.one 'task:completed', -> task.terminate()
    task.start(args...)
    task

  callback: null

  constructor: (taskPath) ->
    bootstrap = """
      require('coffee-script');

    @callback = args.pop() if _.isFunction(args[args.length - 1])
    @childProcess.send({args})

  # Public: Forcefully stop the running task.
  #
  # No events are emitted.
  terminate: ->
    return unless @childProcess?

