Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2101,6 +2101,7 @@ class RTCDataChannel extends SimpleEventTarget {

_dispatchNativeMessageBatch(events) {
if (this._readyState === "closed") return true;
if (this._queuedMessageEvents.length > 0) return false;
const pendingPeer =
this._pc._operationsPending > 0
? this._pc
Expand Down
34 changes: 34 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,40 @@ test("data-channel opening burst is delivered after the datachannel event task",
answerer.close();
});

test("native message batches do not overtake queued messages", async (t) => {
const peerConnection = new RTCPeerConnection();
t.after(() => closeAllAndWait(peerConnection));
const channel = peerConnection.createDataChannel("queued-before-native-batch");
const queuedEvents = ["message 0", "message 1"].map((data) => ({
type: "message",
binary: false,
data,
}));
const nativeBatch = ["message 2", "message 3"].map((data) => ({
type: "message",
binary: false,
data,
}));
const received = [];
const done = new Promise((resolve) => {
channel.onmessage = (event) => {
received.push(event.data);
if (received.length === queuedEvents.length + nativeBatch.length) resolve();
};
});

channel._queueMessageEvents(queuedEvents);
if (!channel._dispatchNativeMessageBatch(nativeBatch)) {
channel._queueMessageEvents(nativeBatch);
}
await done;

assert.deepEqual(
received,
[...queuedEvents, ...nativeBatch].map((event) => event.data),
);
});

test("message listener added during onmessage receives later burst messages", async (t) => {
const offerer = new RTCPeerConnection();
const answerer = new RTCPeerConnection();
Expand Down
Loading