Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: work around a race in waitForFileChooser (#8905)
Closes #6040
  • Loading branch information
OrKoN committed Sep 6, 2022
1 parent b4f5ea1 commit 053d960
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/common/Page.ts
Expand Up @@ -763,25 +763,28 @@ export class Page extends EventEmitter {
* await fileChooser.accept(['/tmp/myfile.pdf']);
* ```
*/
async waitForFileChooser(
options: WaitTimeoutOptions = {}
): Promise<FileChooser> {
if (!this.#fileChooserPromises.size) {
await this.#client.send('Page.setInterceptFileChooserDialog', {
enabled: true,
});
}

waitForFileChooser(options: WaitTimeoutOptions = {}): Promise<FileChooser> {
const needsEnable = this.#fileChooserPromises.size === 0;
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 (needsEnable) {
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 053d960

Please sign in to comment.