Skip to content

Commit

Permalink
Save app state before quit
Browse files Browse the repository at this point in the history
  • Loading branch information
inukshuk committed Jan 11, 2016
1 parent 1cf9cc5 commit 842a7d9
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/browser/tropy.js
Expand Up @@ -5,11 +5,12 @@ const { resolve } = require('path')
const { app, shell, BrowserWindow, ipcMain: ipc } = require('electron')
const { verbose } = require('../common/log')
const AppMenu = require('./menu')
const Storage = require('./storage')
const url = require('url')

const pkg = require('../../package')

const prop = Object.defineProperty
const { defineProperty: prop } = Object

module.exports = class Tropy extends EventEmitter {

Expand All @@ -22,6 +23,8 @@ module.exports = class Tropy extends EventEmitter {
this.menu = new AppMenu(this)
this.menu.load('app')

prop(this, 'store', { value: new Storage() })

prop(this, 'debug', { value: debug })
prop(this, 'environment', { value: environment || process.env.NODE_ENV })

Expand All @@ -33,6 +36,7 @@ module.exports = class Tropy extends EventEmitter {
value: encode({ environment, debug, home: app.getPath('userData') })
})

this.resume()
this.listen()
}

Expand Down Expand Up @@ -64,6 +68,21 @@ module.exports = class Tropy extends EventEmitter {
})
}

resume() {
return this.store
.load('state.json')
.catch({ code: 'ENOENT' }, () => ({}))

.then(state => {
this.state = state
this.emit('app:resume')
})
}

persist() {
return this.store.save.sync('state.json', this.state), this
}

listen() {
this
.on('app:quit', () => app.quit())
Expand Down Expand Up @@ -94,7 +113,8 @@ module.exports = class Tropy extends EventEmitter {

app
.on('before-quit', () => {
verbose('saving state...')
verbose('saving app state')
this.persist()
})

ipc
Expand Down

0 comments on commit 842a7d9

Please sign in to comment.