if (data && data.returnValues && data.returnValues.template !== undefined) {
data.returnValues.template = data.returnValues.template.trim();
}
+
+ // force-invoke the background queue
+ if (data && data.forceBackgroundQueuePerform) {
+ require(['WoltLabSuite/Core/BackgroundQueue'], function(BackgroundQueue) {
+ BackgroundQueue.invoke();
+ });
+ }
}
options.success(data, xhr.responseText, xhr, options.data);
--- /dev/null
+/**
+ * Manages the invocation of the background queue.
+ *
+ * @author Alexander Ebert
+ * @copyright 2001-2018 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;
+
+ _invocations = 0;
+ _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(this.invoke.bind(this), 1000);
+ }
+ else {
+ _isBusy = false;
+ }
+ },
+
+ _ajaxSetup: function () {
+ return {
+ url: _url,
+ ignoreError: true,
+ silent: true
+ }
+ }
+ }
+});
\ No newline at end of file
* Bootstraps WCF's JavaScript with additions for the frontend usage.
*
* @author Alexander Ebert
- * @copyright 2001-2017 WoltLab GmbH
+ * @copyright 2001-2018 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @module WoltLabSuite/Core/BootstrapFrontend
*/
define(
[
- 'Ajax', 'WoltLabSuite/Core/Bootstrap', 'WoltLabSuite/Core/Controller/Style/Changer',
+ 'WoltLabSuite/Core/BackgroundQueue', 'WoltLabSuite/Core/Bootstrap', 'WoltLabSuite/Core/Controller/Style/Changer',
'WoltLabSuite/Core/Controller/Popover', 'WoltLabSuite/Core/Ui/User/Ignore', 'WoltLabSuite/Core/Ui/Page/Header/Menu'
],
function(
- Ajax, Bootstrap, ControllerStyleChanger,
- ControllerPopover, UiUserIgnore, UiPageHeaderMenu
+ BackgroundQueue, Bootstrap, ControllerStyleChanger,
+ ControllerPopover, UiUserIgnore, UiPageHeaderMenu
)
{
"use strict";
- var queueInvocations = 0;
-
/**
* @exports WoltLabSuite/Core/BootstrapFrontend
*/
}
this._initUserPopover();
- this._invokeBackgroundQueue(options.backgroundQueue.url, options.backgroundQueue.force);
+
+ BackgroundQueue.setUrl(options.backgroundQueue.url);
+ if (Math.random() < 0.1 || options.backgroundQueue.force) {
+ // invoke the queue roughly every 10th request or on demand
+ BackgroundQueue.invoke();
+ }
UiUserIgnore.init();
},
}, callback, callback);
}
});
- },
-
- /**
- * Invokes the background queue roughly every 10th request.
- *
- * @param {string} url background queue url
- * @param {boolean} force whether execution should be forced
- */
- _invokeBackgroundQueue: function(url, force) {
- var again = this._invokeBackgroundQueue.bind(this, url, true);
-
- if (Math.random() < 0.1 || force) {
- // 'fire and forget' background queue perform task
- Ajax.apiOnce({
- url: url,
- ignoreError: true,
- silent: true,
- success: (function(data) {
- queueInvocations++;
-
- // process up to 5 queue items per page load
- if (data > 0 && queueInvocations < 5) setTimeout(again, 1000);
- }).bind(this)
- });
- }
}
};
});