Ensure we never deserialize two instances of the same buffer

We might have two edit sessions pointing to the same buffer, for
example if we have a split pane… So when we deserialize a buffer, we
always need to check that we don't already have an instance of that
buffer on the project. If we do, then we've already deserialized it
once so we don't need to worry about the saved text.

We still have a problem when deserializing previously unsaved buffers,
because we can't use the path to identify them.
This commit is contained in:
Nathan Sobo 2013-04-02 15:05:26 -06:00
parent f531d36060
commit 3150785db2
3 changed files with 13 additions and 12 deletions

View File

@ -1265,3 +1265,9 @@ describe 'Buffer', ->
expect(serializedState.path).toBeUndefined()
expect(buffer.getPath()).toBeUndefined()
expect(buffer.getText()).toBe("abc")
it "never deserializes two separate instances of the same buffer", ->
serializedState = buffer.serialize()
buffer.release()
buffer = Buffer.deserialize(serializedState)
expect(Buffer.deserialize(serializedState)).toBe buffer

View File

@ -127,19 +127,17 @@ class Project
else
@on 'buffer-created', (buffer) -> callback(buffer)
bufferForPath: (filePath) ->
bufferForPath: (filePath, text) ->
if filePath?
filePath = @resolve(filePath)
if filePath
buffer = _.find @buffers, (buffer) -> buffer.getPath() == filePath
buffer or @buildBuffer(filePath)
else
buffer or @buildBuffer(filePath, text)
else
@buildBuffer()
@buildBuffer(null, text)
buildBuffer: (filePath) ->
buffer = new Buffer(filePath)
buildBuffer: (filePath, text) ->
buffer = new Buffer(filePath, text)
@buffers.push buffer
@trigger 'buffer-created', buffer
buffer

View File

@ -25,11 +25,8 @@ class Buffer
invalidMarkers: null
refcount: 0
@deserialize: (state) ->
if state && (state.path? || state.text?)
new Buffer(state.path, state.text)
else
new Buffer(null)
@deserialize: ({path, text}) ->
project.bufferForPath(path, text)
constructor: (path, initialText) ->
@id = @constructor.idCounter++