Skip to content

Commit

Permalink
fix: prevent unloading when it's in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-ext committed Dec 23, 2023
1 parent 9ef0d58 commit 3c09f44
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/controllers/export-data-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class ExportDataForm extends HTMLElement {

private object_url: string | undefined;

handle_before_unload = (ev: BeforeUnloadEvent) => {
ev.preventDefault();
ev.returnValue = true;
};

connectedCallback() {
if (!supports_fsa) {
const $fsa_warning = this.fsa_warning.get()!;
Expand Down Expand Up @@ -82,16 +87,24 @@ class ExportDataForm extends HTMLElement {
return;
}

window.addEventListener('beforeunload', this.handle_before_unload);

this.download_archive(signal, fd, identifier, with_media).then(
() => {
// If we got here, we're still dealing with our own controller.
controller!.abort();

window.removeEventListener('beforeunload', this.handle_before_unload);
},
(err) => {
if (signal.aborted) {
return;
}

console.error(err);

window.removeEventListener('beforeunload', this.handle_before_unload);

$status.textContent = err.message;
$status.classList.add('text-red-500');
$status.classList.remove('opacity-50');
Expand Down
11 changes: 11 additions & 0 deletions src/controllers/generate-archive-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class GenerateArchiveForm extends HTMLElement {

private fsa_large_warning = target<HTMLDivElement>(this, 'fsa_large_warning');

handle_before_unload = (ev: BeforeUnloadEvent) => {
ev.preventDefault();
ev.returnValue = true;
};

connectedCallback() {
{
const $picker_input = this.picker_input.get()!;
Expand Down Expand Up @@ -96,10 +101,14 @@ class GenerateArchiveForm extends HTMLElement {
return;
}

window.addEventListener('beforeunload', this.handle_before_unload);

this.generate_archive(signal, fd, archive, with_media).then(
() => {
// If we got here, we're still dealing with our own controller.
controller!.abort();

window.removeEventListener('beforeunload', this.handle_before_unload);
},
(err) => {
if (signal.aborted) {
Expand All @@ -108,6 +117,8 @@ class GenerateArchiveForm extends HTMLElement {

console.error(err);

window.removeEventListener('beforeunload', this.handle_before_unload);

$status.textContent = err.message;
$status.classList.add('text-red-500');
$status.classList.remove('opacity-50');
Expand Down

0 comments on commit 3c09f44

Please sign in to comment.