Fall back to local storage when no history can be found

This commit is contained in:
Antonio Scandurra
2017-03-10 14:37:37 +01:00
parent cf9a5b13e3
commit 7e761d39af
3 changed files with 11 additions and 6 deletions

View File

@@ -30,7 +30,7 @@ describe("HistoryManager", () => {
return projectDisposable
})
historyManager = new HistoryManager({stateStore, project, commands: commandRegistry})
historyManager = new HistoryManager({stateStore, localStorage: window.localStorage, project, commands: commandRegistry})
await historyManager.loadState()
})
@@ -75,7 +75,7 @@ describe("HistoryManager", () => {
it("saves the state", async () => {
await historyManager.clearProjects()
const historyManager2 = new HistoryManager({stateStore, project, commands: commandRegistry})
const historyManager2 = new HistoryManager({stateStore, localStorage: window.localStorage, project, commands: commandRegistry})
await historyManager2.loadState()
expect(historyManager.getProjects().length).toBe(0)
})
@@ -186,7 +186,7 @@ describe("HistoryManager", () => {
it("saves the state", async () => {
await historyManager.addProject(["/save/state"])
await historyManager.saveState()
const historyManager2 = new HistoryManager({stateStore, project, commands: commandRegistry})
const historyManager2 = new HistoryManager({stateStore, localStorage: window.localStorage, project, commands: commandRegistry})
await historyManager2.loadState()
expect(historyManager2.getProjects()[0].paths).toEqual(['/save/state'])
})

View File

@@ -229,7 +229,7 @@ class AtomEnvironment extends Model
@observeAutoHideMenuBar()
@history = new HistoryManager({@project, @commands, @stateStore})
@history = new HistoryManager({@project, @commands, @stateStore, localStorage: window.localStorage})
# Keep instances of HistoryManager in sync
@disposables.add @history.onDidChangeProjects (e) =>
@applicationDelegate.didChangeHistoryManager() unless e.reloaded

View File

@@ -8,8 +8,9 @@ import {Emitter, CompositeDisposable} from 'event-kit'
//
// The project history is used to enable the 'Reopen Project' menu.
export class HistoryManager {
constructor ({stateStore, project, commands}) {
constructor ({stateStore, localStorage, project, commands}) {
this.stateStore = stateStore
this.localStorage = localStorage
this.emitter = new Emitter()
this.projects = []
this.disposables = new CompositeDisposable()
@@ -93,7 +94,11 @@ export class HistoryManager {
}
async loadState () {
const history = await this.stateStore.load('history-manager')
let history = await this.stateStore.load('history-manager')
if (!history) {
history = JSON.parse(this.localStorage.getItem('history'))
}
if (history && history.projects) {
this.projects = history.projects.filter(p => Array.isArray(p.paths) && p.paths.length > 0).map(p => new HistoryProject(p.paths, new Date(p.lastOpened)))
this.didChangeProjects({reloaded: true})