Use file contents instead of md5 to determine disk change

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-11-28 17:16:44 -08:00
parent 2f6566d1c5
commit e98b4f9a9b
3 changed files with 20 additions and 23 deletions

View File

@@ -3,7 +3,7 @@ Buffer = require 'buffer'
fs = require 'fs'
_ = require 'underscore'
describe 'Buffer', ->
fdescribe 'Buffer', ->
[filePath, fileContents, buffer] = []
beforeEach ->
@@ -95,7 +95,7 @@ describe 'Buffer', ->
runs ->
expect(changeHandler).not.toHaveBeenCalled()
describe "when the buffer's memory contents are the same as the *previous* disk contents", ->
describe "when the buffer is in an unmodified state before the on-disk change", ->
it "changes the memory contents of the buffer to match the new disk contents and triggers a 'change' event", ->
changeHandler = jasmine.createSpy('changeHandler')
buffer.on 'change', changeHandler

View File

@@ -76,10 +76,7 @@ class Buffer
@setText(@cachedDiskContents)
updateCachedDiskContents: ->
if fs.exists(@getPath())
@cachedDiskContents = fs.read(@getPath())
else
@cachedDiskContents = null
@cachedDiskContents = @file.read()
getBaseName: ->
@file?.getBaseName()
@@ -239,14 +236,9 @@ class Buffer
unless path then throw new Error("Can't save buffer with no file path")
@trigger 'before-save'
@file?.updateMd5()
@setPath(path)
text = @getText()
@cachedDiskContents = text
@file.write(text)
@cachedDiskContents = @getText()
@file.write(@getText())
@subscribeToFile()
@trigger 'after-save'

View File

@@ -6,13 +6,13 @@ _ = require 'underscore'
module.exports =
class File
path: null
md5: null
cachedContents: null
constructor: (@path) ->
if @exists() and not fs.isFile(@path)
throw new Error(@path + " is a directory")
@updateMd5() if @exists()
@read() if @exists()
setPath: (@path) ->
@@ -23,16 +23,21 @@ class File
write: (text) ->
previouslyExisted = @exists()
@cachedContents = text
fs.write(@getPath(), text)
@updateMd5()
@subscribeToNativeChangeEvents() if not previouslyExisted and @subscriptionCount() > 0
read: (flushCache)->
if not @exists()
@cachedContents = null
else if not @cachedContents? or flushCache
@cachedContents = fs.read(@getPath())
else
@cachedContents
exists: ->
fs.exists(@getPath())
updateMd5: ->
@md5 = fs.md5ForPath(@path)
afterSubscribe: ->
@subscribeToNativeChangeEvents() if @exists() and @subscriptionCount() == 1
@@ -41,15 +46,15 @@ class File
handleNativeChangeEvent: (eventType, path) ->
if eventType is "remove"
@cachedContents = null
@detectResurrectionAfterDelay()
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
oldContents = @read()
newContents = @read(true)
return if oldContents == newContents
@trigger 'contents-change'
detectResurrectionAfterDelay: ->