-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJustificationSet.ts
114 lines (104 loc) · 3.27 KB
/
JustificationSet.ts
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
// @ts-ignore: Import unneccesary for typings, will collate .d.ts files
import type { JustifiedSynonym, Treatment } from "./SynonymGroup.ts";
interface Justification {
toString: () => string;
precedingSynonym?: JustifiedSynonym; // eslint-disable-line no-use-before-define
}
interface TreatmentJustification extends Justification {
treatment: Treatment;
}
type LexicalJustification = Justification;
export type anyJustification = TreatmentJustification | LexicalJustification;
export class JustificationSet implements AsyncIterable<anyJustification> {
private monitor = new EventTarget();
contents: anyJustification[] = [];
isFinished = false;
isAborted = false;
entries = ((Array.from(this.contents.values()).map((v) => [v, v])) as [
anyJustification,
anyJustification,
][]).values;
constructor(iterable?: Iterable<anyJustification>) {
if (iterable) {
for (const el of iterable) {
this.add(el);
}
}
return this;
}
get size() {
return new Promise<number>((resolve, reject) => {
if (this.isAborted) {
reject(new Error("JustificationSet has been aborted"));
} else if (this.isFinished) {
resolve(this.contents.length);
} else {
const listener = () => {
if (this.isFinished) {
this.monitor.removeEventListener("updated", listener);
resolve(this.contents.length);
}
};
this.monitor.addEventListener("updated", listener);
}
});
}
add(value: anyJustification) {
if (
this.contents.findIndex((c) => c.toString() === value.toString()) === -1
) {
this.contents.push(value);
this.monitor.dispatchEvent(new CustomEvent("updated"));
}
return this;
}
finish() {
//console.info("%cJustificationSet finished", "color: #69F0AE;");
this.isFinished = true;
this.monitor.dispatchEvent(new CustomEvent("updated"));
}
forEachCurrent(cb: (val: anyJustification) => void) {
this.contents.forEach(cb);
}
first() {
return new Promise<anyJustification>((resolve) => {
if (this.contents[0]) {
resolve(this.contents[0]);
} else {
this.monitor.addEventListener("update", () => {
resolve(this.contents[0]);
});
}
});
}
[Symbol.toStringTag] = "";
[Symbol.asyncIterator]() {
// this.monitor.addEventListener("updated", () => console.log("ARA"));
let returnedSoFar = 0;
return {
next: () => {
return new Promise<IteratorResult<anyJustification>>(
(resolve, reject) => {
const _ = () => {
if (this.isAborted) {
reject(new Error("JustificationSet has been aborted"));
} else if (returnedSoFar < this.contents.length) {
resolve({ value: this.contents[returnedSoFar++] });
} else if (this.isFinished) {
resolve({ done: true, value: true });
} else {
const listener = () => {
console.log("ahgfd");
this.monitor.removeEventListener("updated", listener);
_();
};
this.monitor.addEventListener("updated", listener);
}
};
_();
},
);
},
};
}
}