Skip to content

Commit ed87ed2

Browse files
committed
Starting FA2 anew
1 parent cf5b91b commit ed87ed2

File tree

2 files changed

+328
-957
lines changed

2 files changed

+328
-957
lines changed

plugins/sigma.layout.forceAtlas2/supervisor.js

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,52 @@
55
throw 'sigma is not declared';
66

77
/**
8-
* Sigma ForceAtlas2.1 Supervisor
8+
* Sigma ForceAtlas2.5 Supervisor
99
* =============================
1010
*
1111
* Author: Guillaume Plique (Yomguithereal)
1212
* Version: 0.1
1313
*/
1414

1515
/**
16-
* Helpers Namespace
16+
* Feature detection
1717
* ------------------
1818
*/
19-
var _helpers = {};
19+
var webWorkers = 'Worker' in window;
2020

2121
/**
2222
* Supervisor Object
2323
* ------------------
2424
*/
25-
function Supervisor(sigInst, workerFunc, options) {
26-
var _this = this;
25+
function Supervisor(sigInst, options) {
26+
var _this = this,
27+
workerFn = sigInst.getForceAtlas2Worker();
2728

28-
// TODO: later, check if transferable is possible
2929
// Window URL Polyfill
3030
window.URL = window.URL || window.webkitURL;
3131

3232
// Properties
3333
this.sigInst = sigInst;
3434
this.graph = this.sigInst.graph;
35-
this.ppn = 8;
35+
this.ppn = 10;
3636
this.ppe = 3;
37+
this.running = false;
3738

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+
}
4051

4152
// Worker message receiver
42-
this.worker.onmessage = function(e) {
53+
this.worker.addEventListener('message', function(e) {
4354

4455
// Retrieving data
4556
_this.nodesByteArray = new Float32Array(e.data.nodes);
@@ -49,11 +60,7 @@
4960

5061
// Send data back to worker and loop
5162
_this.sendByteArrayToWorker();
52-
};
53-
54-
// Post Message Polyfill
55-
this.worker.postMessage =
56-
this.worker.webkitPostMessage || this.worker.postMessage;
63+
});
5764

5865
// Filling byteArrays
5966
this.graphToByteArrays();
@@ -62,19 +69,19 @@
6269
this.sendByteArrayToWorker('start');
6370
}
6471

65-
Supervisor.prototype.makeBlob = function(workerFunc) {
72+
Supervisor.prototype.makeBlob = function(workerFn) {
6673
var blob;
6774

6875
try {
69-
blob = new Blob([workerFunc], {type: 'application/javascript'});
76+
blob = new Blob([workerFn], {type: 'application/javascript'});
7077
}
7178
catch (e) {
7279
window.BlobBuilder = window.BlobBuilder ||
7380
window.WebKitBlobBuilder ||
7481
window.MozBlobBuilder;
7582

7683
blob = new BlobBuilder();
77-
blob.append(workerFunc);
84+
blob.append(workerFn);
7885
blob = blob.getBlob();
7986
}
8087

@@ -109,7 +116,9 @@
109116
this.nodesByteArray[j + 4] = 0;
110117
this.nodesByteArray[j + 5] = 0;
111118
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;
113122
j += this.ppn;
114123
}
115124

@@ -139,15 +148,15 @@
139148
this.sigInst.refresh();
140149
};
141150

142-
Supervisor.prototype.sendByteArrayToWorker = function(header) {
151+
Supervisor.prototype.sendByteArrayToWorker = function(action) {
143152
var content = {
144-
header: header || 'loop',
153+
action: action || 'loop',
145154
nodes: this.nodesByteArray.buffer
146155
};
147156

148157
var buffers = [this.nodesByteArray.buffer];
149158

150-
if (header === 'start') {
159+
if (action === 'start') {
151160
content.config = {};
152161
content.edges = this.edgesByteArray.buffer;
153162
buffers.push(this.edgesByteArray.buffer);
@@ -161,12 +170,25 @@
161170
* Interface
162171
* ----------
163172
*/
173+
var supervisor;
164174

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

168190
sigma.prototype.testFA2Supervisor = function() {
169-
new Supervisor(this, this.getForceAtlas2Worker());
191+
supervisor = new Supervisor(this);
170192
return this;
171193
};
172194
}).call(this);

0 commit comments

Comments
 (0)