mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
Only load serialized content if the disk content's have not changed.
This commit is contained in:
@@ -949,24 +949,48 @@ describe 'TextBuffer', ->
|
||||
expect(buffer2.getText()).toBe(buffer.getText())
|
||||
|
||||
describe "when the serialized buffer had unsaved changes", ->
|
||||
it "restores the previous unsaved state of the buffer", ->
|
||||
previousText = buffer.getText()
|
||||
buffer.setText("abc")
|
||||
describe "when the disk contents were changed since serialization", ->
|
||||
it "loads the disk contents instead of the previous unsaved state", ->
|
||||
buffer.release()
|
||||
|
||||
state = buffer.serialize()
|
||||
expect(state.getObject('text')).toBe 'abc'
|
||||
filePath = temp.openSync('atom').path
|
||||
fs.writeSync(filePath, "words")
|
||||
buffer = project.openSync(filePath).buffer
|
||||
buffer.setText("BUFFER CHANGE")
|
||||
|
||||
buffer2 = deserialize(state, {project})
|
||||
state = buffer.serialize()
|
||||
expect(state.getObject('text')).toBe 'BUFFER CHANGE'
|
||||
fs.writeSync(filePath, "DISK CHANGE")
|
||||
|
||||
waitsFor ->
|
||||
buffer2.cachedDiskContents
|
||||
buffer2 = deserialize(state, {project})
|
||||
|
||||
runs ->
|
||||
expect(buffer2.getPath()).toBe(buffer.getPath())
|
||||
expect(buffer2.getText()).toBe(buffer.getText())
|
||||
expect(buffer2.isModified()).toBeTruthy()
|
||||
buffer2.setText(previousText)
|
||||
expect(buffer2.isModified()).toBeFalsy()
|
||||
waitsFor ->
|
||||
buffer2.cachedDiskContents
|
||||
|
||||
runs ->
|
||||
expect(buffer2.getPath()).toBe(buffer.getPath())
|
||||
expect(buffer2.getText()).toBe("DISK CHANGE")
|
||||
expect(buffer2.isModified()).toBeFalsy()
|
||||
|
||||
describe "when the disk contents are the same since serialization", ->
|
||||
it "restores the previous unsaved state of the buffer", ->
|
||||
previousText = buffer.getText()
|
||||
buffer.setText("abc")
|
||||
|
||||
state = buffer.serialize()
|
||||
expect(state.getObject('text')).toBe 'abc'
|
||||
|
||||
buffer2 = deserialize(state, {project})
|
||||
|
||||
waitsFor ->
|
||||
buffer2.cachedDiskContents
|
||||
|
||||
runs ->
|
||||
expect(buffer2.getPath()).toBe(buffer.getPath())
|
||||
expect(buffer2.getText()).toBe(buffer.getText())
|
||||
expect(buffer2.isModified()).toBeTruthy()
|
||||
buffer2.setText(previousText)
|
||||
expect(buffer2.isModified()).toBeFalsy()
|
||||
|
||||
describe "when the serialized buffer was unsaved and had no path", ->
|
||||
it "restores the previous unsaved state of the buffer", ->
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
crypto = require 'crypto'
|
||||
path = require 'path'
|
||||
pathWatcher = require 'pathwatcher'
|
||||
Q = require 'q'
|
||||
@@ -68,6 +69,9 @@ class File
|
||||
else
|
||||
@cachedContents
|
||||
|
||||
@setDigest(@cachedContents)
|
||||
@cachedContents
|
||||
|
||||
# Public: Reads the contents of the file.
|
||||
#
|
||||
# * flushCache:
|
||||
@@ -101,12 +105,19 @@ class File
|
||||
promise = Q(@cachedContents)
|
||||
|
||||
promise.then (contents) =>
|
||||
@setDigest(contents)
|
||||
@cachedContents = contents
|
||||
|
||||
# Public: Returns whether the file exists.
|
||||
exists: ->
|
||||
fsUtils.exists(@getPath())
|
||||
|
||||
setDigest: (contents)->
|
||||
@digest = crypto.createHash('sha1').update(contents ? '').digest('hex')
|
||||
|
||||
getDigest: ->
|
||||
@digest ? @setDigest(@readSync())
|
||||
|
||||
# Private:
|
||||
handleNativeChangeEvent: (eventType, path) ->
|
||||
if eventType is "delete"
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
{Emitter, Subscriber} = require 'emissary'
|
||||
crypto = require 'crypto'
|
||||
guid = require 'guid'
|
||||
Q = require 'q'
|
||||
{P} = require 'scandal'
|
||||
@@ -57,7 +58,6 @@ class TextBuffer
|
||||
deserializer: @constructor.name
|
||||
version: @constructor.version
|
||||
text: @text
|
||||
@loadFromDisk = true
|
||||
|
||||
@loaded = false
|
||||
@subscribe @text, 'changed', @handleTextChange
|
||||
@@ -68,12 +68,12 @@ class TextBuffer
|
||||
|
||||
loadSync: ->
|
||||
@updateCachedDiskContentsSync()
|
||||
@reload() if @loadFromDisk
|
||||
@reload() if @loadFromDisk or @state.get('diskContentsDigest') != @file?.getDigest()
|
||||
@text.clearUndoStack()
|
||||
|
||||
load: ->
|
||||
@updateCachedDiskContents().then =>
|
||||
@reload() if @loadFromDisk
|
||||
@reload() if @loadFromDisk or @state.get('diskContentsDigest') != @file?.getDigest()
|
||||
@text.clearUndoStack()
|
||||
this
|
||||
|
||||
@@ -108,6 +108,7 @@ class TextBuffer
|
||||
serialize: ->
|
||||
state = @state.clone()
|
||||
state.set('isModified', @isModified())
|
||||
state.set('diskContentsDigest', @file.getDigest()) if @file
|
||||
for marker in state.get('text').getMarkers() when marker.isRemote()
|
||||
marker.destroy()
|
||||
state
|
||||
|
||||
Reference in New Issue
Block a user