Convert `BackgroundQueue` to TypeScript
authorAlexander Ebert <ebert@woltlab.com>
Fri, 16 Oct 2020 23:52:17 +0000 (01:52 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Wed, 28 Oct 2020 11:30:22 +0000 (12:30 +0100)
wcfsetup/install/files/js/WoltLabSuite/Core/Ajax/Request.js
wcfsetup/install/files/js/WoltLabSuite/Core/BackgroundQueue.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Ajax.ts
wcfsetup/install/files/ts/WoltLabSuite/Core/Ajax/Request.ts
wcfsetup/install/files/ts/WoltLabSuite/Core/BackgroundQueue.ts [new file with mode: 0644]

index 91159492e861279edacc295a703e0a9d2b6c4443..d96eda3b1d44f7e19663d6d80d2b73dabd50a86d 100644 (file)
@@ -219,13 +219,7 @@ define(["require", "exports", "./Status", "../Core", "../Dom/Change/Listener"],
                     }
                     // force-invoke the background queue
                     if (data && data.forceBackgroundQueuePerform) {
-                        // TODO
-                        throw new Error('TODO: Invoking the BackgroundQueue is not yet supported.');
-                        /*
-                        require(['WoltLabSuite/Core/BackgroundQueue'], function (BackgroundQueue) {
-                          BackgroundQueue.invoke();
-                        });
-                         */
+                        new Promise((resolve_1, reject_1) => { require(['../BackgroundQueue'], resolve_1, reject_1); }).then(__importStar).then(backgroundQueue => backgroundQueue.invoke());
                     }
                 }
                 options.success(data, xhr.responseText, xhr, options.data);
index 0e12644768a33d6c23a75e3d2e8e4b8c19af0b37..24b103e622cc6dac4d77004e53eaa213869beb95 100644 (file)
@@ -1,69 +1,88 @@
 /**
  * Manages the invocation of the background queue.
- * 
- * @author     Alexander Ebert
- * @copyright  2001-2019 WoltLab GmbH
- * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- * @module     WoltLabSuite/Core/BackgroundQueue
+ *
+ * @author  Alexander Ebert
+ * @copyright  2001-2019 WoltLab GmbH
+ * @license  GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module  WoltLabSuite/Core/BackgroundQueue
  */
-define(['Ajax'], function(Ajax) {
-       "use strict";
-       
-       var _invocations = 0;
-       var _isBusy = false;
-       var _url = '';
-       
-       /**
-        * @exports     WoltLabSuite/Core/BackgroundQueue
-        */
-       return {
-               /**
-                * Sets the url of the background queue perform action.
-                * 
-                * @param       {string}        url     background queue perform url
-                */
-               setUrl: function (url) {
-                       _url = url;
-               },
-               
-               /**
-                * Invokes the background queue.
-                */
-               invoke: function () {
-                       if (_url === '') {
-                               console.error('The background queue has not been initialized yet.');
-                               return;
-                       }
-                       
-                       if (_isBusy) return;
-                       
-                       _isBusy = true;
-                       
-                       Ajax.api(this);
-               },
-               
-               _ajaxSuccess: function (data) {
-                       _invocations++;
-                       
-                       // invoke the queue up to 5 times in a row
-                       if (data > 0 && _invocations < 5) {
-                               window.setTimeout(function () {
-                                       _isBusy = false;
-                                       this.invoke();
-                               }.bind(this), 1000);
-                       }
-                       else {
-                               _isBusy = false;
-                               _invocations = 0;
-                       }
-               },
-               
-               _ajaxSetup: function () {
-                       return {
-                               url: _url,
-                               ignoreError: true,
-                               silent: true
-                       }
-               }
-       }
+var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
+    if (k2 === undefined) k2 = k;
+    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
+}) : (function(o, m, k, k2) {
+    if (k2 === undefined) k2 = k;
+    o[k2] = m[k];
+}));
+var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
+    Object.defineProperty(o, "default", { enumerable: true, value: v });
+}) : function(o, v) {
+    o["default"] = v;
+});
+var __importStar = (this && this.__importStar) || function (mod) {
+    if (mod && mod.__esModule) return mod;
+    var result = {};
+    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
+    __setModuleDefault(result, mod);
+    return result;
+};
+define(["require", "exports", "./Ajax"], function (require, exports, Ajax) {
+    "use strict";
+    Object.defineProperty(exports, "__esModule", { value: true });
+    exports.invoke = exports.setUrl = void 0;
+    Ajax = __importStar(Ajax);
+    class BackgroundQueue {
+        constructor(url) {
+            this._invocations = 0;
+            this._isBusy = false;
+            this._url = url;
+        }
+        invoke() {
+            if (this._isBusy)
+                return;
+            this._isBusy = true;
+            Ajax.api(this);
+        }
+        _ajaxSuccess(data) {
+            this._invocations++;
+            // invoke the queue up to 5 times in a row
+            if (data > 0 && this._invocations < 5) {
+                window.setTimeout(() => {
+                    this._isBusy = false;
+                    this.invoke();
+                }, 1000);
+            }
+            else {
+                this._isBusy = false;
+                this._invocations = 0;
+            }
+        }
+        _ajaxSetup() {
+            return {
+                url: this._url,
+                ignoreError: true,
+                silent: true,
+            };
+        }
+    }
+    let queue;
+    /**
+     * Sets the url of the background queue perform action.
+     */
+    function setUrl(url) {
+        if (!queue) {
+            queue = new BackgroundQueue(url);
+        }
+    }
+    exports.setUrl = setUrl;
+    /**
+     * Invokes the background queue.
+     */
+    function invoke() {
+        if (!queue) {
+            console.error('The background queue has not been initialized yet.');
+            return;
+        }
+        queue.invoke();
+    }
+    exports.invoke = invoke;
 });
index 55f0f7b09ab53c8e52689e95bc62a70bc4d6b199..18630a7fc1ead1111ffeef69b584fd99ef24ae6d 100644 (file)
@@ -17,7 +17,7 @@ const _cache = new WeakMap();
  * Shorthand function to perform a request against the WCF-API with overrides
  * for success and failure callbacks.
  */
-export function api(callbackObject: CallbackObject, data: RequestData, success: CallbackSuccess, failure: CallbackFailure): AjaxRequest {
+export function api(callbackObject: CallbackObject, data?: RequestData, success?: CallbackSuccess, failure?: CallbackFailure): AjaxRequest {
   if (typeof data !== 'object') data = {};
 
   let request = _cache.get(callbackObject);
index 1908550872fd1b67336d28fae1734c91a71c4456..374a5954bb2a6450238f8309301cdc3d7d1a6f81 100644 (file)
@@ -211,7 +211,7 @@ class AjaxRequest {
       let data: ResponseData | null = null;
       if (xhr.getResponseHeader('Content-Type')!.split(';', 1)[0].trim() === 'application/json') {
         try {
-          data = JSON.parse(xhr.responseText);
+          data = JSON.parse(xhr.responseText) as ResponseData;
         } catch (e) {
           // invalid JSON
           this._failure(xhr, options);
@@ -226,13 +226,7 @@ class AjaxRequest {
 
         // force-invoke the background queue
         if (data && data.forceBackgroundQueuePerform) {
-          // TODO
-          throw new Error('TODO: Invoking the BackgroundQueue is not yet supported.');
-          /*
-          require(['WoltLabSuite/Core/BackgroundQueue'], function (BackgroundQueue) {
-            BackgroundQueue.invoke();
-          });
-           */
+          import('../BackgroundQueue').then(backgroundQueue => backgroundQueue.invoke());
         }
       }
 
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/BackgroundQueue.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/BackgroundQueue.ts
new file mode 100644 (file)
index 0000000..662cd0c
--- /dev/null
@@ -0,0 +1,75 @@
+/**
+ * Manages the invocation of the background queue.
+ *
+ * @author  Alexander Ebert
+ * @copyright  2001-2019 WoltLab GmbH
+ * @license  GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module  WoltLabSuite/Core/BackgroundQueue
+ */
+
+import * as Ajax from './Ajax';
+import { CallbackObject, RequestOptions, ResponseData } from './Ajax/Data';
+
+class BackgroundQueue implements CallbackObject {
+  private _invocations = 0;
+  private _isBusy = false;
+  private readonly _url: string;
+
+  constructor(url: string) {
+    this._url = url;
+  }
+
+  invoke(): void {
+    if (this._isBusy) return;
+
+    this._isBusy = true;
+
+    Ajax.api(this);
+  }
+
+  _ajaxSuccess(data: ResponseData): void {
+    this._invocations++;
+
+    // invoke the queue up to 5 times in a row
+    if ((data as unknown) as number > 0 && this._invocations < 5) {
+      window.setTimeout(() => {
+        this._isBusy = false;
+        this.invoke();
+      }, 1000);
+    } else {
+      this._isBusy = false;
+      this._invocations = 0;
+    }
+  }
+
+  _ajaxSetup(): RequestOptions {
+    return {
+      url: this._url,
+      ignoreError: true,
+      silent: true,
+    };
+  }
+}
+
+let queue: BackgroundQueue;
+
+/**
+ * Sets the url of the background queue perform action.
+ */
+export function setUrl(url: string): void {
+  if (!queue) {
+    queue = new BackgroundQueue(url);
+  }
+}
+
+/**
+ * Invokes the background queue.
+ */
+export function invoke(): void {
+  if (!queue) {
+    console.error('The background queue has not been initialized yet.');
+    return;
+  }
+
+  queue.invoke();
+}