Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Teach TurboRequestsService how to abort requests

Alexander Brandon Coles96538b9c64ec
diff --git a/frontend/src/app/core/turbo/turbo-requests.service.ts b/frontend/src/app/core/turbo/turbo-requests.service.ts
index 76305e6a10f..169df5d83ee 100644
--- a/frontend/src/app/core/turbo/turbo-requests.service.ts
+++ b/frontend/src/app/core/turbo/turbo-requests.service.ts
@@ -1,19 +1,35 @@
import { Injectable } from '@angular/core';
import { renderStreamMessage } from '@hotwired/turbo';
import { ToastService } from 'core-app/shared/components/toaster/toast.service';
+import { debugLog } from 'core-app/shared/helpers/debug_output';
@Injectable({ providedIn: 'root' })
export class TurboRequestsService {
+ #controllers = new Map<string, AbortController>();
+
constructor(
private toast:ToastService,
) {
}
- public request(url:string, init:RequestInit = {}, suppressErrorToast = false):Promise<{
+ public request(
+ url:string,
+ init:RequestInit = {},
+ suppressErrorToast = false,
+ requestId?:string,
+ ):Promise<{
html:string,
headers:Headers
}> {
+ if (requestId) {
+ this.abortRequest(requestId);
+
+ const controller = new AbortController();
+ this.#controllers.set(requestId, controller);
+ init.signal = controller.signal;
+ }
+
const defaultHeaders = {
'X-Authentication-Scheme': 'Session',
};
@@ -48,13 +64,22 @@ export class TurboRequestsService {
}
})
.catch((error) => {
+ if (requestId && error instanceof DOMException && error.name === 'AbortError') {
+ debugLog(`Request "${requestId}" was aborted.`);
+
// this should only catch errors happening in the client side parsing in the above .then() calls
- if (!suppressErrorToast) {
+ } else if (!suppressErrorToast) {
this.toast.addError(error as string);
} else {
console.error(error);
}
+
throw error;
+ })
+ .finally(() => {
+ if (requestId) {
+ this.#controllers.delete(requestId);
+ }
});
}
@@ -62,6 +87,7 @@ export class TurboRequestsService {
form:HTMLFormElement,
params:URLSearchParams|null = null,
url = form.action,
+ requestId?:string,
):Promise<{ html:string, headers:Headers }> {
const formData = new FormData(form);
const requestParams = params ? `?${params.toString()}` : '';
@@ -75,16 +101,35 @@ export class TurboRequestsService {
},
},
true,
+ requestId,
);
}
- public requestStream(url:string):Promise<{ html:string, headers:Headers }> {
- return this.request(url, {
- method: 'GET',
- headers: {
- Accept: 'text/vnd.turbo-stream.html',
+ public requestStream(url:string, requestId?:string):Promise<{ html:string, headers:Headers }> {
+ return this.request(
+ url,
+ {
+ method: 'GET',
+ headers: {
+ Accept: 'text/vnd.turbo-stream.html',
+ },
+ credentials: 'same-origin',
},
- credentials: 'same-origin',
- });
+ false,
+ requestId,
+ );
+ }
+
+ public abortRequest(requestId:string):void {
+ const controller = this.#controllers.get(requestId);
+ if (controller) {
+ controller.abort();
+ this.#controllers.delete(requestId);
+ }
+ }
+
+ public abortAll():void {
+ this.#controllers.forEach((controller) => controller.abort());
+ this.#controllers.clear();
}
}