-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatch-worker.js
62 lines (54 loc) · 1.04 KB
/
dispatch-worker.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
var state;
function range(start, end, step) {
step = step || 1;
r = [];
for(var i = start; i < end; i+=step) {
r.push(i);
}
return r;
}
onmessage = function(event) {
var message = event.data;
switch(message.type) {
case 'init':
state = {
'queue': range(0, message.chunks),
'processing': []
};
postMessage({
'type': 'init',
});
break;
case 'status':
postMessage({
'type': 'status',
'status': state,
});
break;
case 'next':
next = state['queue'].shift();
state['processing'].push(next);
postMessage({
'type': 'next',
'next': next,
});
break;
case 'success':
state['processing'] = state['processing'].filter(function(idx) {
return idx != message.index;
});
if(!state['processing'].length) postMessage({
'type': 'success',
});
break;
case 'enqueue':
state['queue'].push(message.index).sort();
break;
default:
postMessage({
'type': 'error',
'error': new Error('Not implemented in worker: ' + message.type);
});
break;
}
}