aboutsummaryrefslogtreecommitdiff
path: root/ext/fiddle/testing-common.js
blob: 2fe9a860ba97d2536f58fe1c3489773e4e2f27cd (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
  2022-05-22

  The author disclaims copyright to this source code.  In place of a
  legal notice, here is a blessing:

  *   May you do good and not evil.
  *   May you find forgiveness for yourself and forgive others.
  *   May you share freely, never taking more than you give.

  ***********************************************************************

  This file contains bootstrapping code used by various test scripts
  which live in this file's directory.
*/
(function(){
    /* querySelectorAll() proxy */
    const EAll = function(/*[element=document,] cssSelector*/){
        return (arguments.length>1 ? arguments[0] : document)
            .querySelectorAll(arguments[arguments.length-1]);
    };
    /* querySelector() proxy */
    const E = function(/*[element=document,] cssSelector*/){
        return (arguments.length>1 ? arguments[0] : document)
            .querySelector(arguments[arguments.length-1]);
    };
    
    const statusElement = E('#module-status');
    const progressElement = E('#module-progress');
    const spinnerElement = E('#module-spinner');

    self.Module = {
        /* ^^^ cannot declare that const because fiddle-module.js
           (auto-generated) includes a decl for it and runs in this scope. */
        preRun: [],
        postRun: [],
        //onRuntimeInitialized: function(){},
        print: function(){
            console.log(Array.prototype.slice.call(arguments));
        },
        printErr: function(){
            console.error(Array.prototype.slice.call(arguments));
        },
        setStatus: function f(text){
            if(!f.last) f.last = { time: Date.now(), text: '' };
            if(text === f.last.text) return;
            const m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
            const now = Date.now();
            if(m && now - f.last.time < 30) return; // if this is a progress update, skip it if too soon
            f.last.time = now;
            f.last.text = text;
            if(m) {
                text = m[1];
                progressElement.value = parseInt(m[2])*100;
                progressElement.max = parseInt(m[4])*100;
                progressElement.hidden = false;
                spinnerElement.hidden = false;
            } else {
                progressElement.remove();
                if(!text) spinnerElement.remove();
            }
            if(text) statusElement.innerText = text;
            else statusElement.remove();
        },
        totalDependencies: 0,
        monitorRunDependencies: function(left) {
            this.totalDependencies = Math.max(this.totalDependencies, left);
            this.setStatus(left
                           ? ('Preparing... (' + (this.totalDependencies-left)
                              + '/' + this.totalDependencies + ')')
                           : 'All downloads complete.');
        },
        /* Loads sqlite3-api.js and calls the given callback (if
           provided), passing it the sqlite3 module. Whether this is
           synchronous or async depends on whether it's run in the
           main thread or a worker.*/
        loadSqliteAPI: function(callback){
            const theScript = 'sqlite3-api.js';
            if(self.importScripts){/*worker*/
                importScripts(theScript);
                if(callback) callback(self.sqlite3);
            }else{/*main thread*/
                new Promise((resolve, reject) => {
                    const script = document.createElement('script');
                    document.body.appendChild(script);
                    script.onload = resolve;
                    script.onerror = reject;
                    script.async = true;
                    script.src = theScript;
                }).then(() => {
                    if(callback) callback(self.sqlite3);
                });
            }
        }
    };
})(self/*window or worker*/);