Fire 'modified-status-changed' events on changes after save/reload

Buffer keeps state about the value with which it fired the last
modified-status-changed event so that it doesn't fire it twice with the
same boolean value. Every piece of code that triggers the event also
needs to set this state, so now everything goes through the
`triggerModifiedStatusChanged` method.
This commit is contained in:
Nathan Sobo 2013-02-28 17:49:11 -07:00 committed by probablycorey
parent 699e780e99
commit 59a06acc0b
2 changed files with 28 additions and 10 deletions

View File

@ -181,7 +181,7 @@ describe 'Buffer', ->
changeHandler.callCount > 0
describe "modified status", ->
it "reports a modified status of true after the user changes buffer", ->
it "reports the modified status changing to true or false after the user changes buffer", ->
modifiedHandler = jasmine.createSpy("modifiedHandler")
buffer.on 'modified-status-changed', modifiedHandler
@ -203,7 +203,7 @@ describe 'Buffer', ->
advanceClock(buffer.stoppedChangingDelay)
expect(modifiedHandler).toHaveBeenCalledWith(false)
it "reports a modified status of true after the underlying file is deleted", ->
it "reports the modified status changing to true after the underlying file is deleted", ->
buffer.release()
filePath = "/tmp/atom-tmp-file"
fs.write(filePath, 'delete me')
@ -216,7 +216,7 @@ describe 'Buffer', ->
waitsFor "modified status to change", -> modifiedHandler.callCount
runs -> expect(buffer.isModified()).toBe true
it "reports a modified status of false after a modified buffer is saved", ->
it "reports the modified status changing to false after a modified buffer is saved", ->
filePath = "/tmp/atom-tmp-file"
fs.write(filePath, '')
buffer.release()
@ -225,14 +225,22 @@ describe 'Buffer', ->
buffer.on 'modified-status-changed', modifiedHandler
buffer.insert([0,0], "hi")
advanceClock(buffer.stoppedChangingDelay)
expect(buffer.isModified()).toBe true
modifiedHandler.reset()
buffer.save()
expect(modifiedHandler).toHaveBeenCalledWith(false)
expect(buffer.isModified()).toBe false
modifiedHandler.reset()
it "reports a modified status of false after a modified buffer is reloaded", ->
buffer.insert([0, 0], 'x')
advanceClock(buffer.stoppedChangingDelay)
expect(modifiedHandler).toHaveBeenCalledWith(true)
expect(buffer.isModified()).toBe true
it "reports the modified status changing to false after a modified buffer is reloaded", ->
filePath = "/tmp/atom-tmp-file"
fs.write(filePath, '')
buffer.release()
@ -241,12 +249,19 @@ describe 'Buffer', ->
buffer.on 'modified-status-changed', modifiedHandler
buffer.insert([0,0], "hi")
advanceClock(buffer.stoppedChangingDelay)
expect(buffer.isModified()).toBe true
modifiedHandler.reset()
buffer.reload()
expect(modifiedHandler).toHaveBeenCalledWith(false)
expect(buffer.isModified()).toBe false
modifiedHandler.reset()
buffer.insert([0, 0], 'x')
advanceClock(buffer.stoppedChangingDelay)
expect(modifiedHandler).toHaveBeenCalledWith(true)
expect(buffer.isModified()).toBe true
it "returns false for an empty buffer with no path", ->
buffer.release()

View File

@ -69,7 +69,7 @@ class Buffer
@file.on "removed", =>
@updateCachedDiskContents()
@trigger "modified-status-changed", @isModified()
@triggerModifiedStatusChanged(@isModified())
@file.on "moved", =>
@trigger "path-changed", this
@ -78,7 +78,7 @@ class Buffer
@trigger 'will-reload'
@updateCachedDiskContents()
@setText(@cachedDiskContents)
@trigger 'modified-status-changed', false
@triggerModifiedStatusChanged(false)
@trigger 'reloaded'
updateCachedDiskContents: ->
@ -253,7 +253,7 @@ class Buffer
@setPath(path)
@cachedDiskContents = @getText()
@file.write(@getText())
@trigger 'modified-status-changed', false
@triggerModifiedStatusChanged(false)
@trigger 'saved'
isModified: ->
@ -432,11 +432,14 @@ class Buffer
@stoppedChangingTimeout = null
modifiedStatus = @isModified()
@trigger 'contents-modified', modifiedStatus
unless modifiedStatus is @previousModifiedStatus
@previousModifiedStatus = modifiedStatus
@trigger 'modified-status-changed', modifiedStatus
@triggerModifiedStatusChanged(modifiedStatus)
@stoppedChangingTimeout = setTimeout(stoppedChangingCallback, @stoppedChangingDelay)
triggerModifiedStatusChanged: (modifiedStatus) ->
return if modifiedStatus is @previousModifiedStatus
@previousModifiedStatus = modifiedStatus
@trigger 'modified-status-changed', modifiedStatus
fileExists: ->
@file.exists()