Skip to content

Commit 226d76a

Browse files
feat: de-duplicate queued messages for not-yet-open channel (#6)
* deduplicate queued messages in not-yet-open channel to avoid jittery behavior for recipient
1 parent 06ebceb commit 226d76a

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

docs/react.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ if (channel.isOpen()) {
6060

6161
> Note: if you or another MUPPET client attempts to send events over a channel before a receiving client is connected, the events will be temporarily held in an in-memory queue in the sender's app (will not disappear).
6262
>
63-
> As soon as both apps have connected to the same MuppetChannel, all of these pending events will be "replayed"--sent over the channel--so the receiver can receive them.
63+
> As soon as both apps have connected to the same MuppetChannel, all of these pending events will be "replayed"--sent over the channel--so the receiver can receive them. Any queued messages with the same `destination` and `eventClass` will be de-duplicated--using only the latest one--to avoid jittery behavior in the receiver when it comes online.
6464
6565
## Hooks
6666

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@noaa-gsl/idsse-muppet",
3-
"version": "0.0.12",
3+
"version": "0.0.13",
44
"description": "Connect to, send, and receive data over MUPPET data channels in convenient React interfaces using the MUPPETs protocol",
55
"main": "index.js",
66
"scripts": {

src/MuppetChannel.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,14 @@ class MuppetChannel {
313313
this.#dataChannel,
314314
);
315315
if (!this.isOpen()) {
316-
console.warn(`[${this.#clientName}] [${this.room}] Channel not ready to receive data`);
316+
console.warn(
317+
`[${this.#clientName}] [${this.room}] Channel not ready to receive data. Message queued`,
318+
);
319+
320+
// de-duplicate queue, so recipient app won't try to process successive state changes when it comes online
321+
this.#queuedMessages = this.#queuedMessages.filter(
322+
(it) => !(it.eventClass === data.eventClass && it.destination === it.destination),
323+
);
317324
this.#queuedMessages.push(data); // stash this message until channel opens (hopefully)
318325
return false;
319326
}
@@ -337,7 +344,7 @@ class MuppetChannel {
337344
* @param {string} requestId (optional) the UUID of the original message that this message is responding to.
338345
* @returns {object} the message, ready to be sent over WebRTC
339346
*/
340-
#createMuppetMessage = (eventClass, event, destination, requestId = null) => ({
347+
buildMuppetMessage = (eventClass, event, destination, requestId = null) => ({
341348
id: crypto.randomUUID(),
342349
destination,
343350
requestId: requestId || undefined,
@@ -357,11 +364,11 @@ class MuppetChannel {
357364
* @returns {boolean} True if message sent successfully.
358365
*/
359366
sendEvent = ({ eventClass, event = {}, destination = '*', requestId = null }) => {
360-
console.log(
367+
console.debug(
361368
`[${this.#clientName}] [${this.room}] Sending MUPPET event ${eventClass} with body`,
362369
event,
363370
);
364-
const data = this.#createMuppetMessage(eventClass, event, destination, requestId);
371+
const data = this.buildMuppetMessage(eventClass, event, destination, requestId);
365372
return this.sendRawData(data);
366373
};
367374

@@ -383,7 +390,7 @@ class MuppetChannel {
383390
requestReject = reject;
384391
});
385392

386-
const message = this.#createMuppetMessage(eventClass, event, destination);
393+
const message = this.buildMuppetMessage(eventClass, event, destination);
387394
// track this message as expecting a response, saving the resolve() to be invoked by onReceiveMessage
388395
this.#requests[message.id] = { callback: requestResolve };
389396

0 commit comments

Comments
 (0)