Skip to content

Commit

Permalink
Close fix
Browse files Browse the repository at this point in the history
  • Loading branch information
robmoffat committed Oct 26, 2023
1 parent 884b961 commit 1cf91c8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/test/v2.0/fdc3-2_0-utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Channel, Context, DesktopAgent, Listener } from "fdc3_2_0";
import { AppControlContext } from "../../context-types";
import { AppControlContext, AppControlContextListener } from "../../context-types";
import constants from "../../constants";
import { wait } from "../../utils";

declare let fdc3: DesktopAgent;

export async function closeMockAppWindow(testId: string) {
const appControlChannel = await fdc3.getOrCreateChannel(constants.ControlChannel);
const contextPromise = waitForContext("windowClosed", testId, appControlChannel);
const { listenerPromise: contextPromise } = await waitForContext("windowClosed", testId, appControlChannel);
await broadcastCloseWindow(testId);
await contextPromise;
await wait(constants.WindowCloseWaitTime); // wait for window to close
Expand All @@ -21,9 +21,10 @@ const broadcastCloseWindow = async (currentTest) => {
} as AppControlContext);
};

export const waitForContext = (contextType: string, testId: string, channel?: Channel): Promise<Context> => {
export const waitForContext = async (contextType: string, testId: string, channel?: Channel): Promise<AppControlContextListener> => {
let executionListener: Listener;
return new Promise<Context>(async (resolve) => {

return {listenerPromise: new Promise<Context>(async (resolve) => {
console.log(Date.now() + ` Waiting for type: "${contextType}", on channel: "${channel.id}" in test: "${testId}"`);

const handler = (context: AppControlContext) => {
Expand All @@ -49,6 +50,7 @@ export const waitForContext = (contextType: string, testId: string, channel?: Ch
console.log("adding listener in waitforcontext");
executionListener = await channel.addContextListener(contextType, handler);
//App channels do not auto-broadcast current context when you start listening, so retrieve current context to avoid races
//TODO remove this once we know that the listeners are always in place before they are used
const ccHandler = async (context: AppControlContext) => {
if (context) {
if (testId) {
Expand All @@ -73,6 +75,8 @@ export const waitForContext = (contextType: string, testId: string, channel?: Ch
}
};
channel.getCurrentContext().then(ccHandler);
}
});
}
})
};

};
3 changes: 2 additions & 1 deletion src/test/v2.0/support/channels-support-2.0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export class ChannelControl2_0 implements ChannelControl<Channel, Context, Liste
};

initCompleteListener = async (testId: string) => {
return await waitForContext("executionComplete", testId, await fdc3.getOrCreateChannel(constants.ControlChannel));
const { listenerPromise } = await waitForContext("executionComplete", testId, await fdc3.getOrCreateChannel(constants.ControlChannel));
return listenerPromise;
};

openChannelApp = async (testId: string, channelId: string | undefined, commands: string[], historyItems: number = undefined, notify: boolean = true, contextId?: string) => {
Expand Down

0 comments on commit 1cf91c8

Please sign in to comment.