-
Notifications
You must be signed in to change notification settings - Fork 3
/
relations.js
90 lines (75 loc) · 2.37 KB
/
relations.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
const _ = require('lodash');
const Graph = require('@dagrejs/graphlib').Graph;
const dijkstraAll = require('@dagrejs/graphlib').alg.dijkstraAll;
class Relations {
constructor(pHb) {
if (arguments.length === 0)
this.hb = [];
else
this.hb = pHb;
}
add(a, b, type) {
this.hb.push({ a, b, type });
}
remove(a, b) {
_.remove(this.hb, (r) => r.a === a && r.b === b);
}
registeredInSameTick(aid, bid) {
let pa = this.hb.find((h) => h.b === aid && h.type === 'registration');
let pb = this.hb.find((h) => h.b === bid && h.type === 'registration');
if (!pa || !pb)
return false;
return pa.a === pb.a;
}
registeredIn(aid) {
let r = this.hb.find((h) => h.b === aid && h.type === 'registration');
if (r)
return r.a;
return null;
}
resolvedIn(aid) {
let r = this.hb.find((h) => h.b === aid && h.type === 'promise-resolve');
if (r)
return r.a;
return null;
}
getRegisteredBy(aid) {
return this.hb
.filter((h) => h.a === aid && h.type === 'registration')
.map((h) => h.b);
}
findDirectRelation(aoi, aoj) {
return this.hb.find((r) => r.a === aoi && r.b === aoj);
}
happensBefore(aoi, aoj) {
let visited = {};
let rels = this.hb.filter(r => r.a === aoi);
while (rels.length > 0) {
let relation = rels.pop();
if (!visited[relation.b]) {
visited[relation.b] = true;
if (relation.b === aoj)
return true;
else {
let ind_rels = this.hb.filter(r => r.a === relation.b);
rels.push(...ind_rels);
}
}
}
return false;
}
startGraphLibDataStructure(nodes) {
let graph = new Graph();
nodes.forEach(n => graph.setNode(n.id));
this.hb.forEach(r => graph.setEdge(r.a, r.b));
this.graph = dijkstraAll(graph);
}
happensBeforeWithGraphLib(aoi, aoj) {
return this.graph[aoi][aoj].distance > 0
&& this.graph[aoi][aoj].distance !== Number.POSITIVE_INFINITY;
}
removeIncomingTo(id) {
_.remove(this.hb, (r) => r.b === id && r.type === 'promise-resolve');
}
}
module.exports = Relations