Skip to content

Commit

Permalink
potential fix for promises resolving in wrong order.
Browse files Browse the repository at this point in the history
  • Loading branch information
robmoffat committed Sep 17, 2024
1 parent 4ea3768 commit b320883
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/test/v2.0/support/intent-support-2.0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@ export class RaiseIntentControl2_0 {
async receiveContext(contextType: string, waitTime?: number): Promise<AppControlContext> {
let timeout;
const appControlChannel = await getOrCreateChannel(constants.ControlChannel);
return new Promise<Context>(async (resolve, reject) => {
const listener = await appControlChannel.addContextListener(contextType, (context: AppControlContext) => {
resolve(context);
clearTimeout(timeout);
listener.unsubscribe();
});

var doResolve = null;
const out = new Promise<Context>(async (resolve, reject) => {
doResolve = resolve;
//if no context received reject promise
const { promise: sleepPromise, timeout: theTimeout } = sleep(waitTime ?? constants.WaitTime);
timeout = theTimeout;
await sleepPromise;
reject(new Error("No context received. Listener expected to receive context of type " + contextType + " from mock app"));
});

const listener = await appControlChannel.addContextListener(contextType, (context: AppControlContext) => {
doResolve(context);
clearTimeout(timeout);
listener.unsubscribe();
});

return out
}

async openIntentApp(appId): Promise<AppIdentifier> {
Expand Down Expand Up @@ -82,7 +86,7 @@ export class RaiseIntentControl2_0 {
const intentResult = intentResolution.getResult();
if (typeof intentResult.then !== "function") {
assert.fail(`intentResolution.getResult() did not return a Promise: ${JSON.stringify(intentResult, null, 2)}`);
}
}
clearTimeout(timeout);
return intentResult;
}
Expand Down

0 comments on commit b320883

Please sign in to comment.