diff options
author | lonkaars <loek@pipeframe.xyz> | 2023-07-12 00:31:12 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2023-07-12 00:31:12 +0200 |
commit | 413501fde6bac909f31ad399781626caa16c8d04 (patch) | |
tree | 7514b0f551a7c41f618d58a50fa56972e6b06170 /api | |
parent | e99ae80f7adc0f0e677381c3cc1549235d3877ab (diff) |
implement parser breaks
Diffstat (limited to 'api')
-rw-r--r-- | api/sentence.ts | 47 |
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(); + } } |