When Buffer's path changes, stop listening for events on former path

This commit is contained in:
Corey Johnson
2012-06-28 13:04:36 -07:00
parent 8e7a8836cd
commit 3dd9253bc5
2 changed files with 21 additions and 0 deletions

View File

@@ -478,3 +478,23 @@ describe 'Buffer', ->
expect(buffer.positionForCharacterIndex(30)).toEqual [1, 0]
expect(buffer.positionForCharacterIndex(61)).toEqual [2, 0]
expect(buffer.positionForCharacterIndex(408)).toEqual [12, 2]
describe ".setPath(path)", ->
[path, newPath] = []
beforeEach ->
path = fs.join(require.resolve('fixtures'), "tmp.txt")
fs.write(path, "first")
afterEach ->
fs.remove(path)
it "stops listening to events on previous path and begins listening to events on new path", ->
buffer = new Buffer(path)
changeHandler = jasmine.createSpy('changeHandler')
buffer.on 'change', changeHandler
buffer.setPath(filePath)
expect(changeHandler).not.toHaveBeenCalled()
fs.write(path, "should not trigger buffer event")
waits 20
runs -> expect(changeHandler).not.toHaveBeenCalled()

View File

@@ -35,6 +35,7 @@ class Buffer
null
setPath: (path) ->
@file?.off()
@file = new File(path)
@file.on "contents-change", =>
@setText(fs.read(@file.getPath())) unless @isModified()