mirror of
https://github.com/atom/atom.git
synced 2026-02-16 01:25:13 -05:00
Use node-pathwatcher.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user