From e0865e8c38e069e8996eaf6acc3006e615a74f4f Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sun, 7 Apr 2013 16:18:08 +0800 Subject: [PATCH] Use node-pathwatcher. --- package.json | 1 + spec/app/file-spec.coffee | 2 ++ spec/app/text-buffer-spec.coffee | 2 ++ spec/spec-helper.coffee | 5 +++-- src/app/directory.coffee | 7 ++++--- src/app/file.coffee | 15 ++++++++------- src/stdlib/fs-utils.coffee | 8 -------- 7 files changed, 20 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index c691b0b27..c31deeab9 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "async": "0.2.6", "nak": "0.2.12", "spellchecker": "0.2.0", + "pathwatcher": "0.1.4", "plist": "git://github.com/nathansobo/node-plist.git", "space-pen": "git://github.com/nathansobo/space-pen.git" }, diff --git a/spec/app/file-spec.coffee b/spec/app/file-spec.coffee index 9cbd613c0..f47738081 100644 --- a/spec/app/file-spec.coffee +++ b/spec/app/file-spec.coffee @@ -54,6 +54,7 @@ describe 'File', -> waitsFor "remove event", (done) -> file.on 'removed', done it "it updates its path", -> + jasmine.unspy(window, "setTimeout") moveHandler = null moveHandler = jasmine.createSpy('moveHandler') file.on 'moved', moveHandler @@ -67,6 +68,7 @@ describe 'File', -> expect(file.getPath()).toBe newPath it "maintains 'contents-changed' events set on previous path", -> + jasmine.unspy(window, "setTimeout") moveHandler = null moveHandler = jasmine.createSpy('moveHandler') file.on 'moved', moveHandler diff --git a/spec/app/text-buffer-spec.coffee b/spec/app/text-buffer-spec.coffee index 2683cc7a7..93e39af75 100644 --- a/spec/app/text-buffer-spec.coffee +++ b/spec/app/text-buffer-spec.coffee @@ -65,6 +65,8 @@ describe 'Buffer', -> expect(eventHandler).toHaveBeenCalledWith(bufferToChange) it "triggers a `path-changed` event when the file is moved", -> + jasmine.unspy(window, "setTimeout") + fsUtils.remove(newPath) if fsUtils.exists(newPath) fsUtils.move(path, newPath) diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index d4dcac2cb..4414240bb 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -13,6 +13,7 @@ File = require 'file' Editor = require 'editor' TokenizedBuffer = require 'tokenized-buffer' fsUtils = require 'fs-utils' +pathwatcher = require 'pathwatcher' RootView = require 'root-view' Git = require 'git' requireStylesheet "jasmine" @@ -95,8 +96,8 @@ afterEach -> waits(0) # yield to ui thread to make screen update more frequently ensureNoPathSubscriptions = -> - watchedPaths = $native.getWatchedPaths() - $native.unwatchAllPaths() + watchedPaths = pathwatcher.getWatchedPaths() + pathwatcher.closeAllWatchers() if watchedPaths.length > 0 throw new Error("Leaking subscriptions for paths: " + watchedPaths.join(", ")) diff --git a/src/app/directory.coffee b/src/app/directory.coffee index d73612fb0..aadc75a94 100644 --- a/src/app/directory.coffee +++ b/src/app/directory.coffee @@ -1,6 +1,7 @@ _ = require 'underscore' fs = require 'fs' fsUtils = require 'fs-utils' +pathWatcher = require 'pathwatcher' File = require 'file' EventEmitter = require 'event-emitter' @@ -39,12 +40,12 @@ class Directory @unsubscribeFromNativeChangeEvents() if @subscriptionCount() == 0 subscribeToNativeChangeEvents: -> - @watchSubscription = fsUtils.watchPath @path, (eventType) => - @trigger "contents-changed" if eventType is "contents-change" + @watchSubscription = pathWatcher.watch @path, (eventType) => + @trigger "contents-changed" if eventType is "change" unsubscribeFromNativeChangeEvents: -> if @watchSubscription? - @watchSubscription.unwatch() + @watchSubscription.close() @watchSubscription = null _.extend Directory.prototype, EventEmitter diff --git a/src/app/file.coffee b/src/app/file.coffee index f982223b5..ec06ef189 100644 --- a/src/app/file.coffee +++ b/src/app/file.coffee @@ -2,6 +2,7 @@ EventEmitter = require 'event-emitter' fs = require 'fs' fsUtils = require 'fs-utils' +pathWatcher = require 'pathwatcher' _ = require 'underscore' module.exports = @@ -45,12 +46,13 @@ class File @unsubscribeFromNativeChangeEvents() if @subscriptionCount() == 0 handleNativeChangeEvent: (eventType, path) -> - if eventType is "remove" + if eventType is "delete" + @unsubscribeFromNativeChangeEvents() @detectResurrectionAfterDelay() - else if eventType is "move" + else if eventType is "rename" @setPath(path) @trigger "moved" - else if eventType is "contents-change" + else if eventType is "change" oldContents = @read() newContents = @read(true) return if oldContents == newContents @@ -62,19 +64,18 @@ class File detectResurrection: -> if @exists() @subscribeToNativeChangeEvents() - @handleNativeChangeEvent("contents-change", @getPath()) + @handleNativeChangeEvent("change", @getPath()) else @cachedContents = null - @unsubscribeFromNativeChangeEvents() @trigger "removed" subscribeToNativeChangeEvents: -> - @watchSubscription = fsUtils.watchPath @path, (eventType, path) => + @watchSubscription = pathWatcher.watch @path, (eventType, path) => @handleNativeChangeEvent(eventType, path) unsubscribeFromNativeChangeEvents: -> if @watchSubscription - @watchSubscription.unwatch() + @watchSubscription.close() @watchSubscription = null _.extend File.prototype, EventEmitter diff --git a/src/stdlib/fs-utils.coffee b/src/stdlib/fs-utils.coffee index a422f1e10..8fc11d36a 100644 --- a/src/stdlib/fs-utils.coffee +++ b/src/stdlib/fs-utils.coffee @@ -312,11 +312,3 @@ module.exports = cson.readObjectAsync(path, done) else @readPlistAsync(path, done) - - watchPath: (path, callback) -> - path = @absolute(path) - watchCallback = (eventType, eventPath) => - path = @absolute(eventPath) if eventType is 'move' - callback(arguments...) - id = $native.watchPath(path, watchCallback) - unwatch: -> $native.unwatchPath(path, id)