-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrunner.mjs
More file actions
99 lines (85 loc) · 2.81 KB
/
runner.mjs
File metadata and controls
99 lines (85 loc) · 2.81 KB
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
import { getState, updateState } from './state.mjs';
const features = document.querySelector('#features');
const output = document.querySelector('#output');
let worker;
function respawn(first = false) {
if (worker) {
worker.terminate();
}
worker = new Worker(new URL('./worker.js', import.meta.url));
worker.addEventListener('message', ({ data }) => {
console.log('@MAIN', data); // eslint-disable-line no-console
if (first && data.type === 'initialize') {
const { FEATURES } = data.value;
FEATURES.forEach(({ name }) => {
// <li>
// <label>
// <input type="checkbox">
// {name}
// </label>
// </li>
const input = document.createElement('input');
input.type = 'checkbox';
input.addEventListener('change', () => {
getState('features')
.then((f) => {
if (input.checked) {
f.add(name);
} else {
f.delete(name);
}
updateState();
respawn();
});
});
getState('features')
.then((requestedFeatures) => {
input.checked = requestedFeatures.has(name);
});
const id = name.replace(/\W+/g, '_');
input.id = id;
const label = document.createElement('label');
label.htmlFor = id;
label.appendChild(document.createTextNode(name));
const pair = document.createElement('div');
pair.appendChild(input);
pair.appendChild(label);
const li = document.createElement('li');
li.appendChild(pair);
features.appendChild(li);
});
} else if (data.type === 'console') {
if (data.value.method === 'clear') {
const range = document.createRange();
range.selectNodeContents(output);
range.deleteContents();
} else {
const line = document.createElement('span');
data.value.values.forEach((v) => {
line.textContent += v;
line.textContent += ' ';
});
const container = document.createElement('div');
container.className = `log-${data.value.method}`;
container.appendChild(line);
output.appendChild(container);
}
} else if (data.type === 'unhandledRejection') {
const line = document.createElement('span');
line.textContent = `Unhandled Rejection:\n${data.value}`;
const container = document.createElement('div');
container.className = 'log-error';
container.appendChild(line);
output.appendChild(container);
}
});
}
export function evaluate(code) {
getState()
.then((state) => {
state.set('code', code);
worker.postMessage({ type: 'evaluate', value: { code, state } });
return updateState();
});
}
respawn(true);