mirror of
https://github.com/atom/atom.git
synced 2026-01-23 22:08:08 -05:00
There still appear to be crashes occurring when using libgit2 from multiple workers at the same time. So only start a new status worker once the current one completes if a refresh was requested while a worker was running. Closes #367
58 lines
1.2 KiB
CoffeeScript
58 lines
1.2 KiB
CoffeeScript
_ = require 'underscore'
|
|
EventEmitter = require 'event-emitter'
|
|
|
|
module.exports =
|
|
class Task
|
|
aborted: false
|
|
|
|
constructor: (@path) ->
|
|
|
|
start: ->
|
|
throw new Error("Task already started") if @worker?
|
|
|
|
@worker = new Worker(require.getPath('task-shell'))
|
|
@worker.onmessage = ({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:
|
|
resourcePath: window.resourcePath
|
|
navigator:
|
|
userAgent: navigator.userAgent
|
|
requirePath: require.getPath('require')
|
|
handlerPath: @path
|
|
|
|
started: ->
|
|
|
|
onMessage: (message) ->
|
|
|
|
callWorkerMethod: (method, args...) ->
|
|
@postMessage({method, args})
|
|
|
|
postMessage: (data) ->
|
|
@worker.postMessage(data)
|
|
|
|
abort: ->
|
|
@aborted = true
|
|
|
|
done: ->
|
|
@abort()
|
|
@worker?.terminate()
|
|
@worker = null
|
|
@trigger 'task-completed'
|
|
|
|
_.extend Task.prototype, EventEmitter
|