From 1cf91c8a72b2d0a28d06afaadfd4cd13136e2f25 Mon Sep 17 00:00:00 2001 From: Rob Moffat Date: Thu, 26 Oct 2023 14:57:40 -0400 Subject: [PATCH] Close fix --- src/test/v2.0/fdc3-2_0-utils.ts | 16 ++++++++++------ src/test/v2.0/support/channels-support-2.0.ts | 3 ++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/test/v2.0/fdc3-2_0-utils.ts b/src/test/v2.0/fdc3-2_0-utils.ts index 24fd4985..8828e75a 100644 --- a/src/test/v2.0/fdc3-2_0-utils.ts +++ b/src/test/v2.0/fdc3-2_0-utils.ts @@ -1,5 +1,5 @@ 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"; @@ -7,7 +7,7 @@ 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 @@ -21,9 +21,10 @@ const broadcastCloseWindow = async (currentTest) => { } as AppControlContext); }; -export const waitForContext = (contextType: string, testId: string, channel?: Channel): Promise => { +export const waitForContext = async (contextType: string, testId: string, channel?: Channel): Promise => { let executionListener: Listener; - return new Promise(async (resolve) => { + + return {listenerPromise: new Promise(async (resolve) => { console.log(Date.now() + ` Waiting for type: "${contextType}", on channel: "${channel.id}" in test: "${testId}"`); const handler = (context: AppControlContext) => { @@ -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) { @@ -73,6 +75,8 @@ export const waitForContext = (contextType: string, testId: string, channel?: Ch } }; channel.getCurrentContext().then(ccHandler); - } - }); + } + }) + }; + }; diff --git a/src/test/v2.0/support/channels-support-2.0.ts b/src/test/v2.0/support/channels-support-2.0.ts index b93f2266..8581b446 100644 --- a/src/test/v2.0/support/channels-support-2.0.ts +++ b/src/test/v2.0/support/channels-support-2.0.ts @@ -52,7 +52,8 @@ export class ChannelControl2_0 implements ChannelControl { - 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) => {