Skip to content

addEventListener is invoked with the wrong event type when leaving a channel in 2.2.2 #1869

@julianna-ciq

Description

@julianna-ciq

Minor Issue

Found while testing with @finos/fdc3 version 2.2.2 (latest published version).

When subscribing via fdc3.addEventListener, leaving a user channel results in a callback with an incorrect event type (channelChangedEvent instead of the expected userChannelChanged). Those are the repro steps of my tests:

// Add event listener and log received events. It fails in both cases when I do and do not pass listener type as first arg
fdc3.addEventListener(null, console.warn);

// Join a channel
await fdc3.joinUserChannel("fdc3.channel.1");

/* The handler is invoked with:
{ 
  type: "userChannelChanged",
  details: {
    currentChannelId: "fdc3.channel.1"
  }
}
*/

// Leave the current channel
await fdc3.leaveCurrentChannel();

// The handler is invoked with:
/*
{
  type: "channelChangedEvent",
  newChannelId: null
}
*/

The issue appears to originate from this code of the handler wrapper. When leaving a channel, the payload contains newChannelId: null (since there is no active channel). Because of this, the logic falls in the else block, which forwards message coming from the DACP right to the FDC3 API which is not the one described in the standard.

Also, newChannelId is deprecated in favor of currentChannelId . Both should be taken into consideration but the newer currentChannelId field is not being used in this logic.

@ksgeorgieva suggested the following fix of the wrapHandler function. She has already tested this suggestion and found that it works as expected:

function wrapHandler(handler: EventHandler): (msg: AgentEventMessage) => void {
  return (m: AgentEventMessage) => {
    if (m.type !== "channelChangedEvent") {
      // Ignore unsupported event types (or throw if appropriate)
      return;
    }
    // 'currentChannelId' takes precedence over deprecated 'newChannelId' if it's set.
    const currentChannelId =
      m.payload.currentChannelId ?? m.payload.newChannelId ?? null;
    // Creating correct channel changed even which follows the typings of the standard
    const channelChangedEvent: FDC3ChannelChangedEvent = {
      type: "userChannelChanged",
      details: { currentChannelId }
    };
    handler(channelChangedEvent);
  };
}

Area of Issue

  • App Directory
  • API
  • Context Data
  • Intents
  • Desktop Agent Bridging
  • Use Cases
  • Other

Issue Description

Additional Context

Metadata

Metadata

Assignees

Labels

FDC3 for Web BrowsersapiFDC3 API Working GroupbugSomething isn't workingjavascriptPull requests that update Javascript code

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions