Passed
Push — main ( c0b881...db16a9 )
by Thierry
13:47 queued 11:55
created

src/libs/ready.js   A

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

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
70