src/http/xhr.js

Maintainability

80.78

Lines of code

44

Created with Raphaël 2.1.002550751002019-3-122018-12-32018-8-22018-5-92018-4-32018-2-282017-12-212017-11-12017-6-7

2019-10-30
Maintainability: 80.78

Created with Raphaël 2.1.0012.52537.5502019-3-122018-12-32018-8-22018-5-92018-4-32018-2-282017-12-212017-11-12017-6-7

2019-10-30
Lines of Code: 44

Difficulty

8.46

Estimated Errors

0.18

Function weight

By Complexity

Created with Raphaël 2.1.0<anonymous>2

By SLOC

Created with Raphaël 2.1.0_gpfHttpXhrWaitForCompletion7
1
/**
2
 * @file Browser specific HTTP implementation
3
 * @since 0.2.1
4
 */
5
/*#ifndef(UMD)*/
6
"use strict";
7
/*global _GPF_HOST*/ // Host types
8
/*global _gpfHttpGenGetResponse*/ // Generates a function that extracts response from the http object
9
/*global _gpfHttpGenSend*/ // Generates a function that implements the http send logic
10
/*global _gpfHttpGenSetHeaders*/ // Generates a function that transmit headers to the http object
11
/*global _gpfHttpSetRequestImplIf*/ // Set the http request implementation if the host matches
12
/*#endif*/
13
 
14
/*jshint browser: true*/
15
/*eslint-env browser*/
16
 
17
var _GPF_HTTP_XHR_READYSTATE_DONE = 4,
18
    _gpfHttpXhrSetHeaders = _gpfHttpGenSetHeaders("setRequestHeader"),
19
    _gpfHttpXhrSend = _gpfHttpGenSend("send"),
20
    _gpfHttpXhrGetResponse = _gpfHttpGenGetResponse("status", "getAllResponseHeaders", "responseText");
21
 
22
function _gpfHttpXhrOpen (request) {
23
    var xhr = new XMLHttpRequest();
24
    xhr.open(request.method, request.url);
25
    return xhr;
26
}
27
 
28
function _gpfHttpXhrWaitForCompletion (xhr, resolve) {
29
    xhr.onreadystatechange = function () {
30
        if (xhr.readyState === _GPF_HTTP_XHR_READYSTATE_DONE) {
31
            resolve(_gpfHttpXhrGetResponse(xhr));
32
        }
33
    };
34
}
35
 
36
function _gpfHttpXhrRequest (request, resolve) {
37
    var xhr = _gpfHttpXhrOpen(request);
38
    _gpfHttpXhrSetHeaders(xhr, request.headers);
39
    _gpfHttpXhrWaitForCompletion(xhr, resolve);
40
    _gpfHttpXhrSend(xhr, request.data);
41
}
42
 
43
_gpfHttpSetRequestImplIf(_GPF_HOST.BROWSER, _gpfHttpXhrRequest);
44
_gpfHttpSetRequestImplIf(_GPF_HOST.PHANTOMJS, _gpfHttpXhrRequest);