mirror of
https://github.com/atom/atom.git
synced 2026-01-26 15:28:27 -05:00
Save state asynchronously on mousedown and keydown events. Tests WIP
This commit is contained in:
@@ -163,7 +163,7 @@ describe "AtomEnvironment", ->
|
||||
|
||||
atom.state.stuff = "cool"
|
||||
atom.project.setPaths([dir1, dir2])
|
||||
atom.saveStateSync()
|
||||
atom.saveState(true)
|
||||
|
||||
atom.state = {}
|
||||
atom.loadStateSync()
|
||||
@@ -174,18 +174,18 @@ describe "AtomEnvironment", ->
|
||||
atom.loadStateSync()
|
||||
expect(atom.state.stuff).toBe("cool")
|
||||
|
||||
it "saves state on keypress and mousedown events", ->
|
||||
spyOn(atom, 'saveStateSync')
|
||||
it "saves state on keydown and mousedown events", ->
|
||||
spyOn(atom, 'saveState')
|
||||
|
||||
keypress = new KeyboardEvent('keypress')
|
||||
atom.document.dispatchEvent(keypress)
|
||||
keydown = new KeyboardEvent('keydown')
|
||||
atom.document.dispatchEvent(keydown)
|
||||
advanceClock atom.saveStateDebounceInterval
|
||||
expect(atom.saveStateSync).toHaveBeenCalled()
|
||||
expect(atom.saveState).toHaveBeenCalled()
|
||||
|
||||
mousedown = new MouseEvent('mousedown')
|
||||
atom.document.dispatchEvent(mousedown)
|
||||
advanceClock atom.saveStateDebounceInterval
|
||||
expect(atom.saveStateSync).toHaveBeenCalled()
|
||||
expect(atom.saveState).toHaveBeenCalled()
|
||||
|
||||
describe "openInitialEmptyEditorIfNecessary", ->
|
||||
describe "when there are no paths set", ->
|
||||
@@ -243,9 +243,10 @@ describe "AtomEnvironment", ->
|
||||
|
||||
atomEnvironment.destroy()
|
||||
|
||||
# TODO: fix failing test. devtools show that ::saveState just goes to jasmine.createSpy.spyObj function
|
||||
it "saves the serialized state of the window so it can be deserialized after reload", ->
|
||||
atomEnvironment = new AtomEnvironment({applicationDelegate: atom.applicationDelegate, window, document})
|
||||
spyOn(atomEnvironment, 'saveStateSync')
|
||||
spyOn(atomEnvironment, 'saveState')
|
||||
|
||||
workspaceState = atomEnvironment.workspace.serialize()
|
||||
grammarsState = {grammarOverridesByPath: atomEnvironment.grammars.grammarOverridesByPath}
|
||||
@@ -256,7 +257,7 @@ describe "AtomEnvironment", ->
|
||||
expect(atomEnvironment.state.workspace).toEqual workspaceState
|
||||
expect(atomEnvironment.state.grammars).toEqual grammarsState
|
||||
expect(atomEnvironment.state.project).toEqual projectState
|
||||
expect(atomEnvironment.saveStateSync).toHaveBeenCalled()
|
||||
expect(atomEnvironment.saveState).toHaveBeenCalled()
|
||||
|
||||
atomEnvironment.destroy()
|
||||
|
||||
|
||||
@@ -121,8 +121,8 @@ class AtomEnvironment extends Model
|
||||
constructor: (params={}) ->
|
||||
{@blobStore, @applicationDelegate, @window, @document, configDirPath, @enablePersistence, onlyLoadBaseStyleSheets} = params
|
||||
|
||||
debouncedSaveStateSync = _.debounce((=> @saveStateSync()), @saveStateDebounceInterval)
|
||||
@document.addEventListener('mousedown', debouncedSaveStateSync, true)
|
||||
debouncedSaveState = _.debounce((=> @saveState()), @saveStateDebounceInterval)
|
||||
@document.addEventListener('mousedown', debouncedSaveState, true)
|
||||
@document.addEventListener('keydown', debouncedSaveState, true)
|
||||
|
||||
@state = {version: @constructor.version}
|
||||
@@ -653,9 +653,8 @@ class AtomEnvironment extends Model
|
||||
return if not @project
|
||||
|
||||
@storeWindowBackground()
|
||||
@serialize()
|
||||
@saveState(true)
|
||||
@packages.deactivatePackages()
|
||||
@saveStateSync()
|
||||
@saveBlobStoreSync()
|
||||
|
||||
openInitialEmptyEditorIfNecessary: ->
|
||||
@@ -789,12 +788,15 @@ class AtomEnvironment extends Model
|
||||
|
||||
@blobStore.save()
|
||||
|
||||
saveStateSync: ->
|
||||
saveState: (synchronous) ->
|
||||
return unless @enablePersistence
|
||||
@serialize()
|
||||
|
||||
if storageKey = @getStateKey(@project?.getPaths())
|
||||
@getStorageFolder().storeSync(storageKey, @state)
|
||||
if synchronous
|
||||
@getStorageFolder().storeSync(storageKey, @state)
|
||||
else
|
||||
@getStorageFolder().storeAsync(storageKey, @state)
|
||||
else
|
||||
@getCurrentWindow().loadSettings.windowState = JSON.stringify(@state)
|
||||
|
||||
|
||||
@@ -11,6 +11,11 @@ class StorageFolder
|
||||
|
||||
fs.writeFileSync(@pathForKey(name), JSON.stringify(object), 'utf8')
|
||||
|
||||
storeAsync: (name, object) ->
|
||||
return unless @path?
|
||||
|
||||
fs.writeFile(@pathForKey(name), JSON.stringify(object), 'utf8')
|
||||
|
||||
load: (name) ->
|
||||
return unless @path?
|
||||
|
||||
|
||||
Reference in New Issue
Block a user