Skip to content

Commit

Permalink
fix finish not being emitted after end() while reopening (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
SerayaEryn authored and mcollina committed Jan 5, 2019
1 parent e7ad0b4 commit c8aebe2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const flatstr = require('flatstr')
const inherits = require('util').inherits

function openFile (file, sonic) {
sonic._opening = true
sonic._writing = true
sonic.file = file
fs.open(file, 'a', (err, fd) => {
Expand All @@ -15,6 +16,7 @@ function openFile (file, sonic) {
}

sonic.fd = fd
sonic._opening = false
sonic._writing = false

sonic.emit('ready')
Expand Down Expand Up @@ -197,6 +199,12 @@ SonicBoom.prototype.end = function () {
return
}

if (this._writing && this._opening) {
this.once('ready', () => {
actualWrite(this)
})
}

if (this._writing) {
return
}
Expand Down
22 changes: 22 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,28 @@ function buildTests (test, sync) {
})
})

test('end after reopen', (t) => {
t.plan(4)

const dest = file()
const stream = new SonicBoom(dest, 4096, sync)

stream.once('ready', () => {
t.pass('ready emitted')
const after = dest + '-moved'
stream.reopen(after)
stream.write('after reopen\n')
stream.on('finish', () => {
t.pass('finish emitted')
fs.readFile(after, 'utf8', (err, data) => {
t.error(err)
t.equal(data, 'after reopen\n')
})
})
stream.end()
})
})

test('reopen with file', (t) => {
t.plan(9)

Expand Down

0 comments on commit c8aebe2

Please sign in to comment.