-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathGraphLayout.jsx
More file actions
54 lines (47 loc) · 1.06 KB
/
GraphLayout.jsx
File metadata and controls
54 lines (47 loc) · 1.06 KB
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
class GraphLayout{
activeWorkers = {}
currentWorkerId = 0
lastFinishedWorkerId = 0
callback = function(){}
constructor(callback) {
this.callback = callback
}
layout(graph) {
const id = this.getWorkerId()
this.activeWorkers[id] = new LayoutWorker(id, graph, this.workerFinished.bind(this))
}
workerFinished({id, graph}) {
if (id >= this.lastFinishedWorkerId) {
this.lastFinishedWorkerId = id
this.callback(graph)
}
}
getWorkerId() {
this.currentWorkerId += 1
return this.currentWorkerId
}
}
class LayoutWorker{
id = 0
worker = null
constructor(id, graph, onFinished) {
this.id = id
this.worker = new Worker("src/scripts/GraphLayoutWorker.js")
this.worker.addEventListener("message", this.receive.bind(this))
this.onFinished = onFinished
this.worker.postMessage(this.encode(graph))
}
receive(message) {
this.worker.terminate()
this.onFinished({
id: this.id,
graph: this.decode(message.data)
})
}
encode(graph) {
return graphlib.json.write(graph)
}
decode(json) {
return graphlib.json.read(json)
}
}