|
1 | | -// var arguments = ["network.txt", "-z", '--clu', "."]; |
2 | | -var Module = {}; |
3 | | - |
4 | | -Module['preRun'] = function infomap_preRun() { |
5 | | - console.log("Pre-run: adding 'filesReady' as run dependency..."); |
6 | | - addRunDependency('filesReady'); |
7 | | -}; |
8 | | - |
9 | | -Module['postRun'] = function infomap_postRun() { |
10 | | - console.log("Post-run: Read result..."); |
11 | | - var output = {}; |
12 | | - var args = Module['arguments']; |
13 | | - // if (args.indexOf('--clu') != -1) |
14 | | - // output.clu = FS.readFile("network.clu", {encoding: "utf8"}); |
15 | | - // else |
16 | | - // output.tree = FS.readFile("network.tree", {encoding: "utf8"}); |
17 | | - var clu = readFile("network.clu"); |
18 | | - if (clu) |
19 | | - output.clu = clu; |
20 | | - var tree = readFile("network.tree"); |
21 | | - if (tree) |
22 | | - output.tree = tree; |
23 | | - postMessage({ target: 'finished', output: output}); |
24 | | -}; |
25 | | - |
26 | | -Module['print'] = function Module_print(x) { |
27 | | - //dump('OUT: ' + x + '\n'); |
28 | | - postMessage({ target: 'stdout', content: x }); |
29 | | -}; |
30 | | -Module['printErr'] = function Module_printErr(x) { |
31 | | - //dump('ERR: ' + x + '\n'); |
32 | | - postMessage({ target: 'stderr', content: x }); |
33 | | -}; |
34 | | - |
35 | 1 | function readFile(filename) { |
36 | | - var content = ''; |
| 2 | + var content = ""; |
37 | 3 | try { |
38 | | - content = FS.readFile(filename, {encoding: "utf8"}); |
| 4 | + content = FS.readFile(filename, { encoding: "utf8" }); |
39 | 5 | } catch (e) {} |
40 | 6 | return content; |
41 | 7 | } |
42 | 8 |
|
| 9 | +var infomapWorkerId = -1; |
| 10 | + |
| 11 | +var memoryHackRequest = { |
| 12 | + status: 200, |
| 13 | + useRequest: null, |
| 14 | + addEventListener: function(event, callback) { |
| 15 | + if (event === "load") { |
| 16 | + this.useRequest = callback; |
| 17 | + } |
| 18 | + } |
| 19 | +}; |
| 20 | + |
| 21 | +var Module = { |
| 22 | + arguments: [], |
| 23 | + preRun: function() { |
| 24 | + addRunDependency("filesReady"); |
| 25 | + }, |
| 26 | + print: function(content) { |
| 27 | + postMessage({ type: "data", content, id: infomapWorkerId }); |
| 28 | + }, |
| 29 | + printErr: function(content) { |
| 30 | + postMessage({ type: "error", content, id: infomapWorkerId }); |
| 31 | + }, |
| 32 | + postRun: function() { |
| 33 | + var content = {}; |
| 34 | + var clu = readFile("network.clu"); |
| 35 | + if (clu) content.clu = clu; |
| 36 | + var tree = readFile("network.tree"); |
| 37 | + if (tree) content.tree = tree; |
| 38 | + var ftree = readFile("network.ftree"); |
| 39 | + if (ftree) content.ftree = ftree; |
| 40 | + postMessage({ type: "finished", content, id: infomapWorkerId }); |
| 41 | + }, |
| 42 | + memoryInitializerRequest: memoryHackRequest |
| 43 | +}; |
| 44 | + |
43 | 45 | onmessage = function onmessage(message) { |
44 | | - //dump('worker got ' + JSON.stringify(message.data).substr(0, 150) + '\n'); |
45 | 46 | var data = message.data; |
46 | | - switch (data.target) { |
47 | | - case 'Infomap': { |
48 | | - var args = [data.inputFilename, "."]; |
49 | | - if (data.arguments) |
50 | | - args = args.concat(data.arguments); |
51 | | - // Module.print("Worker got 'Infomap' with arguments: " + args); |
52 | | - Module['arguments'] = args; |
53 | | - // Module.print("Writing file '" + data.inputFilename + "' with data:\n" + data.inputData); |
54 | | - FS.writeFile(data.inputFilename, data.inputData); |
55 | | - removeRunDependency('filesReady'); |
56 | | - break; |
57 | | - } |
58 | | - default: |
59 | | - throw 'Unknown target on message to worker: ' + JSON.stringify(data).substr(0, 150); |
| 47 | + |
| 48 | + if (data.target === "Infomap") { |
| 49 | + memoryHackRequest.response = data.memBuffer; |
| 50 | + memoryHackRequest.useRequest(); |
| 51 | + infomapWorkerId = data.id; |
| 52 | + var args = [data.inputFilename, "."]; |
| 53 | + if (data.arguments) args = args.concat(data.arguments); |
| 54 | + Module.arguments.push(...args); |
| 55 | + FS.writeFile(data.inputFilename, data.inputData); |
| 56 | + removeRunDependency("filesReady"); |
| 57 | + } else { |
| 58 | + throw "Unknown target on message to worker: " + |
| 59 | + JSON.stringify(data).substr(0, 150); |
60 | 60 | } |
61 | 61 | }; |
0 commit comments