Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: removed IE leftovers from src\client\core\barriers #7997

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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