-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworker-impl.js
72 lines (58 loc) · 1.38 KB
/
worker-impl.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
"format es6";
import h from 'virtual-dom/h';
import diff from 'virtual-dom/diff';
import {primeFactorization} from './spin-loader';
// TODO: receive initial vdom from main thread
const initial = h('span', ['hello']);
this.addEventListener('message', function(event) {
var data = event.data;
if (data.action === 'event') {
const id = data.id;
if (registered[id]) {
registered[id](data.event);
} else {
console.error("Received event for unregistered handler, id: " + id);
}
}
});
// TODO: free up once no longer used? WeakMap?
const registered = {};
let maxId = 1;
function register(func) {
const id = `event-${maxId++}`;
registered[id] = func;
return {emit: id};
}
let i = 0;
function counter(n) {
return h('span', [
`hello: ${n} `,
h('button', {
'ev-click': register(ev => {
console.log("clicked Inc", ev);
i++;
// Really slow operation that blocks execution
primeFactorization(5000000000425343804321);
render();
})
}, 'Inc'),
h('button', {
'ev-click': register(ev => {
console.log("clicked Dec", ev);
i--;
render();
})
}, 'Dec')
]);
}
let last = initial;
function render() {
const next = counter(i);
const d = diff(last, next);
last = next;
self.postMessage({
type: 'patch',
patch: d
});
}
render();