Rename events to be consistent

- Make State.save() always throttle calls -- since that's the common
case.

- Immediate saves are now the exception, with State.saveImmediate().

- The function is called State.save(), so the dispatch event should be
'stateSave'.
This commit is contained in:
Feross Aboukhadijeh
2016-09-21 11:59:23 -07:00
parent 1e05487acd
commit 7c158e9f2c
7 changed files with 21 additions and 20 deletions

View File

@@ -102,8 +102,8 @@ function init () {
app.isQuitting = true app.isQuitting = true
e.preventDefault() e.preventDefault()
windows.main.dispatch('saveState') // try to save state on exit windows.main.dispatch('stateSaveImmediate') // try to save state on exit
ipcMain.once('savedState', () => app.quit()) ipcMain.once('stateSaved', () => app.quit())
setTimeout(() => { setTimeout(() => {
console.error('Saving state took too long. Quitting.') console.error('Saving state took too long. Quitting.')
app.quit() app.quit()

View File

@@ -55,7 +55,7 @@ module.exports = class PrefsController {
ipcRenderer.send('setStartup', state.unsaved.prefs.startup) ipcRenderer.send('setStartup', state.unsaved.prefs.startup)
} }
state.saved.prefs = Object.assign(state.saved.prefs || {}, state.unsaved.prefs) state.saved.prefs = Object.assign(state.saved.prefs || {}, state.unsaved.prefs)
dispatch('saveState') dispatch('stateSaveImmediate')
dispatch('checkDownloadPath') dispatch('checkDownloadPath')
} }
} }

View File

@@ -129,20 +129,20 @@ module.exports = class TorrentController {
const torrentSummary = this.getTorrentSummary(torrentKey) const torrentSummary = this.getTorrentSummary(torrentKey)
if (!torrentSummary) throw new Error('Not saving modtimes for deleted torrent ' + torrentKey) if (!torrentSummary) throw new Error('Not saving modtimes for deleted torrent ' + torrentKey)
torrentSummary.fileModtimes = fileModtimes torrentSummary.fileModtimes = fileModtimes
dispatch('saveStateThrottled') dispatch('stateSave')
} }
torrentFileSaved (torrentKey, torrentFileName) { torrentFileSaved (torrentKey, torrentFileName) {
console.log('torrent file saved %s: %s', torrentKey, torrentFileName) console.log('torrent file saved %s: %s', torrentKey, torrentFileName)
const torrentSummary = this.getTorrentSummary(torrentKey) const torrentSummary = this.getTorrentSummary(torrentKey)
torrentSummary.torrentFileName = torrentFileName torrentSummary.torrentFileName = torrentFileName
dispatch('saveStateThrottled') dispatch('stateSave')
} }
torrentPosterSaved (torrentKey, posterFileName) { torrentPosterSaved (torrentKey, posterFileName) {
const torrentSummary = this.getTorrentSummary(torrentKey) const torrentSummary = this.getTorrentSummary(torrentKey)
torrentSummary.posterFileName = posterFileName torrentSummary.posterFileName = posterFileName
dispatch('saveStateThrottled') dispatch('stateSave')
} }
torrentAudioMetadata (infoHash, index, info) { torrentAudioMetadata (infoHash, index, info) {

View File

@@ -158,7 +158,7 @@ module.exports = class TorrentListController {
// remove torrent from saved list // remove torrent from saved list
this.state.saved.torrents.splice(index, 1) this.state.saved.torrents.splice(index, 1)
dispatch('saveStateThrottled') dispatch('stateSave')
} }
// prevent user from going forward to a deleted torrent // prevent user from going forward to a deleted torrent

View File

@@ -1,4 +1,4 @@
const State = require('../lib/state') const {dispatch} = require('../lib/dispatcher')
// Controls the UI checking for new versions of the app, prompting install // Controls the UI checking for new versions of the app, prompting install
module.exports = class UpdateController { module.exports = class UpdateController {
@@ -21,6 +21,6 @@ module.exports = class UpdateController {
let skipped = this.state.saved.skippedVersions let skipped = this.state.saved.skippedVersions
if (!skipped) skipped = this.state.saved.skippedVersions = [] if (!skipped) skipped = this.state.saved.skippedVersions = []
skipped.push(version) skipped.push(version)
State.saveThrottled(this.state) dispatch('stateSave')
} }
} }

View File

@@ -6,13 +6,14 @@ const {EventEmitter} = require('events')
const config = require('../../config') const config = require('../../config')
const migrations = require('./migrations') const migrations = require('./migrations')
const SAVE_THROTTLED_INTERVAL = 1000 const SAVE_DEBOUNCE_INTERVAL = 1000
const State = module.exports = Object.assign(new EventEmitter(), { const State = module.exports = Object.assign(new EventEmitter(), {
getDefaultPlayState, getDefaultPlayState,
load, load,
save, // state.save() calls are rate-limited. Use state.saveImmediate() to skip limit.
saveThrottled: debounce(save, SAVE_THROTTLED_INTERVAL) save: debounce(saveImmediate, SAVE_DEBOUNCE_INTERVAL),
saveImmediate
}) })
appConfig.filePath = path.join(config.CONFIG_PATH, 'config.json') appConfig.filePath = path.join(config.CONFIG_PATH, 'config.json')
@@ -101,7 +102,7 @@ function getDefaultPlayState () {
} }
/* If the saved state file doesn't exist yet, here's what we use instead */ /* If the saved state file doesn't exist yet, here's what we use instead */
function setupSavedState (cb) { function setupStateSaved (cb) {
const fs = require('fs-extra') const fs = require('fs-extra')
const parseTorrent = require('parse-torrent') const parseTorrent = require('parse-torrent')
const parallel = require('run-parallel') const parallel = require('run-parallel')
@@ -185,7 +186,7 @@ function load (cb) {
appConfig.read(function (err, saved) { appConfig.read(function (err, saved) {
if (err || !saved.version) { if (err || !saved.version) {
console.log('Missing config file: Creating new one') console.log('Missing config file: Creating new one')
setupSavedState(onSaved) setupStateSaved(onSaved)
} else { } else {
onSaved(null, saved) onSaved(null, saved)
} }
@@ -200,7 +201,7 @@ function load (cb) {
} }
// Write state.saved to the JSON state file // Write state.saved to the JSON state file
function save (state, cb) { function saveImmediate (state, cb) {
console.log('Saving state to ' + appConfig.filePath) console.log('Saving state to ' + appConfig.filePath)
// Clean up, so that we're not saving any pending state // Clean up, so that we're not saving any pending state
@@ -228,6 +229,6 @@ function save (state, cb) {
appConfig.write(copy, (err) => { appConfig.write(copy, (err) => {
if (err) console.error(err) if (err) console.error(err)
else State.emit('savedState') else State.emit('stateSaved')
}) })
} }

View File

@@ -257,8 +257,8 @@ const dispatchHandlers = {
'onOpen': onOpen, 'onOpen': onOpen,
'error': onError, 'error': onError,
'uncaughtError': (proc, err) => telemetry.logUncaughtError(proc, err), 'uncaughtError': (proc, err) => telemetry.logUncaughtError(proc, err),
'saveState': () => State.save(state), 'stateSave': () => State.save(state),
'saveStateThrottled': () => State.saveThrottled(state), 'stateSaveImmediate': () => State.saveImmediate(state),
'update': () => {} // No-op, just trigger an update 'update': () => {} // No-op, just trigger an update
} }
@@ -308,7 +308,7 @@ function setupIpc () {
ipcRenderer.send('ipcReady') ipcRenderer.send('ipcReady')
State.on('savedState', () => ipcRenderer.send('savedState')) State.on('stateSaved', () => ipcRenderer.send('stateSaved'))
} }
// Quits any modal popovers and returns to the torrent list screen // Quits any modal popovers and returns to the torrent list screen
@@ -465,7 +465,7 @@ function onFullscreenChanged (e, isFullScreen) {
function onWindowBoundsChanged (e, newBounds) { function onWindowBoundsChanged (e, newBounds) {
state.saved.bounds = newBounds state.saved.bounds = newBounds
dispatch('saveStateThrottled') dispatch('stateSave')
} }
function checkDownloadPath () { function checkDownloadPath () {