<commit_before>﻿(function (root, factory)
{
    "use strict";

    // Universal Module Definition (UMD) to support AMD, CommonJS/Node.js,
    // Rhino, and plain browser loading.
    if (typeof define === "function")
    {
        define("shmuelie/offlineAjax", [], factory);
    }
    else
    {
        factory(true);
    }
}(this, function (bool)
{
    "use strict";

    var xhrOriginal = XMLHttpRequest;

    var OfflineXHR = function ()
    {
        /// <summary>
        ///     Creates an Offline XHR instance.
        /// </summary>
        /// <field name="response">
        ///     Returns the response received from the server.
        /// </field>
        /// <field name="responseBody">
        ///     Retrieves the response body as an array of unsigned bytes. 
        /// </field>
        /// <field name="responseText" type="String">
        ///     Retrieves the response body as a string.
        /// </field>
        /// <field name="responseType" type="String">
        ///     Describes the data type of the response associated with the request.
        /// </field>
        /// <field name="responseXML">
        ///     Retrieves the response body as an XML Document Object Model (DOM) object. 
        /// </field>

        var $this = this;

        this._eventDiv = document.createElement("div");

        this._core = new xhrOriginal();

        this._core.addEventListener("timeout", function (e)
        {
            if (typeof $this.ontimeout === "function")
            {
                $this.ontimeout(e);
            }
            $this._eventDiv.dispatchEvent(e);
        });
        this._core.addEventListener("onreadystatechange", function (e)
        {
            if ($this._core.readyState === xhrOriginal.DONE)
            {
                $this.response = $this._core.response;
                $this.responseBody = $this._core.responseBody;
                $this.responseText = $this._core.responseText;
                $this.responseType = $this._core.responseType;
                $this.responseXML = $this._core.responseXML;
            }
            $this._readyState = $this._core.readyState;
            if (typeof $this.onreadystatechange === "function")
            {
                $this.onreadystatechange(e);
            }
            $this._eventDiv.dispatchEvent(e);
            $this._send();
        });

        window.addEventListener("online", function ()
        {
            $this._send();
        });

        this.ontimeout = null;
        this.onreadystatechange = null;

        this._readyState = xhrOriginal.UNSENT;

        Object.defineProperties(this, {
            readyState:
                {
                    get: function ()
                    {
                        /// <value name="readyState" type="Number" integer="true">
                        ///     Retrieves the current state of the request operation.
                        /// </value>
                        return $this._readyState;
                    }
                },
            timeout:
                {
                    get: function ()
                    {
                        /// <value name="timeout" type="Number" integer="true">
                        ///     Gets or sets the time-out value. 
                        /// </value>
                        return $this._core.timeout;
                    },
                    set: function (value)
                    {
                        $this._core.timeout = value;
                    }
                }
        });

        this._requests = [];
    };

    OfflineXHR.prototype.addEventListener = function (type, listener, useCapture)
    {
        /// <summary>
        ///     Registers an event handler for the specified event type
        /// </summary>
        /// <param name="type" type="String">
        ///     The type of event type to register.
        /// </param>
        /// <param name="listener" type="Function">
        ///     The event handler function to associate with the event.
        /// </param>
        /// <param name="useCapture" type="Boolean" optional="true">
        ///     A Boolean value that specifies the event phase to add the event handler for. 
        /// </param>

        return this._eventDiv.addEventListener(type, listener, useCapture);
    };

    OfflineXHR.prototype.removeEventListener = function (type, listener, useCapture)
    {
        /// <summary>
        ///     Removes an event handler that the addEventListener method registered.
        /// </summary>
        /// <param name="type" type="String">
        ///     The event type that the event handler is registered for.
        /// </param>
        /// <param name="listener" type="Function">
        ///     The event handler function to remove.
        /// </param>
        /// <param name="useCapture" type="Boolean" optional="true">
        ///     A Boolean value that specifies the event phase to remove the event handler from.
        /// </param>

        return this._eventDiv.removeEventListener(type, listener, useCapture);
    };

    OfflineXHR.prototype._send = function ()
    {
        if ((this._requests.length > 0) && navigator.onLine)
        {
            var request = this._requests.shift();

            if (request.sent === false)
            {
                this._send();
                return;
            }

            this._core.open(request.method, request.url, true, request.user, request.password);
            for (var headerName in request.headers)
            {
                this._core.setRequestHeader(headerName, request.headers[headerName]);
            }<commit_msg>Fixed bug where timeout was sent at the wrong time, causing errors. Now it is set on _core in _send, while the property is now just a field<commit_after>﻿(function (root, factory)
{
    "use strict";

    // Universal Module Definition (UMD) to support AMD, CommonJS/Node.js,
    // Rhino, and plain browser loading.
    if (typeof define === "function")
    {
        define("shmuelie/offlineAjax", [], factory);
    }
    else
    {
        factory(true);
    }
}(this, function (bool)
{
    "use strict";

    var xhrOriginal = XMLHttpRequest;

    var OfflineXHR = function ()
    {
        /// <summary>
        ///     Creates an Offline XHR instance.
        /// </summary>
        /// <field name="response">
        ///     Returns the response received from the server.
        /// </field>
        /// <field name="responseBody">
        ///     Retrieves the response body as an array of unsigned bytes. 
        /// </field>
        /// <field name="responseText" type="String">
        ///     Retrieves the response body as a string.
        /// </field>
        /// <field name="responseType" type="String">
        ///     Describes the data type of the response associated with the request.
        /// </field>
        /// <field name="responseXML">
        ///     Retrieves the response body as an XML Document Object Model (DOM) object. 
        /// </field>

        var $this = this;

        this._eventDiv = document.createElement("div");

        this._core = new xhrOriginal();

        this._core.addEventListener("timeout", function (e)
        {
            if (typeof $this.ontimeout === "function")
            {
                $this.ontimeout(e);
            }
            $this._eventDiv.dispatchEvent(e);
        });
        this._core.addEventListener("onreadystatechange", function (e)
        {
            if ($this._core.readyState === xhrOriginal.DONE)
            {
                $this.response = $this._core.response;
                $this.responseBody = $this._core.responseBody;
                $this.responseText = $this._core.responseText;
                $this.responseType = $this._core.responseType;
                $this.responseXML = $this._core.responseXML;
            }
            $this._readyState = $this._core.readyState;
            if (typeof $this.onreadystatechange === "function")
            {
                $this.onreadystatechange(e);
            }
            $this._eventDiv.dispatchEvent(e);
            $this._send();
        });

        window.addEventListener("online", function ()
        {
            $this._send();
        });

        this.ontimeout = null;
        this.onreadystatechange = null;

        this._readyState = xhrOriginal.UNSENT;

        this.timeout = 0;

        Object.defineProperty(this, "readState", {
            get: function ()
            {
                /// <value name="readyState" type="Number" integer="true">
                ///     Retrieves the current state of the request operation.
                /// </value>
                return $this._readyState;
            }
        });

        this._requests = [];
    };

    OfflineXHR.prototype.addEventListener = function (type, listener, useCapture)
    {
        /// <summary>
        ///     Registers an event handler for the specified event type
        /// </summary>
        /// <param name="type" type="String">
        ///     The type of event type to register.
        /// </param>
        /// <param name="listener" type="Function">
        ///     The event handler function to associate with the event.
        /// </param>
        /// <param name="useCapture" type="Boolean" optional="true">
        ///     A Boolean value that specifies the event phase to add the event handler for. 
        /// </param>

        return this._eventDiv.addEventListener(type, listener, useCapture);
    };

    OfflineXHR.prototype.removeEventListener = function (type, listener, useCapture)
    {
        /// <summary>
        ///     Removes an event handler that the addEventListener method registered.
        /// </summary>
        /// <param name="type" type="String">
        ///     The event type that the event handler is registered for.
        /// </param>
        /// <param name="listener" type="Function">
        ///     The event handler function to remove.
        /// </param>
        /// <param name="useCapture" type="Boolean" optional="true">
        ///     A Boolean value that specifies the event phase to remove the event handler from.
        /// </param>

        return this._eventDiv.removeEventListener(type, listener, useCapture);
    };

    OfflineXHR.prototype._send = function ()
    {
        if ((this._requests.length > 0) && navigator.onLine)
        {
            var request = this._requests.shift();

            if (request.sent === false)
            {
                this._send();
                return;
            }

            this._core.open(request.method, request.url, true, request.user, request.password);
            for (var headerName in request.headers)
            {
                this._core.setRequestHeader(headerName, request.headers[headerName]);
            }
            this._core.timeout = this.timeout;
{'fraction_non_alphanumeric': 0.12480527810867773, 'fraction_numerical': 0.00027490149363144874, 'mean_word_length': 1.5269738365362353, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2069327', 'n_tokens_mistral': 2800, 'n_tokens_neox': 2694, 'n_words': 714}