Skip to content

Commit 832cbe2

Browse files
committed
chore: do not wait for open event
1 parent 6ab9b9d commit 832cbe2

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

packages/transport-webrtc/src/muxer.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { AbstractStreamMuxer } from '@libp2p/utils'
2-
import { pEvent } from 'p-event'
32
import { MUXER_PROTOCOL } from './constants.js'
43
import { createStream, WebRTCStream } from './stream.js'
54
import type { DataChannelOptions } from './index.js'
@@ -157,16 +156,6 @@ export class DataChannelMuxer extends AbstractStreamMuxer<WebRTCStream> implemen
157156

158157
this.log('open channel %d for protocol %s', channel.id, options?.protocol)
159158

160-
if (channel.readyState !== 'open') {
161-
this.log('channel ready state is "%s" and not "open", waiting for "open" event before creating stream', channel.readyState)
162-
await pEvent(channel, 'open', {
163-
rejectionEvents: [
164-
'close',
165-
'error'
166-
]
167-
})
168-
}
169-
170159
const stream = createStream({
171160
...options,
172161
...this.dataChannelOptions,

packages/transport-webrtc/src/stream.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,26 @@ export class WebRTCStream extends AbstractStream {
107107
}
108108
}
109109
this.addEventListener('close', cleanUpDatachannelOnClose)
110+
111+
// chrome can receive message events before the open even is fired - calling
112+
// code needs to attach message event listeners before these events occur
113+
// but we need to wait before sending any data so this has to be done async
114+
if (this.channel.readyState !== 'open') {
115+
this.log('channel ready state is "%s" and not "open", waiting for "open" event before sending data', this.channel.readyState)
116+
pEvent(this.channel, 'open', {
117+
rejectionEvents: [
118+
'close',
119+
'error'
120+
]
121+
})
122+
.then(() => {
123+
this.log('channel ready state is now "%s", dispatching drain', this.channel.readyState)
124+
this.safeDispatchEvent('drain')
125+
})
126+
.catch(err => {
127+
this.abort(err.error ?? err)
128+
})
129+
}
110130
}
111131

112132
sendNewStream (): void {

0 commit comments

Comments
 (0)