Skip to content

Commit

Permalink
emit drain after reopening
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <hello@matteocollina.com>
  • Loading branch information
mcollina committed Apr 4, 2024
1 parent 074e574 commit 3e74123
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
11 changes: 9 additions & 2 deletions index.js
Expand Up @@ -42,6 +42,8 @@ function openFile (file, sonic) {
return
}

const reopening = sonic._reopening

sonic.fd = fd
sonic.file = file
sonic._reopening = false
Expand All @@ -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'))
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -478,7 +485,7 @@ SonicBoom.prototype.reopen = function (file) {
}
})

openFile(file || this.file, this)
openFile(this.file, this)
}

SonicBoom.prototype.end = function () {
Expand Down
45 changes: 41 additions & 4 deletions test/reopen.test.js
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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 })
Expand All @@ -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')
Expand Down Expand Up @@ -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()
})
})
})
})
})
})
}

0 comments on commit 3e74123

Please sign in to comment.