Skip to content

Commit

Permalink
Reliably cancel an in-flight request
Browse files Browse the repository at this point in the history
The `_previousXhr` property was not reliably set previously and the current in-flight was not visible unless a new request was dispatched.

The updated control flow in ca409b3 effectively allowed for the `_previousXhr` property to be removed entirely. Working with `_xhr` directly allows to abort an in-flight request without dispatching a new request. This also fixes a third bug hidden in `_finalize()` that could have erased a later in-flight request in some cases.

See https://www.woltlab.com/community/thread/305578-halb-fehler-halb-wunsch/
  • Loading branch information
dtdesign committed Apr 21, 2024
1 parent f34efea commit 5f09a02
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
25 changes: 10 additions & 15 deletions ts/WoltLabSuite/Core/Ajax/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ let _ignoreAllErrors = false;
class AjaxRequest {
private readonly _options: RequestOptions;
private readonly _data: RequestData;
private _previousXhr?: XMLHttpRequest;
private _xhr?: XMLHttpRequest;

constructor(options: RequestOptions) {
Expand Down Expand Up @@ -108,10 +107,6 @@ class AjaxRequest {
* Dispatches a request, optionally aborting a currently active request.
*/
sendRequest(abortPrevious?: boolean): void {
if (this._xhr instanceof XMLHttpRequest) {
this._previousXhr = this._xhr;
}

if (abortPrevious || this._options.autoAbort) {
this.abortPrevious();
}
Expand Down Expand Up @@ -183,12 +178,12 @@ class AjaxRequest {
* Aborts a previous request.
*/
abortPrevious(): void {
if (!this._previousXhr) {
if (this._xhr === undefined) {
return;
}

this._previousXhr.abort();
this._previousXhr = undefined;
this._xhr.abort();
this._xhr = undefined;

if (!this._options.silent) {
AjaxStatus.hide();
Expand Down Expand Up @@ -259,7 +254,7 @@ class AjaxRequest {
options.success(data || {}, xhr.responseText, xhr, options.data!);
}

this._finalize(options);
this._finalize(xhr, options);
}

/**
Expand Down Expand Up @@ -301,7 +296,7 @@ class AjaxRequest {
}
}

this._finalize(options);
this._finalize(xhr, options);
}

/**
Expand Down Expand Up @@ -349,15 +344,15 @@ class AjaxRequest {

/**
* Finalizes a request.
*
* @param {Object} options request options
*/
_finalize(options: RequestOptions): void {
_finalize(xhr: XMLHttpRequest, options: RequestOptions): void {
if (typeof options.finalize === "function") {
options.finalize(this._xhr!);
options.finalize(xhr);
}

this._previousXhr = undefined;
if (this._xhr === xhr) {
this._xhr = undefined;
}

DomChangeListener.trigger();

Expand Down
23 changes: 10 additions & 13 deletions wcfsetup/install/files/js/WoltLabSuite/Core/Ajax/Request.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5f09a02

Please sign in to comment.