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

Add AbortSignal Support to XHRLoader #6971

Open
vforsh opened this issue Nov 30, 2024 · 0 comments
Open

Add AbortSignal Support to XHRLoader #6971

vforsh opened this issue Nov 30, 2024 · 0 comments

Comments

@vforsh
Copy link
Contributor

vforsh commented Nov 30, 2024

Hey Phaser team,

I would like to propose a pull request that adds support for AbortSignal to the XHRLoader. This enhancement will allow users to cancel ongoing XHR requests more effectively, improving resource management and user experience in scenarios where requests may need to be aborted.

Would you be open to reviewing PR like this? I believe it could be a valuable addition to the project.

Here is the diff:

diff --git a/node_modules/phaser/src/loader/File.js b/node_modules/phaser/src/loader/File.js
index 2538cdf..2612a17 100644
--- a/node_modules/phaser/src/loader/File.js
+++ b/node_modules/phaser/src/loader/File.js
@@ -407,6 +407,22 @@ var File = new Class({
         }
     },
 
+    /**
+     * Called if the file loading was aborted, is sent a DOM ProgressEvent.
+     *
+     * @method Phaser.Loader.File#onAbort
+     * @since 3.88.0
+     *
+     * @param {XMLHttpRequest} xhr - The XMLHttpRequest that caused this abort event.
+     * @param {ProgressEvent} event - The DOM ProgressEvent that resulted from this error.
+     */
+    onAbort: function ()
+    {
+        this.resetXHR();
+        this.state = CONST.FILE_LOAD_ABORTED;
+        this.loader.nextFile(this, false);
+    },
+
     /**
      * Called during the file load progress. Is sent a DOM ProgressEvent.
      *
diff --git a/node_modules/phaser/src/loader/XHRLoader.js b/node_modules/phaser/src/loader/XHRLoader.js
index b71bcb5..64d5900 100644
--- a/node_modules/phaser/src/loader/XHRLoader.js
+++ b/node_modules/phaser/src/loader/XHRLoader.js
@@ -75,12 +75,31 @@ var XHRLoader = function (file, globalXHRSettings)
 
     xhr.onload = file.onLoad.bind(file, xhr);
     xhr.onerror = file.onError.bind(file, xhr);
+    xhr.onabort = file.onAbort.bind(file, xhr);
     xhr.onprogress = file.onProgress.bind(file);
     xhr.ontimeout = file.onError.bind(file, xhr);
 
     //  This is the only standard method, the ones above are browser additions (maybe not universal?)
     // xhr.onreadystatechange
 
+    // Listening for the abort signal
+    if (config.signal) {
+        if (config.signal.aborted) {
+            xhr.abort();
+            return xhr;
+        }
+
+        var listener = function() {
+            xhr.abort();
+        };
+
+        xhr.onloadend = function() {
+            config.signal.removeEventListener('abort', listener);
+        }
+
+        config.signal.addEventListener('abort', listener);
+    }
+
     xhr.send();
 
     return xhr;
diff --git a/node_modules/phaser/src/loader/const.js b/node_modules/phaser/src/loader/const.js
index c3fc6f5..10b57de 100644
--- a/node_modules/phaser/src/loader/const.js
+++ b/node_modules/phaser/src/loader/const.js
@@ -148,8 +148,16 @@ var FILE_CONST = {
      * @type {number}
      * @since 3.60.0
      */
-    FILE_PENDING_DESTROY: 20
+    FILE_PENDING_DESTROY: 20,
 
+    /**
+     * File loading was aborted.
+     *
+     * @name Phaser.Loader.FILE_LOAD_ABORTED
+     * @type {number}
+     * @since 3.88.0
+     */
+    FILE_LOAD_ABORTED: 21
 };
 
 module.exports = FILE_CONST;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant