Skip to content

Commit 8bd170d

Browse files
authored
fix: ensure we callback on .end (#9)
1 parent 5c64fb7 commit 8bd170d

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/muxer.js

+4
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ class MultiplexMuxer extends EventEmitter {
5656
/**
5757
* Ends the connection and all of its streams
5858
* @param {function(Error)} callback
59+
* @returns {void}
5960
*/
6061
end (callback) {
6162
callback = callback || noop
63+
if (this.multiplex.destroyed) {
64+
return nextTick(callback)
65+
}
6266
this.multiplex.once('close', callback)
6367
this.multiplex.close()
6468
}

test/muxer.spec.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 5] */
3+
'use strict'
4+
5+
const chai = require('chai')
6+
const dirtyChai = require('dirty-chai')
7+
const expect = chai.expect
8+
chai.use(require('chai-checkmark'))
9+
chai.use(dirtyChai)
10+
11+
const pair = require('pull-pair/duplex')
12+
const mplex = require('../src')
13+
14+
describe('Mplex', () => {
15+
it('multiple calls to end should call back', (done) => {
16+
const p = pair()
17+
const dialer = mplex.dialer(p[0])
18+
19+
expect(2).checks(done)
20+
21+
dialer.end((err) => {
22+
expect(err).to.not.exist().mark()
23+
})
24+
25+
dialer.end((err) => {
26+
expect(err).to.not.exist().mark()
27+
})
28+
})
29+
})

0 commit comments

Comments
 (0)