Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove separate treatment queries #9

Merged
merged 23 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions JustificationSet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
}
};
_();
},
);
},
};
}
}
Loading