Destroy unretained buffers when serializing project

Previously buffers could linger indefinitely if an error occurred
during startup between the deserialization of the project and the
original retaining edit session.
This commit is contained in:
Kevin Sawicki & Nathan Sobo
2013-08-28 18:37:40 -07:00
parent 40b6183937
commit 2bd4386090
3 changed files with 15 additions and 1 deletions

View File

@@ -8,6 +8,13 @@ describe "Project", ->
beforeEach ->
project.setPath(project.resolve('dir'))
describe "serialization", ->
it "destroys unretained buffers and does not include them in the serialized state", ->
project.bufferForPath('a')
expect(project.getBuffers().length).toBe 1
expect(deserialize(project.serialize()).getBuffers().length).toBe 0
expect(project.getBuffers().length).toBe 0
describe "when an edit session is destroyed", ->
it "removes edit session and calls destroy on buffer (if buffer is not referenced by other edit sessions)", ->
editSession = project.open("a")

View File

@@ -91,9 +91,14 @@ class Project
serialize: ->
state = @state.clone()
state.set('path', @getPath())
@destroyUnretainedBuffers()
state.set('buffers', buffer.serialize() for buffer in @getBuffers())
state
# Private:
destroyUnretainedBuffers: ->
buffer.destroy() for buffer in @getBuffers() when not buffer.isRetained()
# Public: ?
getState: -> @state

View File

@@ -81,13 +81,15 @@ class TextBuffer
@destroyed = true
@project?.removeBuffer(this)
isRetained: -> @refcount > 0
retain: ->
@refcount++
this
release: ->
@refcount--
@destroy() if @refcount <= 0
@destroy() unless @isRetained()
this
serialize: ->