Use node-pathwatcher.

This commit is contained in:
Cheng Zhao
2013-04-07 16:18:08 +08:00
parent 20580a5da8
commit e0865e8c38
7 changed files with 20 additions and 20 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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)