diff --git a/spec/atom-environment-spec.coffee b/spec/atom-environment-spec.coffee index 3283b63d6..5fd4b11f1 100644 --- a/spec/atom-environment-spec.coffee +++ b/spec/atom-environment-spec.coffee @@ -4,6 +4,7 @@ temp = require 'temp' Package = require '../src/package' ThemeManager = require '../src/theme-manager' AtomEnvironment = require '../src/atom-environment' +StorageFolder = require '../src/storage-folder' describe "AtomEnvironment", -> describe 'window sizing methods', -> @@ -172,13 +173,39 @@ describe "AtomEnvironment", -> waitsForPromise -> atom.saveState().then -> atom.loadState().then (state) -> - expect(state).toBeNull() + expect(state).toBeFalsy() waitsForPromise -> loadSettings.initialPaths = [dir2, dir1] atom.loadState().then (state) -> expect(state).toEqual({stuff: 'cool'}) + it "loads state from the storage folder when it can't be found in atom.stateStore", -> + jasmine.useRealClock() + + storageFolderState = {foo: 1, bar: 2} + serializedState = {someState: 42} + loadSettings = _.extend(atom.getLoadSettings(), {initialPaths: [temp.mkdirSync("project-directory")]}) + spyOn(atom, 'getLoadSettings').andReturn(loadSettings) + spyOn(atom, 'serialize').andReturn(serializedState) + spyOn(atom, 'getStorageFolder').andReturn(new StorageFolder(temp.mkdirSync("config-directory"))) + atom.project.setPaths(atom.getLoadSettings().initialPaths) + + waitsForPromise -> + atom.stateStore.connect() + + runs -> + atom.getStorageFolder().storeSync(atom.getStateKey(loadSettings.initialPaths), storageFolderState) + + waitsForPromise -> + atom.loadState().then (state) -> expect(state).toEqual(storageFolderState) + + waitsForPromise -> + atom.saveState() + + waitsForPromise -> + atom.loadState().then (state) -> expect(state).toEqual(serializedState) + it "saves state on keydown, mousedown, and when the editor window unloads", -> spyOn(atom, 'saveState') diff --git a/src/atom-environment.coffee b/src/atom-environment.coffee index 8c79d66f6..6ef46dc2a 100644 --- a/src/atom-environment.coffee +++ b/src/atom-environment.coffee @@ -11,6 +11,7 @@ Model = require './model' WindowEventHandler = require './window-event-handler' StylesElement = require './styles-element' StateStore = require './state-store' +StorageFolder = require './storage-folder' {getWindowLoadSettings, setWindowLoadSettings} = require './window-load-settings-helpers' registerDefaultCommands = require './register-default-commands' @@ -127,7 +128,7 @@ class AtomEnvironment extends Model # Call .loadOrCreate instead constructor: (params={}) -> - {@blobStore, @applicationDelegate, @window, @document, configDirPath, @enablePersistence, onlyLoadBaseStyleSheets} = params + {@blobStore, @applicationDelegate, @window, @document, @configDirPath, @enablePersistence, onlyLoadBaseStyleSheets} = params @unloaded = false @loadTime = null @@ -138,7 +139,9 @@ class AtomEnvironment extends Model @stateStore = new StateStore('AtomEnvironments', 1) - @stateStore.clear() if clearWindowState + if clearWindowState + @getStorageFolder().clear() + @stateStore.clear() @deserializers = new DeserializerManager(this) @deserializeTimings = {} @@ -147,10 +150,10 @@ class AtomEnvironment extends Model @notifications = new NotificationManager - @config = new Config({configDirPath, resourcePath, notificationManager: @notifications, @enablePersistence}) + @config = new Config({@configDirPath, resourcePath, notificationManager: @notifications, @enablePersistence}) @setConfigSchema() - @keymaps = new KeymapManager({configDirPath, resourcePath, notificationManager: @notifications}) + @keymaps = new KeymapManager({@configDirPath, resourcePath, notificationManager: @notifications}) @tooltips = new TooltipManager(keymapManager: @keymaps) @@ -159,16 +162,16 @@ class AtomEnvironment extends Model @grammars = new GrammarRegistry({@config}) - @styles = new StyleManager({configDirPath}) + @styles = new StyleManager({@configDirPath}) @packages = new PackageManager({ - devMode, configDirPath, resourcePath, safeMode, @config, styleManager: @styles, + devMode, @configDirPath, resourcePath, safeMode, @config, styleManager: @styles, commandRegistry: @commands, keymapManager: @keymaps, notificationManager: @notifications, grammarRegistry: @grammars, deserializerManager: @deserializers, viewRegistry: @views }) @themes = new ThemeManager({ - packageManager: @packages, configDirPath, resourcePath, safeMode, @config, + packageManager: @packages, @configDirPath, resourcePath, safeMode, @config, styleManager: @styles, notificationManager: @notifications, viewRegistry: @views }) @@ -853,7 +856,12 @@ class AtomEnvironment extends Model loadState: -> if @enablePersistence if stateKey = @getStateKey(@getLoadSettings().initialPaths) - @stateStore.load(stateKey) + @stateStore.load(stateKey).then (state) => + if state + state + else + # TODO: remove this when every user has migrated to the IndexedDb state store. + @getStorageFolder().load(stateKey) else @applicationDelegate.getTemporaryWindowState() else @@ -882,6 +890,9 @@ class AtomEnvironment extends Model else null + getStorageFolder: -> + @storageFolder ?= new StorageFolder(@getConfigDirPath()) + getConfigDirPath: -> @configDirPath ?= process.env.ATOM_HOME diff --git a/src/storage-folder.coffee b/src/storage-folder.coffee index 06beae56a..280eb8b5c 100644 --- a/src/storage-folder.coffee +++ b/src/storage-folder.coffee @@ -6,6 +6,14 @@ class StorageFolder constructor: (containingPath) -> @path = path.join(containingPath, "storage") if containingPath? + clear: -> + return unless @path? + + try + fs.removeSync(@path) + catch error + console.warn "Error deleting #{@path}", error.stack, error + storeSync: (name, object) -> return unless @path?