mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
Replicate insertion and removal of project buffers
Buffers are now destroyed when removed from the project if they haven't been destroyed already.
This commit is contained in:
committed by
Kevin Sawicki
parent
30273c6a66
commit
85cc81851f
@@ -8,6 +8,7 @@ describe "Project replication", ->
|
||||
beforeEach ->
|
||||
project1 = new Project(fsUtils.resolveOnLoadPath('fixtures/dir'))
|
||||
project1.bufferForPath('a')
|
||||
project1.bufferForPath('a')
|
||||
expect(project1.getBuffers().length).toBe 1
|
||||
|
||||
doc1 = project1.serialize()
|
||||
@@ -24,3 +25,13 @@ describe "Project replication", ->
|
||||
expect(project2.getPath()).toBe project1.getPath()
|
||||
expect(project2.getBuffers().length).toBe 1
|
||||
expect(project2.getBuffers()[0].getPath()).toBe project1.getBuffers()[0].getPath()
|
||||
|
||||
it "replicates insertion and removal of open buffers", ->
|
||||
bufferB = project2.bufferForPath('b')
|
||||
expect(project1.getBuffers().length).toBe 2
|
||||
expect(project2.getBuffers()[0].getPath()).toBe project1.getBuffers()[0].getPath()
|
||||
expect(project2.getBuffers()[1].getPath()).toBe project1.getBuffers()[1].getPath()
|
||||
|
||||
project1.removeBuffer(project1.bufferForPath('b'))
|
||||
expect(project1.getBuffers().length).toBe 1
|
||||
expect(project2.getBuffers()[0].getPath()).toBe project1.getBuffers()[0].getPath()
|
||||
|
||||
@@ -57,11 +57,20 @@ class Project
|
||||
@state = pathOrState
|
||||
@setPath(pathOrState.get('path'))
|
||||
@state.get('buffers').each (bufferState) =>
|
||||
@addBuffer(deserialize(bufferState))
|
||||
if buffer = deserialize(bufferState)
|
||||
@addBuffer(buffer, updateState: false)
|
||||
else
|
||||
@state = telepath.Document.create(deserializer: @constructor.name, version: @constructor.version, buffers: [])
|
||||
@setPath(pathOrState)
|
||||
|
||||
@state.get('buffers').observe ({inserted, removed, index, site}) =>
|
||||
return if site is @state.site.id
|
||||
|
||||
for removedBuffer in removed
|
||||
@removeBufferAtIndex(index, updateState: false)
|
||||
for insertedBuffer, i in inserted
|
||||
@addBufferAtIndex(deserialize(insertedBuffer), index + i, updateState: false)
|
||||
|
||||
serialize: ->
|
||||
@state
|
||||
|
||||
@@ -236,8 +245,11 @@ class Project
|
||||
buffer
|
||||
|
||||
addBuffer: (buffer, options={}) ->
|
||||
@buffers.push(buffer)
|
||||
@state.get('buffers').push(buffer.getState()) if options.updateState ? true
|
||||
@addBufferAtIndex(buffer, @buffers.length, options)
|
||||
|
||||
addBufferAtIndex: (buffer, index, options={}) ->
|
||||
@buffers[index] = buffer
|
||||
@state.get('buffers').insert(index, buffer.getState()) if options.updateState ? true
|
||||
|
||||
# Removes a {Buffer} association from the project.
|
||||
#
|
||||
@@ -246,8 +258,9 @@ class Project
|
||||
@removeBufferAtIndex(@buffers.indexOf(buffer))
|
||||
|
||||
removeBufferAtIndex: (index, options={}) ->
|
||||
@buffers.splice(index, 1)
|
||||
[buffer] = @buffers.splice(index, 1)
|
||||
@state.get('buffers').remove(index) if options.updateState ? true
|
||||
buffer?.destroy()
|
||||
|
||||
# Performs a search across all the files in the project.
|
||||
#
|
||||
|
||||
@@ -50,7 +50,7 @@ class TextBuffer
|
||||
else
|
||||
[path, initialText] = args
|
||||
@text = telepath.Document.create(initialText, shareStrings: true) if initialText
|
||||
@state = telepath.Document.create(deserializer: @constructor.name)
|
||||
@state = telepath.Document.create(deserializer: @constructor.name, version: @constructor.version)
|
||||
|
||||
if path
|
||||
@setPath(path)
|
||||
@@ -77,10 +77,10 @@ class TextBuffer
|
||||
@scheduleModifiedEvents()
|
||||
|
||||
destroy: ->
|
||||
throw new Error("Destroying buffer twice with path '#{@getPath()}'") if @destroyed
|
||||
@file?.off()
|
||||
@destroyed = true
|
||||
project?.removeBuffer(this)
|
||||
unless @destroyed
|
||||
@file?.off()
|
||||
@destroyed = true
|
||||
project?.removeBuffer(this)
|
||||
|
||||
retain: ->
|
||||
@refcount++
|
||||
|
||||
Reference in New Issue
Block a user