Skip to content

Commit

Permalink
fix(fdc3) - using queueMicroTask in callback instead of the setTimeou…
Browse files Browse the repository at this point in the history
…t when calling handler after returning back the context listener object (#911)
  • Loading branch information
lilla28 authored Feb 4, 2025
1 parent c3122aa commit b4feb82
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
19 changes: 11 additions & 8 deletions src/fdc3/js/composeui-fdc3/src/ComposeUIDesktopAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,11 @@ export class ComposeUIDesktopAgent implements DesktopAgent {
return listener;
}

await this.getLastContext(listener);

return listener;
return await new Promise<Listener>((resolve) => {
resolve(listener);
}).finally(() => {
queueMicrotask(async () => await this.callHandlerOnChannelsCurrentContext(listener));
});
}

public async getUserChannels(): Promise<Array<Channel>> {
Expand All @@ -162,8 +164,10 @@ export class ComposeUIDesktopAgent implements DesktopAgent {
this.currentChannel = channel;

for (const listener of this.topLevelContextListeners) {
await listener.subscribe(this.currentChannel.id, this.currentChannel.type);
await this.getLastContext(listener);
await listener.subscribe(this.currentChannel.id, this.currentChannel.type)
.finally(() => {
queueMicrotask(async () => await this.callHandlerOnChannelsCurrentContext(listener));
});
}
}

Expand Down Expand Up @@ -239,12 +243,11 @@ export class ComposeUIDesktopAgent implements DesktopAgent {
}
}

private async getLastContext(listener: ComposeUIContextListener) : Promise<void> {
private async callHandlerOnChannelsCurrentContext(listener: ComposeUIContextListener) : Promise<void> {
const lastContext = await this.currentChannel!.getCurrentContext(listener.contextType);

if (lastContext) {
//TODO: timing issue
setTimeout(async() => await listener.handleContextMessage(lastContext), 100);
await listener.handleContextMessage(lastContext);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export class ComposeUIContextListener implements Listener {
//TODO: integration test
const context = <Context>JSON.parse(topicMessage.payload!);
if (!this.contextType || this.contextType == context!.type) {

if (this.openHandled === true) {
this.handler!(context!);
} else {
Expand Down

0 comments on commit b4feb82

Please sign in to comment.