mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2024-09-21 07:58:04 +03:00
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:
parent
f531d36060
commit
3150785db2
@ -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
|
||||
|
@ -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
|
||||
|
@ -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++
|
||||
|
Loading…
Reference in New Issue
Block a user