Skip to content

Commit

Permalink
Merge pull request atom#18134 from atom/revert-17774-fb-mdt-atomic-co…
Browse files Browse the repository at this point in the history
…nfig-saves

Fix atom#18118 by reverting PR atom#17774: "Write config file atomically"
  • Loading branch information
daviwil authored Sep 28, 2018
2 parents 4f3d457 + 72f4a68 commit 5d30e4e
Showing 1 changed file with 5 additions and 32 deletions.
37 changes: 5 additions & 32 deletions src/config-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -38,20 +37,18 @@ 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)}\`.
${error.message}
`)
}
callback()
})()
})
})
)

this.requestLoad = _.debounce(() => this.reload(), 200)
this.requestSave = _.debounce((data) => writeQueue.push(data), 200)
Expand Down Expand Up @@ -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()
})
})
}

0 comments on commit 5d30e4e

Please sign in to comment.