Refactor main.js: fix state save on exit
This commit is contained in:
@@ -90,7 +90,10 @@ function init () {
|
|||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
windows.main.dispatch('saveState') // try to save state on exit
|
windows.main.dispatch('saveState') // try to save state on exit
|
||||||
ipcMain.once('savedState', () => app.quit())
|
ipcMain.once('savedState', () => app.quit())
|
||||||
setTimeout(() => app.quit(), 2000) // quit after 2 secs, at most
|
setTimeout(() => {
|
||||||
|
console.error('Saving state took too long. Quitting.')
|
||||||
|
app.quit()
|
||||||
|
}, 2000) // quit after 2 secs, at most
|
||||||
})
|
})
|
||||||
|
|
||||||
app.on('activate', function () {
|
app.on('activate', function () {
|
||||||
|
|||||||
@@ -1,15 +1,16 @@
|
|||||||
module.exports = {
|
var appConfig = require('application-config')('WebTorrent')
|
||||||
|
var path = require('path')
|
||||||
|
var {EventEmitter} = require('events')
|
||||||
|
|
||||||
|
var config = require('../../config')
|
||||||
|
var migrations = require('./migrations')
|
||||||
|
|
||||||
|
var State = module.exports = Object.assign(new EventEmitter(), {
|
||||||
getDefaultPlayState,
|
getDefaultPlayState,
|
||||||
load,
|
load,
|
||||||
save,
|
save,
|
||||||
saveThrottled
|
saveThrottled
|
||||||
}
|
})
|
||||||
|
|
||||||
var appConfig = require('application-config')('WebTorrent')
|
|
||||||
var path = require('path')
|
|
||||||
|
|
||||||
var config = require('../../config')
|
|
||||||
var migrations = require('./migrations')
|
|
||||||
|
|
||||||
appConfig.filePath = path.join(config.CONFIG_PATH, 'config.json')
|
appConfig.filePath = path.join(config.CONFIG_PATH, 'config.json')
|
||||||
|
|
||||||
@@ -183,8 +184,6 @@ function save (state, cb) {
|
|||||||
console.log('Saving state to ' + appConfig.filePath)
|
console.log('Saving state to ' + appConfig.filePath)
|
||||||
delete state.saveStateTimeout
|
delete state.saveStateTimeout
|
||||||
|
|
||||||
var electron = require('electron')
|
|
||||||
|
|
||||||
// Clean up, so that we're not saving any pending state
|
// Clean up, so that we're not saving any pending state
|
||||||
var copy = Object.assign({}, state.saved)
|
var copy = Object.assign({}, state.saved)
|
||||||
// Remove torrents pending addition to the list, where we haven't finished
|
// Remove torrents pending addition to the list, where we haven't finished
|
||||||
@@ -205,11 +204,9 @@ function save (state, cb) {
|
|||||||
return torrent
|
return torrent
|
||||||
})
|
})
|
||||||
|
|
||||||
appConfig.write(copy, function (err) {
|
appConfig.write(copy, (err) => {
|
||||||
if (err) console.error(err)
|
if (err) console.error(err)
|
||||||
|
else State.emit('savedState')
|
||||||
// TODO: this doesn't belong here
|
|
||||||
electron.ipcRenderer.send('savedState')
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -180,7 +180,6 @@ const dispatchHandlers = {
|
|||||||
'deleteTorrent': (infoHash) => controllers.torrentList.deleteTorrent(infoHash),
|
'deleteTorrent': (infoHash) => controllers.torrentList.deleteTorrent(infoHash),
|
||||||
'toggleSelectTorrent': (infoHash) => controllers.torrentList.toggleSelectTorrent(infoHash),
|
'toggleSelectTorrent': (infoHash) => controllers.torrentList.toggleSelectTorrent(infoHash),
|
||||||
'openTorrentContextMenu': (infoHash) => controllers.torrentList.openTorrentContextMenu(infoHash),
|
'openTorrentContextMenu': (infoHash) => controllers.torrentList.openTorrentContextMenu(infoHash),
|
||||||
|
|
||||||
'startTorrentingSummary': (torrentSummary) =>
|
'startTorrentingSummary': (torrentSummary) =>
|
||||||
controllers.torrentList.startTorrentingSummary(torrentSummary),
|
controllers.torrentList.startTorrentingSummary(torrentSummary),
|
||||||
|
|
||||||
@@ -236,9 +235,9 @@ const dispatchHandlers = {
|
|||||||
|
|
||||||
// Everything else
|
// Everything else
|
||||||
'onOpen': (files) => onOpen(files),
|
'onOpen': (files) => onOpen(files),
|
||||||
'saveState': (state) => State.save(state),
|
|
||||||
'onError': (err) => onError(err),
|
'onError': (err) => onError(err),
|
||||||
'uncaughtError': (proc, err) => telemetry.logUncaughtError(proc, err)
|
'uncaughtError': (proc, err) => telemetry.logUncaughtError(proc, err),
|
||||||
|
'saveState': () => State.save(state)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Events from the UI never modify state directly. Instead they call dispatch()
|
// Events from the UI never modify state directly. Instead they call dispatch()
|
||||||
@@ -284,6 +283,8 @@ function setupIpc () {
|
|||||||
ipcRenderer.on('wt-uncaught-error', (e, err) => telemetry.logUncaughtError('webtorrent', err))
|
ipcRenderer.on('wt-uncaught-error', (e, err) => telemetry.logUncaughtError('webtorrent', err))
|
||||||
|
|
||||||
ipcRenderer.send('ipcReady')
|
ipcRenderer.send('ipcReady')
|
||||||
|
|
||||||
|
State.on('savedState', () => ipcRenderer.send('savedState'))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Quits any modal popovers and returns to the torrent list screen
|
// Quits any modal popovers and returns to the torrent list screen
|
||||||
|
|||||||
Reference in New Issue
Block a user