JSFiddle - React, Tailwind, and code Playground

by Glutamat

JavaScript

/*jshint boss:true*/
var util, XHRManager;

//A factory function for utility methods.
function utilIIF() {
    return {
        formatString: function () {
            var args,
                str,
                i = 0;

            args = Array.prototype.slice.call(arguments);
            str = args.shift();
            str = str.replace(/%s/g, function () {
                return args[i++];
            });

            return str;
        }
    };
}
//Factory function for the XHR Manager.
function XHRManagerIIF() {
    var NUM_XHR = 5; //Number of parallel requests, possible
    var TIMEOUT = 30000; //The Default Timeout
    var MAX_RETRIES = 3; //The number of retries after an timeout

    var instance, free, used, buffer, xhr;

    buffer = (function () {
        var buffer = [];

        return {
            add: function (request) {
                return buffer.push(request);
            },
            get: function () {
                return buffer.shift();
            },
            has: function () {
                return !!buffer.length;
            }
        };
    })();

    xhr = {
        get: function () {
            if (free.length) {
                return used[used.push(free.shift()) - 1];
            }
        },
        release: function (xhr) {
            var index = used.indexOf(xhr);
            if ( !! ~index) {
                used.splice(index, 1);
                free.push(createXHRObject());
            }
        },
        handleNext: function (xhr) {
	    if (xhr) {
                this.release(xhr); //Release the current xhr object
            }

            if (buffer.has()) { //If there are any buffered requests left, assign them to a free xhr object.
                Request.assign(this.get(), buffer.get());
            }
        },
        retry: function (xhr) {
            if (xhr.request.retries < MAX_RETRIES) {
                buffer.add(xhr.request);
                xhr.request.retries++;
            }
           ...