-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathExample-6-1-BIG_data_worker.js
71 lines (58 loc) · 1.43 KB
/
Example-6-1-BIG_data_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
63
64
65
66
67
68
69
70
71
// Take care of vendor prefixes.
self.postMessage = self.webkitPostMessage || self.postMessage;
var ready = false;
function time() {
var now = new Date();
var time = /(\d+:\d+:\d+)/.exec(now)[0] + ':';
for (var ms = String(now.getMilliseconds()), i = ms.length - 3; i < 0; ++i) {
time += '0';
}
return time + ms;
}
function source() {
return '<span style="color:red;">The Worker:</span> ';
}
self.onmessage = function(e) {
if (!ready) {
initComplete();
return;
}
var USE_TRANSFERRABLE = true;
var dataLength;
var uInt8View = null;
if (e.data.copy !== undefined) {
// not a copy case
USE_TRANSFERRABLE = false;
uInt8View = new Uint8Array(e.data.ourArray);
dataLength = e.data.ourArray.byteLength;
}
else {
uInt8View = new Uint8Array(e.data);
e.data.byteLength = e.data.byteLength;
}
// Here we are 'computing' something important on the data.
// In our case - just play with %
for (var i=0; i < dataLength; i++ ) {
uInt8View[i] = uInt8View[i] % 2;
}
if (USE_TRANSFERRABLE) {
self.postMessage(uInt8View.buffer, [uInt8View.buffer]);
} else {
self.postMessage(e.data.ourArray);
}
};
self.onerror = function(message) {
log('worker error');
};
function log(msg) {
var object = {
type: 'debug',
msg: source() + msg + ' [' + time() + ']'
};
self.postMessage(object);
}
function initComplete() {
ready = true;
log('READY!');
}
//setupArray();