Skip to content

Commit

Permalink
Merge pull request #24 from tilde-nlp/feature/whole-page-translation
Browse files Browse the repository at this point in the history
dispatching translation finished event once ALL batches have been translated
  • Loading branch information
alexattt authored Jul 16, 2024
2 parents edd1ae6 + f692b35 commit 4714867
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/js/lib/AsyncTranslator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class AsyncTranslator {
private itemsTranslated: number;
private itemsTotal:number;
private retryTimeout = 1000;
private batchesCount: number;
private translationFinishedEvent = new Event("translation-finished");

constructor (
private readonly websiteService:WebsiteService,
Expand Down Expand Up @@ -193,11 +195,17 @@ class AsyncTranslator {
if (this.itemsTotal === 0) {
return 1
}
if (this.batchesCount === 0 && (this.itemsTranslated / this.itemsTotal) === 1) {
document.dispatchEvent(this.translationFinishedEvent);
}

return this.itemsTranslated / this.itemsTotal
}

private onTranslationItemDiscovered (items: Array<TranslationTextRange>, priority: TranslationPriority) {
const batches = this.getBatches(items)
this.batchesCount = batches.length;

if (batches.length > 0) {
const queueBatches = this.queue.getItems()

Expand All @@ -218,6 +226,10 @@ class AsyncTranslator {

this.onProgress(this.getProgress())
}
else {
this.batchesCount = 0;
this.onProgress(this.getProgress())
}
}

private cancelBatch (batch: Array<ITranslatableItem>) {
Expand Down

0 comments on commit 4714867

Please sign in to comment.