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 => { cb => {
try { try {
const stream = fs.createWriteStream(tempFilename, { const stream = fs.createWriteStream(tempFilename)
flags: 'w' const next = () => {
}) const line = lines.shift()
for (const line of lines) { if (line === undefined) {
stream.write(line) stream.close(cb)
stream.write('\n') } else {
stream.write(line)
stream.write('\n')
setImmediate(next)
}
} }
stream.close(() => cb()) next()
} catch (err) { } catch (err) {
cb(err) cb(err)
} }

Loading…
Cancel
Save