Skip to content

Commit e477669

Browse files
committed
Working API
1 parent f79c204 commit e477669

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

examples/transferables.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
});
128128

129129
// Start the ForceAtlas2 algorithm:
130-
s.testFA2Supervisor();
130+
s.startForceAtlas2();
131131
</script>
132132
</body>
133133
</html>

plugins/sigma.layout.forceAtlas2/supervisor.js

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
this.graph = this.sigInst.graph;
3535
this.ppn = 10;
3636
this.ppe = 3;
37+
38+
// State
39+
this.started = false;
3740
this.running = false;
3841

3942
// Web worker or classic DOM events?
@@ -62,17 +65,15 @@
6265
_this.applyLayoutChanges();
6366

6467
// Send data back to worker and loop
65-
_this.sendByteArrayToWorker();
68+
if (_this.running)
69+
_this.sendByteArrayToWorker();
6670

6771
// Rendering graph
6872
_this.sigInst.refresh();
6973
});
7074

7175
// Filling byteArrays
7276
this.graphToByteArrays();
73-
74-
// Sending to worker
75-
this.sendByteArrayToWorker('start');
7677
}
7778

7879
Supervisor.prototype.makeBlob = function(workerFn) {
@@ -171,30 +172,72 @@
171172
window.postMessage(content, '*')
172173
};
173174

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+
};
174203

175204
/**
176205
* Interface
177206
* ----------
178207
*/
179-
var supervisor;
208+
var supervisor = null;
180209

181210
sigma.prototype.startForceAtlas2 = function() {
182211

183212
// Create supervisor if undefined
213+
if (!supervisor)
214+
supervisor = new Supervisor(this);
215+
184216
// Start algorithm
217+
supervisor.start();
218+
219+
return this;
185220
};
221+
186222
sigma.prototype.stopForceAtlas2 = function() {
187223

188224
// Pause algorithm
225+
supervisor.stop();
226+
227+
return this;
189228
};
229+
190230
sigma.prototype.killForceAtlas2 = function() {
191231

192-
// Stop and kill worker
232+
// Stop Algorithm
233+
supervisor.stop();
234+
235+
// Kill Worker
236+
supervisor.killWorker();
237+
193238
// Kill supervisor
194-
};
239+
supervisor = null;
195240

196-
sigma.prototype.testFA2Supervisor = function() {
197-
supervisor = new Supervisor(this);
198241
return this;
199242
};
200243
}).call(this);

0 commit comments

Comments
 (0)