@dabalabs/lang 0.0.4-beta → 0.0.5-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/dabalang.js CHANGED
@@ -237,8 +237,20 @@ var TranslationApplier = class {
237
237
  /** Re-crawls the current DOM — call after content is known to have
238
238
  * changed (e.g. after a route change in an SPA). Not called
239
239
  * automatically; dabalang does one crawl per page load. */
240
+ /** Re-crawls, returning how many text nodes were seen for the first
241
+ * time.
242
+ *
243
+ * That count is the signal a caller needs to decide whether anything
244
+ * actually arrived. Applying a language writes to the DOM, which any
245
+ * MutationObserver watching will see as a change — so "did the DOM
246
+ * mutate" cannot distinguish new content from our own echo, and acting
247
+ * on it would loop. "Did previously-unseen text appear" can: our own
248
+ * writes land on nodes already in sourceByNode, so they count zero. */
240
249
  recrawl() {
241
- this.groups = this.absorb(crawlVisibleText(this.root));
250
+ const crawled = crawlVisibleText(this.root);
251
+ const fresh = crawled.reduce((n, { node }) => this.sourceByNode.has(node) ? n : n + 1, 0);
252
+ this.groups = this.absorb(crawled);
253
+ return fresh;
242
254
  }
243
255
  currentLanguage() {
244
256
  return this.activeLang;
@@ -508,13 +520,7 @@ var NavigationWatcher = class {
508
520
  const onPop = () => this.onRouteMaybeChanged();
509
521
  window.addEventListener("popstate", onPop);
510
522
  this.restorers.push(() => window.removeEventListener("popstate", onPop));
511
- this.observer = new MutationObserver(() => {
512
- if (this.currentPath() !== this.lastPath) {
513
- this.schedule();
514
- } else if (this.passesLeft > 0) {
515
- this.schedule();
516
- }
517
- });
523
+ this.observer = new MutationObserver(() => this.schedule());
518
524
  this.observer.observe(document.body, { childList: true, subtree: true });
519
525
  }
520
526
  currentPath() {
@@ -534,11 +540,10 @@ var NavigationWatcher = class {
534
540
  if (path !== this.lastPath) {
535
541
  this.lastPath = path;
536
542
  this.passesLeft = FOLLOW_UP_PASSES;
537
- this.onNavigated();
538
543
  } else if (this.passesLeft > 0) {
539
544
  this.passesLeft -= 1;
540
- this.onNavigated();
541
545
  }
546
+ this.onNavigated();
542
547
  }, SETTLE_MS);
543
548
  }
544
549
  stop() {
@@ -1454,7 +1459,7 @@ var DabaLang = class {
1454
1459
  if (this.isSiteTranslated(this.activeLang)) return;
1455
1460
  this.retranslating = this.retranslating.catch(() => void 0).then(async () => {
1456
1461
  if (!this.applier || this.activeLang === null) return;
1457
- this.applier.recrawl();
1462
+ if (this.applier.recrawl() === 0) return;
1458
1463
  try {
1459
1464
  await this.applier.applyLanguage(this.activeLang);
1460
1465
  } catch (err) {