-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci_worker.js
46 lines (38 loc) · 1.05 KB
/
fibonacci_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
var fibonacciInit = Module.cwrap('init', null, ['string']);
var fibonacciStep = Module.cwrap('step', null, ['string']);
var filePath = '/sfa/fibonacci';
Module.onRuntimeInitialized = function() {
FS.mkdir('/sfa');
FS.mount(SFAFS, { root: '.' }, '/sfa');
// Storage Foundation API must explicitly request capacity.
storageFoundation.requestCapacitySync(10000);
fibonacciInit(filePath);
getData();
}
var getData = function() {
// Each int entry is composed of 4 bytes
var file = FS.open(filePath, 'r')
var entryBuffer = new SharedArrayBuffer(4);
var entryView = new Uint32Array(entryBuffer);
var result = [];
while(FS.read(file, entryView, 0, 4) != 0) {
result.push(entryView[0]);
}
FS.close(file);
self.postMessage({command: 'getData', result: result});
}
onmessage = function(event) {
switch(event.data.command) {
case 'getData':
getData();
break;
case 'step':
fibonacciStep(filePath);
break;
case 'reset':
FS.unlink(filePath);
fibonacciInit(filePath);
getData();
break;
}
};