forked from w8r/graphology-layout-forceatlas2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupervisor.js
187 lines (148 loc) · 4.17 KB
/
supervisor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* Graphology ForceAtlas2 Layout Supervisor
* =========================================
*
* Supervisor class able to spawn a web worker to run the FA2 layout in a
* separate thread not to block UI with heavy synchronous computations.
*/
var Worker = require('worker-loader?inline&fallback=false!./webworker.js'),
isGraph = require('graphology-utils/is-graph'),
helpers = require('./helpers.js');
var DEFAULT_SETTINGS = require('./defaults.js');
/**
* Class representing a FA2 layout run by a webworker.
*
* @constructor
* @param {Graph} graph - Target graph.
* @param {object|number} params - Parameters:
* @param {object} [settings] - Settings.
*/
function FA2LayoutSupervisor(graph, params) {
params = params || {};
// Validation
if (!isGraph(graph))
throw new Error('graphology-layout-forceatlas2/worker: the given graph is not a valid graphology instance.');
// Validating settings
var settings = helpers.assign({}, DEFAULT_SETTINGS, params.settings),
validationError = helpers.validateSettings(settings);
if (validationError)
throw new Error('graphology-layout-forceatlas2/worker: ' + validationError.message);
// Properties
this.worker = null;
this.graph = graph;
this.settings = settings;
this.matrices = null;
this.running = false;
this.killed = false;
// Binding listeners
this.handleMessage = this.handleMessage.bind(this);
var alreadyRespawning = false;
var self = this;
this.handleAddition = function() {
if (alreadyRespawning)
return;
alreadyRespawning = true;
self.spawnWorker();
setImmediate(function() {
alreadyRespawning = false;
});
};
graph.on('nodeAdded', this.handleAddition);
graph.on('edgeAdded', this.handleAddition);
// Spawning worker
this.spawnWorker();
}
/**
* Internal method used to spawn the web worker.
*/
FA2LayoutSupervisor.prototype.spawnWorker = function() {
if (this.worker)
this.worker.terminate();
this.worker = new Worker();
this.worker.addEventListener('message', this.handleMessage);
if (this.running) {
this.running = false;
this.start();
}
};
/**
* Internal method used to handle the worker's messages.
*
* @param {object} event - Event to handle.
*/
FA2LayoutSupervisor.prototype.handleMessage = function(event) {
if (!this.running)
return;
var matrix = new Float32Array(event.data.nodes);
helpers.applyLayoutChanges(this.graph, matrix);
this.matrices.nodes = matrix;
// Looping
this.askForIterations();
};
/**
* Internal method used to ask for iterations from the worker.
*
* @param {boolean} withEdges - Should we send edges along?
* @return {FA2LayoutSupervisor}
*/
FA2LayoutSupervisor.prototype.askForIterations = function(withEdges) {
var matrices = this.matrices;
var payload = {
settings: this.settings,
nodes: matrices.nodes.buffer
};
var buffers = [matrices.nodes.buffer];
if (withEdges) {
payload.edges = matrices.edges.buffer;
buffers.push(matrices.edges.buffer);
}
this.worker.postMessage(payload, buffers);
return this;
};
/**
* Method used to start the layout.
*
* @return {FA2LayoutSupervisor}
*/
FA2LayoutSupervisor.prototype.start = function() {
if (this.killed)
throw new Error('graphology-layout-forceatlas2/worker.start: layout was killed.');
if (this.running)
return this;
// Building matrices
this.matrices = helpers.graphToByteArrays(this.graph);
this.running = true;
this.askForIterations(true);
return this;
};
/**
* Method used to stop the layout.
*
* @return {FA2LayoutSupervisor}
*/
FA2LayoutSupervisor.prototype.stop = function() {
this.running = false;
return this;
};
/**
* Method used to kill the layout.
*
* @return {FA2LayoutSupervisor}
*/
FA2LayoutSupervisor.prototype.kill = function() {
if (this.killed)
return this;
this.running = false;
this.killed = true;
// Clearing memory
this.matrices = null;
// Terminating worker
this.worker.terminate();
// Unbinding listeners
this.graph.removeListener('nodeAdded', this.handleAddition);
this.graph.removeListener('edgeAdded', this.handleAddition);
};
/**
* Exporting.
*/
module.exports = FA2LayoutSupervisor;