-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndt7-core.js
37 lines (37 loc) · 1.08 KB
/
ndt7-core.js
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
/* jshint esversion: 6, asi: true */
// ndt7core is a simple ndt7 client API.
const ndt7core = (function() {
return {
// run runs the specified test with the specified base URL and calls
// callback to notify the caller of ndt7 events.
run: function(baseURL, testName, callback) {
callback('starting', {Origin: 'client', Test: testName})
let done = false
let worker = new Worker('ndt7-' + testName + '.js')
function finish() {
if (!done) {
done = true
if (callback !== undefined) {
callback('complete', {Origin: 'client', Test: testName})
}
}
}
worker.onmessage = function (ev) {
if (ev.data === null) {
finish()
return
}
callback('measurement', ev.data)
}
// Kill the worker after the timeout. This force the browser to
// close the WebSockets and prevent too-long tests.
setTimeout(function () {
worker.terminate()
finish()
}, 10000)
worker.postMessage({
href: baseURL,
})
}
}
}())