This repository was archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMicroVizEvents.js
More file actions
71 lines (61 loc) · 1.75 KB
/
MicroVizEvents.js
File metadata and controls
71 lines (61 loc) · 1.75 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
class MicroVizEvents {
constructor(programOrSendEvent, sourceLoc) {
this.programOrSendEvent = programOrSendEvent;
this.sourceLoc = sourceLoc;
this.eventGroups = [];
}
get lastEventGroup() {
return this.eventGroups[this.eventGroups.length - 1];
}
add(event) {
const eventIsLocal = this.sourceLoc.strictlyContains(event.sourceLoc);
if (eventIsLocal && this.lastEventGroup instanceof LocalEventGroup) {
const lastEvent = this.lastEventGroup.lastEvent;
if (event.sourceLoc.startPos >= lastEvent.sourceLoc.endPos || // Toby's rule
event.sourceLoc.strictlyContains(lastEvent.sourceLoc)) { // Inside-out rule
// no-op
} else {
this.eventGroups.push(new LocalEventGroup());
}
} else if (eventIsLocal && !(this.lastEventGroup instanceof LocalEventGroup)) {
this.eventGroups.push(new LocalEventGroup());
} else if (!eventIsLocal && !(this.lastEventGroup instanceof RemoteEventGroup)) {
this.eventGroups.push(new RemoteEventGroup());
}
this.lastEventGroup.add(event);
}
}
class AbstractEventGroup {
constructor(events) {
this.events = events;
}
add(event) {
throw new Error('abstract method!');
}
get lastEvent() {
return this.events[this.events.length - 1];
}
}
class LocalEventGroup extends AbstractEventGroup {
constructor(...events) {
super(events);
}
add(event) {
this.events.push(event);
}
}
class RemoteEventGroup extends AbstractEventGroup {
constructor(...events) {
super(events);
}
add(event) {
for (let idx = 0; idx < this.events.length; idx++) {
if (event.subsumes(this.events[idx])) {
this.events[idx] = event;
return;
}
}
this.events.push(event);
}
}