summaryrefslogtreecommitdiff
path: root/common/content/modules.js
blob: 7e1a0b1b15775d758ba5b75fe9b7b0f2a6e25360 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

const ModuleBase = Class("ModuleBase", { requires: [] });
function Module(name, inst, clas, moduleInit) {
    const module = Class(name, ModuleBase, inst, clas);
    module.INIT = moduleInit || {};
    module.requires = inst.requires || [];
    Module.list.push(module);
    Module.constructors[name] = module;
    return module;
}
Module.list = [];
Module.constructors = {};

window.addEventListener("load", function () {
    function dump(str) window.dump(String.replace(str, /\n?$/, "\n").replace(/^/m, config.name.toLowerCase() + ": "));
    const start = Date.now();
    const deferredInit = { load: [] };
    const seen = set();

    function load(module, prereq) {
        try {
            if (module.name in modules)
                return;
            if (module.name in seen)
                throw Error("Module dependency loop.");
            set.add(seen, module.name);

            for (let dep in values(module.requires))
                load(Module.constructors[dep], module.name);

            dump("Load" + (isstring(prereq) ? " " + prereq + " dependency: " : ": ") + module.name);
            modules[module.name] = module();

            function init(mod, module)
                function () module.INIT[mod].call(modules[module.name], modules[mod]);
            for (let [mod, ] in iter(module.INIT))
                try {
                    if (mod in modules)
                        init(mod, module)();
                    else {
                        deferredInit[mod] = deferredInit[mod] || [];
                        deferredInit[mod].push(init(mod, module));
                    }
                }
                catch(e) {
                    if (modules.liberator)
                        liberator.reportError(e);
                }
            for (let [, fn] in iter(deferredInit[module.name] || []))
                fn();
        }
        catch (e) {
            dump("Loading " + (module && module.name) + ": " + e);
            if (e.stack)
                dump(e.stack);
        }
    }
    Module.list.forEach(load);
    deferredInit['load'].forEach(call)

    for (let module in values(Module.list))
        delete module.INIT;

    dump("Loaded in " + (Date.now() - start) + "ms\n");
}, false);

window.addEventListener("unload", function () {
    for (let [, mod] in iter(modules))
        if (mod instanceof ModuleBase && 'destroy' in mod)
            mod.destroy();
}, false);

// vim: set fdm=marker sw=4 ts=4 et: