diff --git a/spec/app/git-spec.coffee b/spec/app/git-spec.coffee index 36ca195e1..04c5596fc 100644 --- a/spec/app/git-spec.coffee +++ b/spec/app/git-spec.coffee @@ -1,5 +1,6 @@ Git = require 'git' fs = require 'fs' +Task = require 'task' describe "Git", -> repo = null @@ -212,3 +213,22 @@ describe "Git", -> expect(statuses[cleanPath]).toBeUndefined() expect(repo.isStatusNew(statuses[newPath])).toBeTruthy() expect(repo.isStatusModified(statuses[modifiedPath])).toBeTruthy() + + it "only starts a single web worker at a time and schedules a restart if one is already running", => + fs.write(modifiedPath, 'making this path modified') + statusHandler = jasmine.createSpy('statusHandler') + repo.on 'statuses-changed', statusHandler + + spyOn(Task.prototype, "start").andCallThrough() + repo.refreshStatus() + expect(Task.prototype.start.callCount).toBe 1 + repo.refreshStatus() + expect(Task.prototype.start.callCount).toBe 1 + repo.refreshStatus() + expect(Task.prototype.start.callCount).toBe 1 + + waitsFor -> + statusHandler.callCount > 0 + + runs -> + expect(Task.prototype.start.callCount).toBe 2 diff --git a/src/app/git.coffee b/src/app/git.coffee index 761084c57..b9f469b1c 100644 --- a/src/app/git.coffee +++ b/src/app/git.coffee @@ -28,6 +28,7 @@ class Git statuses: null upstream: null + statusTask: null constructor: (path, options={}) -> @statuses = {} @@ -58,7 +59,11 @@ class Git @path ?= fs.absolute(@getRepo().getPath()) destroy: -> - @statusTask?.abort() + if @statusTask? + @statusTask.abort() + @statusTask.off() + @statusTask = null + @getRepo().destroy() @repo = null @unsubscribe() @@ -130,8 +135,16 @@ class Git @getRepo().isSubmodule(@relativize(path)) refreshStatus: -> - @statusTask = new RepositoryStatusTask(this) - @statusTask.start() + if @statusTask? + @statusTask.off() + @statusTask.one 'task-completed', => + @statusTask = null + @refreshStatus() + else + @statusTask = new RepositoryStatusTask(this) + @statusTask.one 'task-completed', => + @statusTask = null + @statusTask.start() getDirectoryStatus: (directoryPath) -> directoryPath = "#{directoryPath}/" diff --git a/src/stdlib/task.coffee b/src/stdlib/task.coffee index 3177e50be..eb3e2f3f5 100644 --- a/src/stdlib/task.coffee +++ b/src/stdlib/task.coffee @@ -1,3 +1,6 @@ +_ = require 'underscore' +EventEmitter = require 'event-emitter' + module.exports = class Task aborted: false @@ -49,3 +52,6 @@ class Task @abort() @worker?.terminate() @worker = null + @trigger 'task-completed' + +_.extend Task.prototype, EventEmitter