Stream lines using setImmediate to ensure it doesn't block event loop

pull/5/head
eliot-akira 3 years ago
parent ba14f11949
commit 7404ee1bc2
  1. 18
      lib/storage.js

@ -89,14 +89,18 @@ storage.crashSafeWriteFileLines = (filename, lines, callback = () => {}) => {
},
cb => {
try {
const stream = fs.createWriteStream(tempFilename, {
flags: 'w'
})
for (const line of lines) {
stream.write(line)
stream.write('\n')
const stream = fs.createWriteStream(tempFilename)
const next = () => {
const line = lines.shift()
if (line === undefined) {
stream.close(cb)
} else {
stream.write(line)
stream.write('\n')
setImmediate(next)
}
}
stream.close(() => cb())
next()
} catch (err) {
cb(err)
}

Loading…
Cancel
Save