Handle file resurrection :cross:

This commit is contained in:
Corey Johnson
2012-11-16 16:13:34 -08:00
parent 726345d93d
commit 5841eba1bc
2 changed files with 48 additions and 11 deletions

View File

@@ -85,3 +85,25 @@ describe 'File', ->
waitsFor "change event", ->
changeHandler.callCount > 0
describe "when a file is deleted and the recreated within a small amount of time (git sometimes does this)", ->
it "triggers a contents change event if the contents change", ->
jasmine.unspy(window, "setTimeout")
changeHandler = jasmine.createSpy("file changed")
removeHandler = jasmine.createSpy("file removed")
file.on 'contents-change', changeHandler
file.on 'remove', removeHandler
fs.remove(path)
fs.write(path, "HE HAS RISEN!")
waitsFor "change event", ->
changeHandler.callCount > 0
runs ->
expect(removeHandler).not.toHaveBeenCalled()
fs.write(path, "Hallelujah!")
changeHandler.reset()
waitsFor "change event", ->
changeHandler.callCount > 0

View File

@@ -19,6 +19,9 @@ class File
getBaseName: ->
fs.base(@path)
exists: ->
fs.exists(@getPath())
updateMd5: ->
@md5 = fs.md5ForPath(@path)
@@ -30,18 +33,30 @@ class File
subscribeToNativeChangeEvents: ->
@watchId = $native.watchPath @path, (eventType, path) =>
if eventType is "remove"
@trigger "remove"
@off()
else if eventType is "move"
@setPath(path)
@trigger "move"
else if eventType is "contents-change"
newMd5 = fs.md5ForPath(@getPath())
return if newMd5 == @md5
@handleNativeChangeEvent(eventType, path)
@md5 = newMd5
@trigger 'contents-change'
handleNativeChangeEvent: (eventType, path) ->
console.log eventType
if eventType is "remove"
@unsubscribeFromNativeChangeEvents()
detectResurrection = =>
if @exists()
@subscribeToNativeChangeEvents()
@handleNativeChangeEvent("contents-change", path)
else
@trigger "remove"
@off()
_.delay detectResurrection, 50
else if eventType is "move"
@setPath(path)
@trigger "move"
else if eventType is "contents-change"
newMd5 = fs.md5ForPath(@getPath())
return if newMd5 == @md5
@md5 = newMd5
@trigger 'contents-change'
unsubscribeFromNativeChangeEvents: ->
$native.unwatchPath(@path, @watchId)