Skip to content

Commit

Permalink
refactor: removed IE leftovers from src\client\core\barriers (#7997)
Browse files Browse the repository at this point in the history
<!--
Thank you for your contribution.

Before making a PR, please read our contributing guidelines at

https://github.com/DevExpress/testcafe/blob/master/CONTRIBUTING.md#code-contribution

We recommend creating a *draft* PR, so that you can mark it as 'ready
for review' when you are done.
-->

## Purpose
_Describe the problem you want to address or the feature you want to
implement._

## Approach
_Describe how your changes address the issue or implement the desired
functionality in as much detail as possible._

## References
_Provide a link to the existing issue(s), if any._

## Pre-Merge TODO
- [ ] Write tests for your proposed changes
- [ ] Make sure that existing tests do not fail
  • Loading branch information
Aleksey28 authored Sep 12, 2023
1 parent f7943e6 commit 95e42bc
Showing 1 changed file with 13 additions and 58 deletions.
71 changes: 13 additions & 58 deletions src/client/core/barriers/page-unload-barrier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ import hammerhead from '../deps/hammerhead';
import * as eventUtils from '../utils/event';
import delay from '../utils/delay';
import MESSAGE from '../../../test-run/client-messages';
import { isAnchorElement } from '../utils/dom';

const Promise = hammerhead.Promise;
const browserUtils = hammerhead.utils.browser;
const nativeMethods = hammerhead.nativeMethods;
const transport = hammerhead.transport;
const Promise = hammerhead.Promise;
const transport = hammerhead.transport;


const DEFAULT_BARRIER_TIMEOUT = 400;
const SHORT_WAIT_FOR_UNLOAD_TIMEOUT = 30;
const FILE_DOWNLOAD_CHECK_DELAY = 500;
const MAX_UNLOADING_TIMEOUT = 15_000;
const DEFAULT_BARRIER_TIMEOUT = 400;
const FILE_DOWNLOAD_CHECK_DELAY = 500;
const MAX_UNLOADING_TIMEOUT = 15_000;


let unloading = false;
Expand All @@ -22,48 +18,9 @@ let pageNavigationTriggeredListener = null as (() => void) | null;
let pageNavigationTriggered = false;

function onBeforeUnload (): void {
if (browserUtils.isIE) {
prolongUnloadWaitingIeOnly(SHORT_WAIT_FOR_UNLOAD_TIMEOUT);
exceptFileDownloadingIeOnly();

return;
}

unloading = true;
}

// NOTE: this variables are for IE only
let waitingForUnload = false;
let waitingForUnloadTimeoutId = null as (() => void) | null;
let waitingPromiseResolvers = [] as (() => void)[];

function prolongUnloadWaitingIeOnly (timeout: number): void {
if (waitingForUnloadTimeoutId)
nativeMethods.clearTimeout.call(window, waitingForUnloadTimeoutId);

waitingForUnload = true;

waitingForUnloadTimeoutId = nativeMethods.setTimeout.call(window, () => {
waitingForUnloadTimeoutId = null;
waitingForUnload = false;

waitingPromiseResolvers.forEach(resolve => resolve());
waitingPromiseResolvers = [];
}, timeout);
}

function exceptFileDownloadingIeOnly (): void {
delay(0)
.then(() => {
// NOTE: except file downloading
if (document.readyState === 'loading') {
const activeElement = nativeMethods.documentActiveElementGetter.call(document);

if (!activeElement || !isAnchorElement(activeElement) || !activeElement.hasAttribute('download'))
unloading = true;
}
});
}

function waitForFileDownload (): Promise<void> {
return delay(FILE_DOWNLOAD_CHECK_DELAY)
Expand Down Expand Up @@ -104,17 +61,15 @@ export function wait (timeout?: number): Promise<void> {
}

const waitForUnloadingPromise = delay(timeout)
// eslint-disable-next-line consistent-return
.then((): void | Promise<void> => {
if (unloading) {
return waitForFileDownload()
.then(() => {
unloading = false;
});
}

if (waitingForUnload)
return new Promise((resolve: () => void) => waitingPromiseResolvers.push(resolve));
if (!unloading)
return void 0;

return waitForFileDownload()
.then(() => {
unloading = false;
});

});

// NOTE: sometimes the page isn't actually unloaded after the beforeunload event
Expand Down

0 comments on commit 95e42bc

Please sign in to comment.