aboutsummaryrefslogtreecommitdiff
path: root/api/sentence.ts
diff options
context:
space:
mode:
Diffstat (limited to 'api/sentence.ts')
-rw-r--r--api/sentence.ts47
1 files changed, 39 insertions, 8 deletions
diff --git a/api/sentence.ts b/api/sentence.ts
index 1d22be3..6b1a1e4 100644
--- a/api/sentence.ts
+++ b/api/sentence.ts
@@ -7,25 +7,24 @@ export default class Sentence extends APIBase {
public words: Array<Word> = [];
protected query?: SearchSentenceResult;
protected original: string = "";
+ protected breaks: Array<number> = [];
+ protected frozen = false;
public ready: Promise<void>;
private _resolveReady: () => void = () => {};
constructor(input: string) {
super();
- this.ready = new Promise(res => this._resolveReady = res);
- this.fetch(input);
+ this.original = input;
+ this.update();
}
first(searchValue: RegExp | string): Word | undefined {
- return this.words[0];
+ return this.words[0]; // TODO: implement
}
- private async fetch(input: string) {
- this.original = input;
- this.query = await (await this.api)["core"].search.sentence(input);
- await this.updateWords();
- this._resolveReady();
+ private async fetch() {
+ this.query = await (await this.api)["core"].search.sentence(this.original, { breaks: this.breaks });
}
private async updateWords() {
@@ -53,4 +52,36 @@ export default class Sentence extends APIBase {
return out + word.furigana(format);
}, "");
}
+
+ public async update() {
+ if (this.frozen) return;
+ // unresolve ready
+ this.ready = new Promise(res => this._resolveReady = res);
+
+ // fetch sentence from DB
+ await this.fetch();
+ // parse words into Word
+ await this.updateWords();
+
+ // mark ready again
+ this._resolveReady();
+ }
+
+ public at(term: string) {
+ return this.original.indexOf(term);
+ }
+
+ public async break(location: number) {
+ this.breaks.push(location);
+ await this.update();
+ }
+
+ public async freeze() {
+ this.frozen = true;
+ }
+
+ public async unfreeze() {
+ this.frozen = false;
+ await this.update();
+ }
}