|
34 | 34 | this.graph = this.sigInst.graph;
|
35 | 35 | this.ppn = 10;
|
36 | 36 | this.ppe = 3;
|
| 37 | + |
| 38 | + // State |
| 39 | + this.started = false; |
37 | 40 | this.running = false;
|
38 | 41 |
|
39 | 42 | // Web worker or classic DOM events?
|
|
62 | 65 | _this.applyLayoutChanges();
|
63 | 66 |
|
64 | 67 | // Send data back to worker and loop
|
65 |
| - _this.sendByteArrayToWorker(); |
| 68 | + if (_this.running) |
| 69 | + _this.sendByteArrayToWorker(); |
66 | 70 |
|
67 | 71 | // Rendering graph
|
68 | 72 | _this.sigInst.refresh();
|
69 | 73 | });
|
70 | 74 |
|
71 | 75 | // Filling byteArrays
|
72 | 76 | this.graphToByteArrays();
|
73 |
| - |
74 |
| - // Sending to worker |
75 |
| - this.sendByteArrayToWorker('start'); |
76 | 77 | }
|
77 | 78 |
|
78 | 79 | Supervisor.prototype.makeBlob = function(workerFn) {
|
|
171 | 172 | window.postMessage(content, '*')
|
172 | 173 | };
|
173 | 174 |
|
| 175 | + Supervisor.prototype.start = function() { |
| 176 | + if (this.running) |
| 177 | + return; |
| 178 | + |
| 179 | + this.running = true; |
| 180 | + |
| 181 | + if (!this.started) { |
| 182 | + |
| 183 | + // Sending init message to worker |
| 184 | + this.sendByteArrayToWorker('start'); |
| 185 | + this.started = true; |
| 186 | + } |
| 187 | + else { |
| 188 | + this.sendByteArrayToWorker(); |
| 189 | + } |
| 190 | + }; |
| 191 | + |
| 192 | + Supervisor.prototype.stop = function() { |
| 193 | + if (!this.running) |
| 194 | + return; |
| 195 | + |
| 196 | + this.running = false; |
| 197 | + }; |
| 198 | + |
| 199 | + // TODO: kill polyfill when worker is not true worker |
| 200 | + Supervisor.prototype.killWorker = function() { |
| 201 | + this.worker.terminate(); |
| 202 | + }; |
174 | 203 |
|
175 | 204 | /**
|
176 | 205 | * Interface
|
177 | 206 | * ----------
|
178 | 207 | */
|
179 |
| - var supervisor; |
| 208 | + var supervisor = null; |
180 | 209 |
|
181 | 210 | sigma.prototype.startForceAtlas2 = function() {
|
182 | 211 |
|
183 | 212 | // Create supervisor if undefined
|
| 213 | + if (!supervisor) |
| 214 | + supervisor = new Supervisor(this); |
| 215 | + |
184 | 216 | // Start algorithm
|
| 217 | + supervisor.start(); |
| 218 | + |
| 219 | + return this; |
185 | 220 | };
|
| 221 | + |
186 | 222 | sigma.prototype.stopForceAtlas2 = function() {
|
187 | 223 |
|
188 | 224 | // Pause algorithm
|
| 225 | + supervisor.stop(); |
| 226 | + |
| 227 | + return this; |
189 | 228 | };
|
| 229 | + |
190 | 230 | sigma.prototype.killForceAtlas2 = function() {
|
191 | 231 |
|
192 |
| - // Stop and kill worker |
| 232 | + // Stop Algorithm |
| 233 | + supervisor.stop(); |
| 234 | + |
| 235 | + // Kill Worker |
| 236 | + supervisor.killWorker(); |
| 237 | + |
193 | 238 | // Kill supervisor
|
194 |
| - }; |
| 239 | + supervisor = null; |
195 | 240 |
|
196 |
| - sigma.prototype.testFA2Supervisor = function() { |
197 |
| - supervisor = new Supervisor(this); |
198 | 241 | return this;
|
199 | 242 | };
|
200 | 243 | }).call(this);
|
0 commit comments