mirror of
https://github.com/atom/atom.git
synced 2026-01-23 13:58:08 -05:00
62 lines
1.3 KiB
CoffeeScript
62 lines
1.3 KiB
CoffeeScript
_ = require 'underscore'
|
|
child_process = require 'child_process'
|
|
EventEmitter = require 'event-emitter'
|
|
fsUtils = require 'fs-utils'
|
|
|
|
module.exports =
|
|
class Task
|
|
aborted: false
|
|
|
|
constructor: (@path) ->
|
|
|
|
start: ->
|
|
throw new Error("Task already started") if @worker?
|
|
|
|
# Equivalent with node --eval "...".
|
|
blob = "require('coffee-script'); require('task-shell');"
|
|
@worker = child_process.fork '--eval', [ blob, '--harmony_collections' ], cwd: __dirname
|
|
|
|
@worker.on 'message', (data) =>
|
|
if @aborted
|
|
@done()
|
|
return
|
|
|
|
if data.method and this[data.method]
|
|
this[data.method](data.args...)
|
|
else
|
|
@onMessage(data)
|
|
|
|
@startWorker()
|
|
|
|
log: -> console.log(arguments...)
|
|
warn: -> console.warn(arguments...)
|
|
error: -> console.error(arguments...)
|
|
|
|
startWorker: ->
|
|
@callWorkerMethod 'start',
|
|
globals:
|
|
navigator:
|
|
userAgent: navigator.userAgent
|
|
handlerPath: @path
|
|
|
|
started: ->
|
|
|
|
onMessage: (message) ->
|
|
|
|
callWorkerMethod: (method, args...) ->
|
|
@postMessage({method, args})
|
|
|
|
postMessage: (data) ->
|
|
@worker.send(data)
|
|
|
|
abort: ->
|
|
@aborted = true
|
|
|
|
done: ->
|
|
@abort()
|
|
@worker?.kill()
|
|
@worker = null
|
|
@trigger 'task-completed'
|
|
|
|
_.extend Task.prototype, EventEmitter
|