From 413501fde6bac909f31ad399781626caa16c8d04 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Wed, 12 Jul 2023 00:31:12 +0200 Subject: implement parser breaks --- api/sentence.ts | 47 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 8 deletions(-) (limited to 'api') 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 = []; protected query?: SearchSentenceResult; protected original: string = ""; + protected breaks: Array = []; + protected frozen = false; public ready: Promise; 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(); + } } -- cgit v1.2.3