src/libs/ready.js   A
last analyzed

Complexity

Total Complexity 9
Complexity/F 1.8

Size

Lines of Code 61
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 25
mnd 4
bc 4
fnc 5
dl 0
loc 61
rs 10
bpm 0.8
cpm 1.8
noi 0
c 0
b 0
f 0
1
/**
2
 * Class: jaxon.dom
3
 *
4
 * global: jaxon
5
 */
6
7
/**
8
 * Plain javascript replacement for jQuery's .ready() function.
9
 * See https://github.com/jfriend00/docReady for a detailed description, copyright and license information.
10
 */
11
(function(self) {
12
    "use strict";
13
14
    let readyList = [];
15
    let readyFired = false;
16
    let readyEventHandlersInstalled = false;
17
18
    /**
19
     * Call this when the document is ready.
20
     * This function protects itself against being called more than once
21
     */
22
    const ready = () => {
23
        if (readyFired) {
24
            return;
25
        }
26
        // this must be set to true before we start calling callbacks
27
        readyFired = true;
28
        // if a callback here happens to add new ready handlers,
29
        // the jaxon.dom.ready() function will see that it already fired
30
        // and will schedule the callback to run right after
31
        // this event loop finishes so all handlers will still execute
32
        // in order and no new ones will be added to the readyList
33
        // while we are processing the list
34
        readyList.forEach(cb => cb.fn.call(window, cb.ctx));
35
        // allow any closures held by these functions to free
36
        readyList = [];
37
    }
38
39
    // Was used with the document.attachEvent() function.
40
    // const readyStateChange = () => document.readyState === "complete" && ready();
41
42
    /**
43
     * This is the one public interface
44
     * jaxon.dom.ready(fn, context);
45
     * The context argument is optional - if present, it will be passed as an argument to the callback
46
     */
47
    self.ready = function(callback, context) {
48
        // if ready has already fired, then just schedule the callback
49
        // to fire asynchronously, but right away
50
        if (readyFired) {
51
            setTimeout(function() { callback(context); }, 1);
52
            return;
53
        }
54
        // add the function and context to the list
55
        readyList.push({ fn: callback, ctx: context });
56
        // if document already ready to go, schedule the ready function to run
57
        if (document.readyState === "complete" ||
58
            (!document.attachEvent && document.readyState === "interactive")) {
59
            setTimeout(ready, 1);
60
            return;
61
        }
62
        if (!readyEventHandlersInstalled) {
63
            // first choice is DOMContentLoaded event
64
            document.addEventListener("DOMContentLoaded", ready, false);
65
            // backup is window load event
66
            window.addEventListener("load", ready, false);
67
68
            readyEventHandlersInstalled = true;
69
        }
70
    }
71
})(jaxon.dom);
72