Skip to content

Commit

Permalink
fix: work around a race in waitForFileChooser
Browse files Browse the repository at this point in the history
Closes #6040
  • Loading branch information
OrKoN committed Sep 6, 2022
1 parent 3240095 commit 59fa9ce
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/common/Page.ts
Expand Up @@ -766,22 +766,26 @@ export class Page extends EventEmitter {
async waitForFileChooser(
options: WaitTimeoutOptions = {}
): Promise<FileChooser> {
if (!this.#fileChooserPromises.size) {
await this.#client.send('Page.setInterceptFileChooserDialog', {
enabled: true,
});
}

const {timeout = this.#timeoutSettings.timeout()} = options;
const promise = createDeferredPromise<FileChooser>({
message: `Waiting for \`FileChooser\` failed: ${timeout}ms exceeded`,
timeout,
});
this.#fileChooserPromises.add(promise);
return promise.catch(error => {
this.#fileChooserPromises.delete(promise);
throw error;
});
let enablePromise: Promise<void> | undefined;
if (!this.#fileChooserPromises.size) {
enablePromise = this.#client.send('Page.setInterceptFileChooserDialog', {
enabled: true,
});
}
return Promise.all([promise, enablePromise])
.then(([result]) => {
return result;
})
.catch(error => {
this.#fileChooserPromises.delete(promise);
throw error;
});
}

/**
Expand Down

0 comments on commit 59fa9ce

Please sign in to comment.