/**
* 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;
});
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);
// 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());
}
}
--- /dev/null
+/**
+ * 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();
+}