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

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