Revert "Write config file atomically"

This commit is contained in:
Jason Rudolph
2018-09-27 18:33:09 -04:00
committed by GitHub
parent 36147f8925
commit 72f4a68d13

View File

@@ -6,7 +6,6 @@ const {watchPath} = require('./path-watcher')
const CSON = require('season')
const Path = require('path')
const async = require('async')
const temp = require('temp')
const EVENT_TYPES = new Set([
'created',
@@ -38,11 +37,9 @@ class ConfigFile {
this.reloadCallbacks = []
// Use a queue to prevent multiple concurrent write to the same file.
const writeQueue = async.queue((data, callback) => {
(async () => {
try {
await writeCSONFileAtomically(this.path, data)
} catch (error) {
const writeQueue = async.queue((data, callback) =>
CSON.writeFile(this.path, data, error => {
if (error) {
this.emitter.emit('did-error', dedent `
Failed to write \`${Path.basename(this.path)}\`.
@@ -50,8 +47,8 @@ class ConfigFile {
`)
}
callback()
})()
})
})
)
this.requestLoad = _.debounce(() => this.reload(), 200)
this.requestSave = _.debounce((data) => writeQueue.push(data), 200)
@@ -119,27 +116,3 @@ class ConfigFile {
})
}
}
function writeCSONFile (path, data) {
return new Promise((resolve, reject) => {
CSON.writeFile(path, data, error => {
if (error) reject(error)
else resolve()
})
})
}
async function writeCSONFileAtomically (path, data) {
const tempPath = temp.path()
await writeCSONFile(tempPath, data)
await rename(tempPath, path)
}
function rename (oldPath, newPath) {
return new Promise((resolve, reject) => {
fs.rename(oldPath, newPath, error => {
if (error) reject(error)
else resolve()
})
})
}