|
5 | 5 | throw 'sigma is not declared';
|
6 | 6 |
|
7 | 7 | /**
|
8 |
| - * Sigma ForceAtlas2.1 Supervisor |
| 8 | + * Sigma ForceAtlas2.5 Supervisor |
9 | 9 | * =============================
|
10 | 10 | *
|
11 | 11 | * Author: Guillaume Plique (Yomguithereal)
|
12 | 12 | * Version: 0.1
|
13 | 13 | */
|
14 | 14 |
|
15 | 15 | /**
|
16 |
| - * Helpers Namespace |
| 16 | + * Feature detection |
17 | 17 | * ------------------
|
18 | 18 | */
|
19 |
| - var _helpers = {}; |
| 19 | + var webWorkers = 'Worker' in window; |
20 | 20 |
|
21 | 21 | /**
|
22 | 22 | * Supervisor Object
|
23 | 23 | * ------------------
|
24 | 24 | */
|
25 |
| - function Supervisor(sigInst, workerFunc, options) { |
26 |
| - var _this = this; |
| 25 | + function Supervisor(sigInst, options) { |
| 26 | + var _this = this, |
| 27 | + workerFn = sigInst.getForceAtlas2Worker(); |
27 | 28 |
|
28 |
| - // TODO: later, check if transferable is possible |
29 | 29 | // Window URL Polyfill
|
30 | 30 | window.URL = window.URL || window.webkitURL;
|
31 | 31 |
|
32 | 32 | // Properties
|
33 | 33 | this.sigInst = sigInst;
|
34 | 34 | this.graph = this.sigInst.graph;
|
35 |
| - this.ppn = 8; |
| 35 | + this.ppn = 10; |
36 | 36 | this.ppe = 3;
|
| 37 | + this.running = false; |
37 | 38 |
|
38 |
| - var blob = this.makeBlob(workerFunc); |
39 |
| - this.worker = new Worker(URL.createObjectURL(blob)); |
| 39 | + // Web worker or classic DOM events? |
| 40 | + if (webWorkers) { |
| 41 | + var blob = this.makeBlob(workerFn); |
| 42 | + this.worker = new Worker(URL.createObjectURL(blob)); |
| 43 | + |
| 44 | + // Post Message Polyfill |
| 45 | + this.worker.postMessage = |
| 46 | + this.worker.webkitPostMessage || this.worker.postMessage; |
| 47 | + } |
| 48 | + else { |
| 49 | + |
| 50 | + } |
40 | 51 |
|
41 | 52 | // Worker message receiver
|
42 |
| - this.worker.onmessage = function(e) { |
| 53 | + this.worker.addEventListener('message', function(e) { |
43 | 54 |
|
44 | 55 | // Retrieving data
|
45 | 56 | _this.nodesByteArray = new Float32Array(e.data.nodes);
|
|
49 | 60 |
|
50 | 61 | // Send data back to worker and loop
|
51 | 62 | _this.sendByteArrayToWorker();
|
52 |
| - }; |
53 |
| - |
54 |
| - // Post Message Polyfill |
55 |
| - this.worker.postMessage = |
56 |
| - this.worker.webkitPostMessage || this.worker.postMessage; |
| 63 | + }); |
57 | 64 |
|
58 | 65 | // Filling byteArrays
|
59 | 66 | this.graphToByteArrays();
|
|
62 | 69 | this.sendByteArrayToWorker('start');
|
63 | 70 | }
|
64 | 71 |
|
65 |
| - Supervisor.prototype.makeBlob = function(workerFunc) { |
| 72 | + Supervisor.prototype.makeBlob = function(workerFn) { |
66 | 73 | var blob;
|
67 | 74 |
|
68 | 75 | try {
|
69 |
| - blob = new Blob([workerFunc], {type: 'application/javascript'}); |
| 76 | + blob = new Blob([workerFn], {type: 'application/javascript'}); |
70 | 77 | }
|
71 | 78 | catch (e) {
|
72 | 79 | window.BlobBuilder = window.BlobBuilder ||
|
73 | 80 | window.WebKitBlobBuilder ||
|
74 | 81 | window.MozBlobBuilder;
|
75 | 82 |
|
76 | 83 | blob = new BlobBuilder();
|
77 |
| - blob.append(workerFunc); |
| 84 | + blob.append(workerFn); |
78 | 85 | blob = blob.getBlob();
|
79 | 86 | }
|
80 | 87 |
|
|
109 | 116 | this.nodesByteArray[j + 4] = 0;
|
110 | 117 | this.nodesByteArray[j + 5] = 0;
|
111 | 118 | this.nodesByteArray[j + 6] = 1 + this.graph.degree(nodes[i].id);
|
112 |
| - this.nodesByteArray[j + 7] = 0; |
| 119 | + this.nodesByteArray[j + 7] = 1; |
| 120 | + this.nodesByteArray[j + 8] = nodes[i].size; |
| 121 | + this.nodesByteArray[j + 9] = 0; |
113 | 122 | j += this.ppn;
|
114 | 123 | }
|
115 | 124 |
|
|
139 | 148 | this.sigInst.refresh();
|
140 | 149 | };
|
141 | 150 |
|
142 |
| - Supervisor.prototype.sendByteArrayToWorker = function(header) { |
| 151 | + Supervisor.prototype.sendByteArrayToWorker = function(action) { |
143 | 152 | var content = {
|
144 |
| - header: header || 'loop', |
| 153 | + action: action || 'loop', |
145 | 154 | nodes: this.nodesByteArray.buffer
|
146 | 155 | };
|
147 | 156 |
|
148 | 157 | var buffers = [this.nodesByteArray.buffer];
|
149 | 158 |
|
150 |
| - if (header === 'start') { |
| 159 | + if (action === 'start') { |
151 | 160 | content.config = {};
|
152 | 161 | content.edges = this.edgesByteArray.buffer;
|
153 | 162 | buffers.push(this.edgesByteArray.buffer);
|
|
161 | 170 | * Interface
|
162 | 171 | * ----------
|
163 | 172 | */
|
| 173 | + var supervisor; |
164 | 174 |
|
165 |
| - sigma.prototype.startForceAtlas2 = function() {}; |
166 |
| - sigma.prototype.stopForceAtlas2 = function() {}; |
| 175 | + sigma.prototype.startForceAtlas2 = function() { |
| 176 | + |
| 177 | + // Create supervisor if undefined |
| 178 | + // Start algorithm |
| 179 | + }; |
| 180 | + sigma.prototype.stopForceAtlas2 = function() { |
| 181 | + |
| 182 | + // Pause algorithm |
| 183 | + }; |
| 184 | + sigma.prototype.killForceAtlas2 = function() { |
| 185 | + |
| 186 | + // Stop and kill worker |
| 187 | + // Kill supervisor |
| 188 | + }; |
167 | 189 |
|
168 | 190 | sigma.prototype.testFA2Supervisor = function() {
|
169 |
| - new Supervisor(this, this.getForceAtlas2Worker()); |
| 191 | + supervisor = new Supervisor(this); |
170 | 192 | return this;
|
171 | 193 | };
|
172 | 194 | }).call(this);
|
0 commit comments