diff --git a/index.js b/index.js index 5c45b4d..9d8bcdf 100644 --- a/index.js +++ b/index.js @@ -42,6 +42,8 @@ function openFile (file, sonic) { return } + const reopening = sonic._reopening + sonic.fd = fd sonic.file = file sonic._reopening = false @@ -54,13 +56,15 @@ function openFile (file, sonic) { sonic.emit('ready') } - if (sonic._reopening || sonic.destroyed) { + if (sonic.destroyed) { return } // start if ((!sonic._writing && sonic._len > sonic.minLength) || sonic._flushPending) { sonic._actualWrite() + } else if (reopening) { + process.nextTick(() => sonic.emit('drain')) } } @@ -461,6 +465,9 @@ SonicBoom.prototype.reopen = function (file) { throw new Error('Unable to reopen a file descriptor, you must pass a file to SonicBoom') } + if (file) { + this.file = file + } this._reopening = true if (this._writing) { @@ -478,7 +485,7 @@ SonicBoom.prototype.reopen = function (file) { } }) - openFile(file || this.file, this) + openFile(this.file, this) } SonicBoom.prototype.end = function () { diff --git a/test/reopen.test.js b/test/reopen.test.js index 4bc4231..13fd8ad 100644 --- a/test/reopen.test.js +++ b/test/reopen.test.js @@ -32,7 +32,7 @@ function buildTests (test, sync) { t.pass('ready emitted') t.ok(stream.write('after reopen\n')) - stream.on('drain', () => { + stream.once('drain', () => { fs.readFile(after, 'utf8', (err, data) => { t.error(err) t.equal(data, 'hello world\nsomething else\n') @@ -70,7 +70,7 @@ function buildTests (test, sync) { t.ok(stream.write('after reopen\n')) stream.flush() - stream.on('drain', () => { + stream.once('drain', () => { fs.readFile(after, 'utf8', (err, data) => { t.error(err) t.equal(data, 'hello world\nsomething else\n') @@ -103,7 +103,7 @@ function buildTests (test, sync) { }) test('reopen with file', (t) => { - t.plan(9) + t.plan(10) const dest = file() const stream = new SonicBoom({ dest, minLength: 0, sync }) @@ -117,12 +117,13 @@ function buildTests (test, sync) { t.pass('drain emitted') stream.reopen(after) + t.equal(stream.file, after) stream.once('ready', () => { t.pass('ready emitted') t.ok(stream.write('after reopen\n')) - stream.on('drain', () => { + stream.once('drain', () => { fs.readFile(dest, 'utf8', (err, data) => { t.error(err) t.equal(data, 'hello world\nsomething else\n') @@ -199,4 +200,40 @@ function buildTests (test, sync) { }, 0) }) }) + + test('reopen emits drain', (t) => { + t.plan(9) + + const dest = file() + const stream = new SonicBoom({ dest, sync }) + + t.ok(stream.write('hello world\n')) + t.ok(stream.write('something else\n')) + + const after = dest + '-moved' + + stream.once('drain', () => { + t.pass('drain emitted') + + fs.renameSync(dest, after) + stream.reopen() + + stream.once('drain', () => { + t.pass('drain emitted') + t.ok(stream.write('after reopen\n')) + + stream.once('drain', () => { + fs.readFile(after, 'utf8', (err, data) => { + t.error(err) + t.equal(data, 'hello world\nsomething else\n') + fs.readFile(dest, 'utf8', (err, data) => { + t.error(err) + t.equal(data, 'after reopen\n') + stream.end() + }) + }) + }) + }) + }) + }) }