fix: do not return from constructor

This commit is contained in:
nazeh 2022-08-31 11:52:12 +03:00
commit 01751b51bc
2 changed files with 5 additions and 5 deletions

View File

@ -245,7 +245,6 @@ class Channel {
module.exports = class Protomux {
constructor (stream, { alloc } = {}) {
if (stream.protomux) return stream.protomux
stream.protomux = this
this.isProtomux = true
@ -274,6 +273,7 @@ module.exports = class Protomux {
}
static from (stream, opts) {
if (stream.protomux) return stream.protomux
if (stream.isProtomux) return stream
return new this(stream, opts)
}

View File

@ -351,7 +351,7 @@ test('deduplicate muxers', function (t) {
replicate({ stream: sa }, { stream: sb })
const a = new Protomux(sa)
const a = Protomux.from(sa)
const foo = a.createChannel({
protocol: 'foo',
onopen () { t.pass('a remote opened') }
@ -364,10 +364,10 @@ test('deduplicate muxers', function (t) {
onmessage (message) { t.is(message, 'hello foo') }
})
const bfoo = new Protomux(sb).createChannel({ protocol: 'foo' })
const bfoo = Protomux.from(sb).createChannel({ protocol: 'foo' })
// Another Protomux instance for another protocol
const a2 = new Protomux(sa)
const a2 = Protomux.from(sa)
const bar = a2.createChannel({
protocol: 'bar',
onopen () { t.pass('a remote opened') }
@ -380,7 +380,7 @@ test('deduplicate muxers', function (t) {
onmessage (message) { t.is(message, 'hello bar') }
})
const bbar = new Protomux(sb).createChannel({ protocol: 'bar' })
const bbar = Protomux.from(sb).createChannel({ protocol: 'bar' })
t.plan(4)