-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
135 lines (120 loc) · 3.79 KB
/
index.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
document.addEventListener("DOMContentLoaded", function () {
'use strict';
var tester = createTester();
var buttons = document.getElementsByTagName('button');
var display = document.getElementById('display');
var worker = new Worker('worker.js');
function disableButtons(bool) {
for (var i = 0; i < buttons.length; i++) {
buttons[i].disabled = bool;
}
}
function getChoice(name) {
var choices = document.getElementsByName(name);
for (var i = 0; i < choices.length; i++) {
var choice = choices[i];
if (choice.checked) {
var label = document.querySelector('label[for=' + choice.id + ']').innerHTML;
return {value: choice.value, label: label};
}
}
}
function waitForUI() {
return new Promise(function (resolve) {
display.getBoundingClientRect();
requestAnimationFrame(function () {
requestAnimationFrame(resolve);
});
});
}
function workerPromise(message) {
return new Promise(function (resolve, reject) {
function cleanup() {
worker.removeEventListener('message', onSuccess);
worker.removeEventListener('error', onError);
}
function onSuccess(e) {
cleanup();
if (e.data.error) {
reject(e.data.error);
} else {
resolve(e);
}
}
function onError(e) {
cleanup();
reject(e);
}
worker.addEventListener('message', onSuccess);
worker.addEventListener('error', onError);
worker.postMessage(message);
});
}
document.getElementById('insertButton').addEventListener('click', function () {
disableButtons(true);
var dbTypeChoice = getChoice('db');
var numDocsChoice = getChoice('numDocs');
var numDocs = parseInt(numDocsChoice.value, 10);
var useWorker = getChoice('worker').value === 'true';
var cloneWorker = getChoice('worker').value === 'clone';
display.innerHTML = 'Inserting ' + numDocs + ' docs using ' +
dbTypeChoice.label + (useWorker ? ' in a worker' : '') + '...';
waitForUI().then(function () {
var startTime = Date.now();
if (useWorker) {
return workerPromise({
action: 'test',
dbType: dbTypeChoice.value,
numDocs: numDocs
}).then(function (e) {
if (!e.data.success) {
throw new Error('did not work');
}
return Date.now() - startTime;
});
} else if (cloneWorker) {
return workerPromise({
action: 'test',
dbType: dbTypeChoice.value,
numDocs: tester.createDocs(numDocs)
}).then(function (e) {
if (!e.data.success) {
throw new Error('did not work');
}
return Date.now() - startTime;
});
}
var fun = tester.getTest(dbTypeChoice.value);
return Promise.resolve().then(function () {
return fun(numDocs);
}).then(function () {
return Date.now() - startTime;
});
}).then(function (timeSpent) {
display.innerHTML += "\nTook " + timeSpent + "ms";
disableButtons(false);
}).catch(function (e) {
disableButtons(false);
display.innerHTML += "\nError: " + e;
console.error(e);
});
});
document.getElementById('deleteButton').addEventListener('click', function () {
display.innerHTML = 'Deleting...';
disableButtons(true);
var useWorker = getChoice('worker').value === 'true';
waitForUI().then(function () {
if (useWorker) {
return workerPromise({ action: 'cleanup' });
}
return tester.cleanup();
}).then(function () {
disableButtons(false);
display.innerHTML += '\nDone.';
}).catch(function (e) {
disableButtons(false);
display.innerHTML += "\nError: " + e;
console.error(e);
});
});
});