@embedpdf-editor/vue3-chapter-viewer 1.0.7-beta.8 → 1.0.9-beta.1

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/index.js CHANGED
@@ -419,12 +419,17 @@ const st$1 = class e extends BasePlugin {
419
419
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
420
420
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
421
421
  const s2 = this.findChapter(e15.chapterId);
422
- if (s2) {
423
- if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
424
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
425
- });
426
- else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
422
+ if (!s2) continue;
423
+ if (!this.ensureVisibleChapterUrls(s2)) {
424
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
425
+ continue;
427
426
  }
427
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
428
+ let n9 = false;
429
+ const r2 = () => {
430
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
431
+ };
432
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
428
433
  }
429
434
  }
430
435
  prepareChapterForSegmentLoad(e14) {
@@ -458,13 +463,6 @@ const st$1 = class e extends BasePlugin {
458
463
  }
459
464
  if (0 === a2.size) return;
460
465
  const l = this.virtualPageMap.atIndex(r2);
461
- if (l) {
462
- const e15 = this.findChapter(l.chapterId);
463
- if (e15) {
464
- const t3 = Ze$1(e15, l.localPageIndex);
465
- o2(l.chapterId, t3 + 1);
466
- }
467
- }
468
466
  for (const d2 of a2) this.chapterLastUsed.set(d2, n9);
469
467
  const h2 = [...i2.entries()].map(([e15, t3]) => {
470
468
  const [s3, n10] = e15.split(":");
@@ -514,21 +512,21 @@ const st$1 = class e extends BasePlugin {
514
512
  ensureSegmentLoaded(e14, t2) {
515
513
  return this.loadSegmentNow(e14, t2);
516
514
  }
517
- loadSegmentNow(e14, t2) {
515
+ loadSegmentNow(e14, t2, s2) {
518
516
  this.prepareChapterForSegmentLoad(e14);
519
- const s2 = this.findChapter(e14);
520
- if (!s2) return Promise.resolve("error");
521
- const n9 = je$1(s2);
522
- if (!n9) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(s2);
523
- if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
524
- const r2 = n9.segments[t2], a2 = r2.documentId;
525
- if (this.documentManager.provides().isDocumentOpen(a2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
526
- const i2 = this.pendingSegmentLoadPromises.get(a2);
527
- if (i2) return i2;
528
- const o2 = this.startLoadSegment(s2, r2).finally(() => {
529
- this.pendingSegmentLoadPromises.delete(a2);
517
+ const n9 = this.findChapter(e14);
518
+ if (!n9) return Promise.resolve("error");
519
+ const r2 = je$1(n9);
520
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
521
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
522
+ const a2 = r2.segments[t2], i2 = a2.documentId;
523
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
524
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
525
+ if (o2) return o2;
526
+ const l = this.startLoadSegment(n9, a2, s2).finally(() => {
527
+ this.pendingSegmentLoadPromises.delete(i2);
530
528
  });
531
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
529
+ return this.pendingSegmentLoadPromises.set(i2, l), l;
532
530
  }
533
531
  ensureSingleDocumentChapter(e14) {
534
532
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -602,24 +600,27 @@ const st$1 = class e extends BasePlugin {
602
600
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(t2, s2);
603
601
  throw new Error(`Chapter "${t2.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
604
602
  }
605
- async startLoadSegment(t2, s2) {
606
- var n9;
607
- const r2 = t2.chapterId, a2 = (null == s2 ? void 0 : s2.index) ?? 0, i2 = (null == s2 ? void 0 : s2.documentId) ?? r2;
608
- this.chapterErrorMessages.delete(r2), this.anySegmentOpen(r2) || this.updateStatus(r2, "loading"), this.chapterLastUsed.set(r2, Date.now());
603
+ async startLoadSegment(t2, s2, n9) {
604
+ var r2;
605
+ const a2 = t2.chapterId, i2 = (null == s2 ? void 0 : s2.index) ?? 0, o2 = (null == s2 ? void 0 : s2.documentId) ?? a2;
606
+ let l = false;
607
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
609
608
  try {
610
- const e14 = t2.encrypted && (null == (n9 = this.passwordProvider) ? void 0 : n9.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, o2 = await this.resolvePdfPayload(t2, a2), l = this.documentManager.provides(), h2 = "url" in o2 ? l.openDocumentUrl({ documentId: i2, url: o2.url, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }) : l.openDocumentBuffer({ documentId: i2, buffer: o2.buffer, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
611
- h2.wait((t4) => e15(t4), (e16) => t3(e16));
609
+ const e14 = t2.encrypted && (null == (r2 = this.passwordProvider) ? void 0 : r2.getCachedPassword) ? this.passwordProvider.getCachedPassword(a2) : null, h2 = await this.resolvePdfPayload(t2, i2), d2 = this.documentManager.provides(), c2 = "url" in h2 ? d2.openDocumentUrl({ documentId: o2, url: h2.url, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }) : d2.openDocumentBuffer({ documentId: o2, buffer: h2.buffer, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false });
610
+ l || (l = true, null == n9 || n9());
611
+ const u2 = await new Promise((e15, t3) => {
612
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
612
613
  });
613
614
  return await new Promise((e15, t3) => {
614
- d2.task.wait(() => e15(), (e16) => t3(e16));
615
- }), l.isDocumentOpen(i2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
616
- } catch (o2) {
617
- const t3 = this.chapterStatus.get(r2);
615
+ u2.task.wait(() => e15(), (e16) => t3(e16));
616
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
617
+ } catch (h2) {
618
+ const t3 = this.chapterStatus.get(a2);
618
619
  if ("loaded" !== t3 && "password-required" !== t3) {
619
- const t4 = o2 instanceof Error ? o2.message : `Failed to load chapter "${r2}"`;
620
- this.logger.error(e.id, "LoadChapter", t4, o2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", t4);
620
+ const t4 = h2 instanceof Error ? h2.message : `Failed to load chapter "${a2}"`;
621
+ this.logger.error(e.id, "LoadChapter", t4, h2), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", t4);
621
622
  }
622
- return this.chapterStatus.get(r2) ?? "error";
623
+ return this.chapterStatus.get(a2) ?? "error";
623
624
  }
624
625
  }
625
626
  waitForTerminalStatus(e14) {
@@ -1032,12 +1033,17 @@ const xt$1 = class e2 extends BasePlugin {
1032
1033
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
1033
1034
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
1034
1035
  const s2 = this.findChapter(e15.chapterId);
1035
- if (s2) {
1036
- if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
1037
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
1038
- });
1039
- else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
1036
+ if (!s2) continue;
1037
+ if (!this.ensureVisibleChapterUrls(s2)) {
1038
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
1039
+ continue;
1040
1040
  }
1041
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
1042
+ let n9 = false;
1043
+ const r2 = () => {
1044
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
1045
+ };
1046
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
1041
1047
  }
1042
1048
  }
1043
1049
  prepareChapterForSegmentLoad(e14) {
@@ -1071,13 +1077,6 @@ const xt$1 = class e2 extends BasePlugin {
1071
1077
  }
1072
1078
  if (0 === a2.size) return;
1073
1079
  const l = this.virtualPageMap.atIndex(r2);
1074
- if (l) {
1075
- const e15 = this.findChapter(l.chapterId);
1076
- if (e15) {
1077
- const t3 = bt$1(e15, l.localPageIndex);
1078
- o2(l.chapterId, t3 + 1);
1079
- }
1080
- }
1081
1080
  for (const d2 of a2) this.chapterLastUsed.set(d2, n9);
1082
1081
  const h2 = [...i2.entries()].map(([e15, t3]) => {
1083
1082
  const [s3, n10] = e15.split(":");
@@ -1127,21 +1126,21 @@ const xt$1 = class e2 extends BasePlugin {
1127
1126
  ensureSegmentLoaded(e14, t2) {
1128
1127
  return this.loadSegmentNow(e14, t2);
1129
1128
  }
1130
- loadSegmentNow(e14, t2) {
1129
+ loadSegmentNow(e14, t2, s2) {
1131
1130
  this.prepareChapterForSegmentLoad(e14);
1132
- const s2 = this.findChapter(e14);
1133
- if (!s2) return Promise.resolve("error");
1134
- const n9 = Pt$1(s2);
1135
- if (!n9) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(s2);
1136
- if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
1137
- const r2 = n9.segments[t2], a2 = r2.documentId;
1138
- if (this.documentManager.provides().isDocumentOpen(a2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
1139
- const i2 = this.pendingSegmentLoadPromises.get(a2);
1140
- if (i2) return i2;
1141
- const o2 = this.startLoadSegment(s2, r2).finally(() => {
1142
- this.pendingSegmentLoadPromises.delete(a2);
1131
+ const n9 = this.findChapter(e14);
1132
+ if (!n9) return Promise.resolve("error");
1133
+ const r2 = Pt$1(n9);
1134
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
1135
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
1136
+ const a2 = r2.segments[t2], i2 = a2.documentId;
1137
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
1138
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
1139
+ if (o2) return o2;
1140
+ const l = this.startLoadSegment(n9, a2, s2).finally(() => {
1141
+ this.pendingSegmentLoadPromises.delete(i2);
1143
1142
  });
1144
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
1143
+ return this.pendingSegmentLoadPromises.set(i2, l), l;
1145
1144
  }
1146
1145
  ensureSingleDocumentChapter(e14) {
1147
1146
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -1215,24 +1214,27 @@ const xt$1 = class e2 extends BasePlugin {
1215
1214
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(t2, s2);
1216
1215
  throw new Error(`Chapter "${t2.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
1217
1216
  }
1218
- async startLoadSegment(t2, s2) {
1219
- var n9;
1220
- const r2 = t2.chapterId, a2 = (null == s2 ? void 0 : s2.index) ?? 0, i2 = (null == s2 ? void 0 : s2.documentId) ?? r2;
1221
- this.chapterErrorMessages.delete(r2), this.anySegmentOpen(r2) || this.updateStatus(r2, "loading"), this.chapterLastUsed.set(r2, Date.now());
1217
+ async startLoadSegment(t2, s2, n9) {
1218
+ var r2;
1219
+ const a2 = t2.chapterId, i2 = (null == s2 ? void 0 : s2.index) ?? 0, o2 = (null == s2 ? void 0 : s2.documentId) ?? a2;
1220
+ let l = false;
1221
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
1222
1222
  try {
1223
- const e14 = t2.encrypted && (null == (n9 = this.passwordProvider) ? void 0 : n9.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, o2 = await this.resolvePdfPayload(t2, a2), l = this.documentManager.provides(), h2 = "url" in o2 ? l.openDocumentUrl({ documentId: i2, url: o2.url, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }) : l.openDocumentBuffer({ documentId: i2, buffer: o2.buffer, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
1224
- h2.wait((t4) => e15(t4), (e16) => t3(e16));
1223
+ const e14 = t2.encrypted && (null == (r2 = this.passwordProvider) ? void 0 : r2.getCachedPassword) ? this.passwordProvider.getCachedPassword(a2) : null, h2 = await this.resolvePdfPayload(t2, i2), d2 = this.documentManager.provides(), c2 = "url" in h2 ? d2.openDocumentUrl({ documentId: o2, url: h2.url, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }) : d2.openDocumentBuffer({ documentId: o2, buffer: h2.buffer, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false });
1224
+ l || (l = true, null == n9 || n9());
1225
+ const u2 = await new Promise((e15, t3) => {
1226
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
1225
1227
  });
1226
1228
  return await new Promise((e15, t3) => {
1227
- d2.task.wait(() => e15(), (e16) => t3(e16));
1228
- }), l.isDocumentOpen(i2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
1229
- } catch (o2) {
1230
- const t3 = this.chapterStatus.get(r2);
1229
+ u2.task.wait(() => e15(), (e16) => t3(e16));
1230
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
1231
+ } catch (h2) {
1232
+ const t3 = this.chapterStatus.get(a2);
1231
1233
  if ("loaded" !== t3 && "password-required" !== t3) {
1232
- const t4 = o2 instanceof Error ? o2.message : `Failed to load chapter "${r2}"`;
1233
- this.logger.error(e2.id, "LoadChapter", t4, o2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", t4);
1234
+ const t4 = h2 instanceof Error ? h2.message : `Failed to load chapter "${a2}"`;
1235
+ this.logger.error(e2.id, "LoadChapter", t4, h2), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", t4);
1234
1236
  }
1235
- return this.chapterStatus.get(r2) ?? "error";
1237
+ return this.chapterStatus.get(a2) ?? "error";
1236
1238
  }
1237
1239
  }
1238
1240
  waitForTerminalStatus(e14) {
@@ -1835,12 +1837,17 @@ const jt$1 = class e3 extends BasePlugin {
1835
1837
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
1836
1838
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
1837
1839
  const s2 = this.findChapter(e15.chapterId);
1838
- if (s2) {
1839
- if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
1840
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
1841
- });
1842
- else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
1840
+ if (!s2) continue;
1841
+ if (!this.ensureVisibleChapterUrls(s2)) {
1842
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
1843
+ continue;
1843
1844
  }
1845
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
1846
+ let n9 = false;
1847
+ const r2 = () => {
1848
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
1849
+ };
1850
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
1844
1851
  }
1845
1852
  }
1846
1853
  prepareChapterForSegmentLoad(e14) {
@@ -1874,13 +1881,6 @@ const jt$1 = class e3 extends BasePlugin {
1874
1881
  }
1875
1882
  if (0 === a2.size) return;
1876
1883
  const l = this.virtualPageMap.atIndex(r2);
1877
- if (l) {
1878
- const e15 = this.findChapter(l.chapterId);
1879
- if (e15) {
1880
- const t3 = Bt$1(e15, l.localPageIndex);
1881
- o2(l.chapterId, t3 + 1);
1882
- }
1883
- }
1884
1884
  for (const d2 of a2) this.chapterLastUsed.set(d2, n9);
1885
1885
  const h2 = [...i2.entries()].map(([e15, t3]) => {
1886
1886
  const [s3, n10] = e15.split(":");
@@ -1930,21 +1930,21 @@ const jt$1 = class e3 extends BasePlugin {
1930
1930
  ensureSegmentLoaded(e14, t2) {
1931
1931
  return this.loadSegmentNow(e14, t2);
1932
1932
  }
1933
- loadSegmentNow(e14, t2) {
1933
+ loadSegmentNow(e14, t2, s2) {
1934
1934
  this.prepareChapterForSegmentLoad(e14);
1935
- const s2 = this.findChapter(e14);
1936
- if (!s2) return Promise.resolve("error");
1937
- const n9 = zt$1(s2);
1938
- if (!n9) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(s2);
1939
- if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
1940
- const r2 = n9.segments[t2], a2 = r2.documentId;
1941
- if (this.documentManager.provides().isDocumentOpen(a2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
1942
- const i2 = this.pendingSegmentLoadPromises.get(a2);
1943
- if (i2) return i2;
1944
- const o2 = this.startLoadSegment(s2, r2).finally(() => {
1945
- this.pendingSegmentLoadPromises.delete(a2);
1935
+ const n9 = this.findChapter(e14);
1936
+ if (!n9) return Promise.resolve("error");
1937
+ const r2 = zt$1(n9);
1938
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
1939
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
1940
+ const a2 = r2.segments[t2], i2 = a2.documentId;
1941
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
1942
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
1943
+ if (o2) return o2;
1944
+ const l = this.startLoadSegment(n9, a2, s2).finally(() => {
1945
+ this.pendingSegmentLoadPromises.delete(i2);
1946
1946
  });
1947
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
1947
+ return this.pendingSegmentLoadPromises.set(i2, l), l;
1948
1948
  }
1949
1949
  ensureSingleDocumentChapter(e14) {
1950
1950
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -2018,24 +2018,27 @@ const jt$1 = class e3 extends BasePlugin {
2018
2018
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(t2, s2);
2019
2019
  throw new Error(`Chapter "${t2.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
2020
2020
  }
2021
- async startLoadSegment(t2, s2) {
2022
- var n9;
2023
- const r2 = t2.chapterId, a2 = (null == s2 ? void 0 : s2.index) ?? 0, i2 = (null == s2 ? void 0 : s2.documentId) ?? r2;
2024
- this.chapterErrorMessages.delete(r2), this.anySegmentOpen(r2) || this.updateStatus(r2, "loading"), this.chapterLastUsed.set(r2, Date.now());
2021
+ async startLoadSegment(t2, s2, n9) {
2022
+ var r2;
2023
+ const a2 = t2.chapterId, i2 = (null == s2 ? void 0 : s2.index) ?? 0, o2 = (null == s2 ? void 0 : s2.documentId) ?? a2;
2024
+ let l = false;
2025
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
2025
2026
  try {
2026
- const e14 = t2.encrypted && (null == (n9 = this.passwordProvider) ? void 0 : n9.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, o2 = await this.resolvePdfPayload(t2, a2), l = this.documentManager.provides(), h2 = "url" in o2 ? l.openDocumentUrl({ documentId: i2, url: o2.url, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }) : l.openDocumentBuffer({ documentId: i2, buffer: o2.buffer, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
2027
- h2.wait((t4) => e15(t4), (e16) => t3(e16));
2027
+ const e14 = t2.encrypted && (null == (r2 = this.passwordProvider) ? void 0 : r2.getCachedPassword) ? this.passwordProvider.getCachedPassword(a2) : null, h2 = await this.resolvePdfPayload(t2, i2), d2 = this.documentManager.provides(), c2 = "url" in h2 ? d2.openDocumentUrl({ documentId: o2, url: h2.url, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }) : d2.openDocumentBuffer({ documentId: o2, buffer: h2.buffer, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false });
2028
+ l || (l = true, null == n9 || n9());
2029
+ const u2 = await new Promise((e15, t3) => {
2030
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
2028
2031
  });
2029
2032
  return await new Promise((e15, t3) => {
2030
- d2.task.wait(() => e15(), (e16) => t3(e16));
2031
- }), l.isDocumentOpen(i2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
2032
- } catch (o2) {
2033
- const t3 = this.chapterStatus.get(r2);
2033
+ u2.task.wait(() => e15(), (e16) => t3(e16));
2034
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
2035
+ } catch (h2) {
2036
+ const t3 = this.chapterStatus.get(a2);
2034
2037
  if ("loaded" !== t3 && "password-required" !== t3) {
2035
- const t4 = o2 instanceof Error ? o2.message : `Failed to load chapter "${r2}"`;
2036
- this.logger.error(e3.id, "LoadChapter", t4, o2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", t4);
2038
+ const t4 = h2 instanceof Error ? h2.message : `Failed to load chapter "${a2}"`;
2039
+ this.logger.error(e3.id, "LoadChapter", t4, h2), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", t4);
2037
2040
  }
2038
- return this.chapterStatus.get(r2) ?? "error";
2041
+ return this.chapterStatus.get(a2) ?? "error";
2039
2042
  }
2040
2043
  }
2041
2044
  waitForTerminalStatus(e14) {
@@ -2447,12 +2450,17 @@ const ms = class e4 extends BasePlugin {
2447
2450
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
2448
2451
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
2449
2452
  const s2 = this.findChapter(e15.chapterId);
2450
- if (s2) {
2451
- if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
2452
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
2453
- });
2454
- else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
2453
+ if (!s2) continue;
2454
+ if (!this.ensureVisibleChapterUrls(s2)) {
2455
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
2456
+ continue;
2455
2457
  }
2458
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
2459
+ let n9 = false;
2460
+ const r2 = () => {
2461
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
2462
+ };
2463
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
2456
2464
  }
2457
2465
  }
2458
2466
  prepareChapterForSegmentLoad(e14) {
@@ -2486,13 +2494,6 @@ const ms = class e4 extends BasePlugin {
2486
2494
  }
2487
2495
  if (0 === a2.size) return;
2488
2496
  const l = this.virtualPageMap.atIndex(r2);
2489
- if (l) {
2490
- const e15 = this.findChapter(l.chapterId);
2491
- if (e15) {
2492
- const t3 = hs(e15, l.localPageIndex);
2493
- o2(l.chapterId, t3 + 1);
2494
- }
2495
- }
2496
2497
  for (const d2 of a2) this.chapterLastUsed.set(d2, n9);
2497
2498
  const h2 = [...i2.entries()].map(([e15, t3]) => {
2498
2499
  const [s3, n10] = e15.split(":");
@@ -2542,21 +2543,21 @@ const ms = class e4 extends BasePlugin {
2542
2543
  ensureSegmentLoaded(e14, t2) {
2543
2544
  return this.loadSegmentNow(e14, t2);
2544
2545
  }
2545
- loadSegmentNow(e14, t2) {
2546
+ loadSegmentNow(e14, t2, s2) {
2546
2547
  this.prepareChapterForSegmentLoad(e14);
2547
- const s2 = this.findChapter(e14);
2548
- if (!s2) return Promise.resolve("error");
2549
- const n9 = os(s2);
2550
- if (!n9) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(s2);
2551
- if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
2552
- const r2 = n9.segments[t2], a2 = r2.documentId;
2553
- if (this.documentManager.provides().isDocumentOpen(a2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
2554
- const i2 = this.pendingSegmentLoadPromises.get(a2);
2555
- if (i2) return i2;
2556
- const o2 = this.startLoadSegment(s2, r2).finally(() => {
2557
- this.pendingSegmentLoadPromises.delete(a2);
2548
+ const n9 = this.findChapter(e14);
2549
+ if (!n9) return Promise.resolve("error");
2550
+ const r2 = os(n9);
2551
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
2552
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
2553
+ const a2 = r2.segments[t2], i2 = a2.documentId;
2554
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
2555
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
2556
+ if (o2) return o2;
2557
+ const l = this.startLoadSegment(n9, a2, s2).finally(() => {
2558
+ this.pendingSegmentLoadPromises.delete(i2);
2558
2559
  });
2559
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
2560
+ return this.pendingSegmentLoadPromises.set(i2, l), l;
2560
2561
  }
2561
2562
  ensureSingleDocumentChapter(e14) {
2562
2563
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -2630,24 +2631,27 @@ const ms = class e4 extends BasePlugin {
2630
2631
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(t2, s2);
2631
2632
  throw new Error(`Chapter "${t2.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
2632
2633
  }
2633
- async startLoadSegment(t2, s2) {
2634
- var n9;
2635
- const r2 = t2.chapterId, a2 = (null == s2 ? void 0 : s2.index) ?? 0, i2 = (null == s2 ? void 0 : s2.documentId) ?? r2;
2636
- this.chapterErrorMessages.delete(r2), this.anySegmentOpen(r2) || this.updateStatus(r2, "loading"), this.chapterLastUsed.set(r2, Date.now());
2634
+ async startLoadSegment(t2, s2, n9) {
2635
+ var r2;
2636
+ const a2 = t2.chapterId, i2 = (null == s2 ? void 0 : s2.index) ?? 0, o2 = (null == s2 ? void 0 : s2.documentId) ?? a2;
2637
+ let l = false;
2638
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
2637
2639
  try {
2638
- const e14 = t2.encrypted && (null == (n9 = this.passwordProvider) ? void 0 : n9.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, o2 = await this.resolvePdfPayload(t2, a2), l = this.documentManager.provides(), h2 = "url" in o2 ? l.openDocumentUrl({ documentId: i2, url: o2.url, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }) : l.openDocumentBuffer({ documentId: i2, buffer: o2.buffer, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
2639
- h2.wait((t4) => e15(t4), (e16) => t3(e16));
2640
+ const e14 = t2.encrypted && (null == (r2 = this.passwordProvider) ? void 0 : r2.getCachedPassword) ? this.passwordProvider.getCachedPassword(a2) : null, h2 = await this.resolvePdfPayload(t2, i2), d2 = this.documentManager.provides(), c2 = "url" in h2 ? d2.openDocumentUrl({ documentId: o2, url: h2.url, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }) : d2.openDocumentBuffer({ documentId: o2, buffer: h2.buffer, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false });
2641
+ l || (l = true, null == n9 || n9());
2642
+ const u2 = await new Promise((e15, t3) => {
2643
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
2640
2644
  });
2641
2645
  return await new Promise((e15, t3) => {
2642
- d2.task.wait(() => e15(), (e16) => t3(e16));
2643
- }), l.isDocumentOpen(i2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
2644
- } catch (o2) {
2645
- const t3 = this.chapterStatus.get(r2);
2646
+ u2.task.wait(() => e15(), (e16) => t3(e16));
2647
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
2648
+ } catch (h2) {
2649
+ const t3 = this.chapterStatus.get(a2);
2646
2650
  if ("loaded" !== t3 && "password-required" !== t3) {
2647
- const t4 = o2 instanceof Error ? o2.message : `Failed to load chapter "${r2}"`;
2648
- this.logger.error(e4.id, "LoadChapter", t4, o2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", t4);
2651
+ const t4 = h2 instanceof Error ? h2.message : `Failed to load chapter "${a2}"`;
2652
+ this.logger.error(e4.id, "LoadChapter", t4, h2), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", t4);
2649
2653
  }
2650
- return this.chapterStatus.get(r2) ?? "error";
2654
+ return this.chapterStatus.get(a2) ?? "error";
2651
2655
  }
2652
2656
  }
2653
2657
  waitForTerminalStatus(e14) {
@@ -3363,12 +3367,17 @@ const zs$1 = class e6 extends BasePlugin {
3363
3367
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
3364
3368
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
3365
3369
  const s2 = this.findChapter(e15.chapterId);
3366
- if (s2) {
3367
- if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
3368
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
3369
- });
3370
- else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
3370
+ if (!s2) continue;
3371
+ if (!this.ensureVisibleChapterUrls(s2)) {
3372
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
3373
+ continue;
3371
3374
  }
3375
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
3376
+ let n9 = false;
3377
+ const r2 = () => {
3378
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
3379
+ };
3380
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
3372
3381
  }
3373
3382
  }
3374
3383
  prepareChapterForSegmentLoad(e14) {
@@ -3402,13 +3411,6 @@ const zs$1 = class e6 extends BasePlugin {
3402
3411
  }
3403
3412
  if (0 === a2.size) return;
3404
3413
  const l = this.virtualPageMap.atIndex(r2);
3405
- if (l) {
3406
- const e15 = this.findChapter(l.chapterId);
3407
- if (e15) {
3408
- const t3 = Ns$1(e15, l.localPageIndex);
3409
- o2(l.chapterId, t3 + 1);
3410
- }
3411
- }
3412
3414
  for (const d2 of a2) this.chapterLastUsed.set(d2, n9);
3413
3415
  const h2 = [...i2.entries()].map(([e15, t3]) => {
3414
3416
  const [s3, n10] = e15.split(":");
@@ -3458,21 +3460,21 @@ const zs$1 = class e6 extends BasePlugin {
3458
3460
  ensureSegmentLoaded(e14, t2) {
3459
3461
  return this.loadSegmentNow(e14, t2);
3460
3462
  }
3461
- loadSegmentNow(e14, t2) {
3463
+ loadSegmentNow(e14, t2, s2) {
3462
3464
  this.prepareChapterForSegmentLoad(e14);
3463
- const s2 = this.findChapter(e14);
3464
- if (!s2) return Promise.resolve("error");
3465
- const n9 = Es$1(s2);
3466
- if (!n9) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(s2);
3467
- if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
3468
- const r2 = n9.segments[t2], a2 = r2.documentId;
3469
- if (this.documentManager.provides().isDocumentOpen(a2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
3470
- const i2 = this.pendingSegmentLoadPromises.get(a2);
3471
- if (i2) return i2;
3472
- const o2 = this.startLoadSegment(s2, r2).finally(() => {
3473
- this.pendingSegmentLoadPromises.delete(a2);
3465
+ const n9 = this.findChapter(e14);
3466
+ if (!n9) return Promise.resolve("error");
3467
+ const r2 = Es$1(n9);
3468
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
3469
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
3470
+ const a2 = r2.segments[t2], i2 = a2.documentId;
3471
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
3472
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
3473
+ if (o2) return o2;
3474
+ const l = this.startLoadSegment(n9, a2, s2).finally(() => {
3475
+ this.pendingSegmentLoadPromises.delete(i2);
3474
3476
  });
3475
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
3477
+ return this.pendingSegmentLoadPromises.set(i2, l), l;
3476
3478
  }
3477
3479
  ensureSingleDocumentChapter(e14) {
3478
3480
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -3546,24 +3548,27 @@ const zs$1 = class e6 extends BasePlugin {
3546
3548
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(t2, s2);
3547
3549
  throw new Error(`Chapter "${t2.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
3548
3550
  }
3549
- async startLoadSegment(t2, s2) {
3550
- var n9;
3551
- const r2 = t2.chapterId, a2 = (null == s2 ? void 0 : s2.index) ?? 0, i2 = (null == s2 ? void 0 : s2.documentId) ?? r2;
3552
- this.chapterErrorMessages.delete(r2), this.anySegmentOpen(r2) || this.updateStatus(r2, "loading"), this.chapterLastUsed.set(r2, Date.now());
3551
+ async startLoadSegment(t2, s2, n9) {
3552
+ var r2;
3553
+ const a2 = t2.chapterId, i2 = (null == s2 ? void 0 : s2.index) ?? 0, o2 = (null == s2 ? void 0 : s2.documentId) ?? a2;
3554
+ let l = false;
3555
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
3553
3556
  try {
3554
- const e14 = t2.encrypted && (null == (n9 = this.passwordProvider) ? void 0 : n9.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, o2 = await this.resolvePdfPayload(t2, a2), l = this.documentManager.provides(), h2 = "url" in o2 ? l.openDocumentUrl({ documentId: i2, url: o2.url, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }) : l.openDocumentBuffer({ documentId: i2, buffer: o2.buffer, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
3555
- h2.wait((t4) => e15(t4), (e16) => t3(e16));
3557
+ const e14 = t2.encrypted && (null == (r2 = this.passwordProvider) ? void 0 : r2.getCachedPassword) ? this.passwordProvider.getCachedPassword(a2) : null, h2 = await this.resolvePdfPayload(t2, i2), d2 = this.documentManager.provides(), c2 = "url" in h2 ? d2.openDocumentUrl({ documentId: o2, url: h2.url, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }) : d2.openDocumentBuffer({ documentId: o2, buffer: h2.buffer, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false });
3558
+ l || (l = true, null == n9 || n9());
3559
+ const u2 = await new Promise((e15, t3) => {
3560
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
3556
3561
  });
3557
3562
  return await new Promise((e15, t3) => {
3558
- d2.task.wait(() => e15(), (e16) => t3(e16));
3559
- }), l.isDocumentOpen(i2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
3560
- } catch (o2) {
3561
- const t3 = this.chapterStatus.get(r2);
3563
+ u2.task.wait(() => e15(), (e16) => t3(e16));
3564
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
3565
+ } catch (h2) {
3566
+ const t3 = this.chapterStatus.get(a2);
3562
3567
  if ("loaded" !== t3 && "password-required" !== t3) {
3563
- const t4 = o2 instanceof Error ? o2.message : `Failed to load chapter "${r2}"`;
3564
- this.logger.error(e6.id, "LoadChapter", t4, o2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", t4);
3568
+ const t4 = h2 instanceof Error ? h2.message : `Failed to load chapter "${a2}"`;
3569
+ this.logger.error(e6.id, "LoadChapter", t4, h2), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", t4);
3565
3570
  }
3566
- return this.chapterStatus.get(r2) ?? "error";
3571
+ return this.chapterStatus.get(a2) ?? "error";
3567
3572
  }
3568
3573
  }
3569
3574
  waitForTerminalStatus(e14) {
@@ -3975,12 +3980,17 @@ const ln = class e7 extends BasePlugin {
3975
3980
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
3976
3981
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
3977
3982
  const s2 = this.findChapter(e15.chapterId);
3978
- if (s2) {
3979
- if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
3980
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
3981
- });
3982
- else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
3983
+ if (!s2) continue;
3984
+ if (!this.ensureVisibleChapterUrls(s2)) {
3985
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
3986
+ continue;
3983
3987
  }
3988
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
3989
+ let n9 = false;
3990
+ const r2 = () => {
3991
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
3992
+ };
3993
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
3984
3994
  }
3985
3995
  }
3986
3996
  prepareChapterForSegmentLoad(e14) {
@@ -4014,13 +4024,6 @@ const ln = class e7 extends BasePlugin {
4014
4024
  }
4015
4025
  if (0 === a2.size) return;
4016
4026
  const l = this.virtualPageMap.atIndex(r2);
4017
- if (l) {
4018
- const e15 = this.findChapter(l.chapterId);
4019
- if (e15) {
4020
- const t3 = tn(e15, l.localPageIndex);
4021
- o2(l.chapterId, t3 + 1);
4022
- }
4023
- }
4024
4027
  for (const d2 of a2) this.chapterLastUsed.set(d2, n9);
4025
4028
  const h2 = [...i2.entries()].map(([e15, t3]) => {
4026
4029
  const [s3, n10] = e15.split(":");
@@ -4070,21 +4073,21 @@ const ln = class e7 extends BasePlugin {
4070
4073
  ensureSegmentLoaded(e14, t2) {
4071
4074
  return this.loadSegmentNow(e14, t2);
4072
4075
  }
4073
- loadSegmentNow(e14, t2) {
4076
+ loadSegmentNow(e14, t2, s2) {
4074
4077
  this.prepareChapterForSegmentLoad(e14);
4075
- const s2 = this.findChapter(e14);
4076
- if (!s2) return Promise.resolve("error");
4077
- const n9 = Js(s2);
4078
- if (!n9) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(s2);
4079
- if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
4080
- const r2 = n9.segments[t2], a2 = r2.documentId;
4081
- if (this.documentManager.provides().isDocumentOpen(a2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
4082
- const i2 = this.pendingSegmentLoadPromises.get(a2);
4083
- if (i2) return i2;
4084
- const o2 = this.startLoadSegment(s2, r2).finally(() => {
4085
- this.pendingSegmentLoadPromises.delete(a2);
4078
+ const n9 = this.findChapter(e14);
4079
+ if (!n9) return Promise.resolve("error");
4080
+ const r2 = Js(n9);
4081
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
4082
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
4083
+ const a2 = r2.segments[t2], i2 = a2.documentId;
4084
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
4085
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
4086
+ if (o2) return o2;
4087
+ const l = this.startLoadSegment(n9, a2, s2).finally(() => {
4088
+ this.pendingSegmentLoadPromises.delete(i2);
4086
4089
  });
4087
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
4090
+ return this.pendingSegmentLoadPromises.set(i2, l), l;
4088
4091
  }
4089
4092
  ensureSingleDocumentChapter(e14) {
4090
4093
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -4158,24 +4161,27 @@ const ln = class e7 extends BasePlugin {
4158
4161
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(t2, s2);
4159
4162
  throw new Error(`Chapter "${t2.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
4160
4163
  }
4161
- async startLoadSegment(t2, s2) {
4162
- var n9;
4163
- const r2 = t2.chapterId, a2 = (null == s2 ? void 0 : s2.index) ?? 0, i2 = (null == s2 ? void 0 : s2.documentId) ?? r2;
4164
- this.chapterErrorMessages.delete(r2), this.anySegmentOpen(r2) || this.updateStatus(r2, "loading"), this.chapterLastUsed.set(r2, Date.now());
4164
+ async startLoadSegment(t2, s2, n9) {
4165
+ var r2;
4166
+ const a2 = t2.chapterId, i2 = (null == s2 ? void 0 : s2.index) ?? 0, o2 = (null == s2 ? void 0 : s2.documentId) ?? a2;
4167
+ let l = false;
4168
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
4165
4169
  try {
4166
- const e14 = t2.encrypted && (null == (n9 = this.passwordProvider) ? void 0 : n9.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, o2 = await this.resolvePdfPayload(t2, a2), l = this.documentManager.provides(), h2 = "url" in o2 ? l.openDocumentUrl({ documentId: i2, url: o2.url, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }) : l.openDocumentBuffer({ documentId: i2, buffer: o2.buffer, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
4167
- h2.wait((t4) => e15(t4), (e16) => t3(e16));
4170
+ const e14 = t2.encrypted && (null == (r2 = this.passwordProvider) ? void 0 : r2.getCachedPassword) ? this.passwordProvider.getCachedPassword(a2) : null, h2 = await this.resolvePdfPayload(t2, i2), d2 = this.documentManager.provides(), c2 = "url" in h2 ? d2.openDocumentUrl({ documentId: o2, url: h2.url, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }) : d2.openDocumentBuffer({ documentId: o2, buffer: h2.buffer, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false });
4171
+ l || (l = true, null == n9 || n9());
4172
+ const u2 = await new Promise((e15, t3) => {
4173
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
4168
4174
  });
4169
4175
  return await new Promise((e15, t3) => {
4170
- d2.task.wait(() => e15(), (e16) => t3(e16));
4171
- }), l.isDocumentOpen(i2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
4172
- } catch (o2) {
4173
- const t3 = this.chapterStatus.get(r2);
4176
+ u2.task.wait(() => e15(), (e16) => t3(e16));
4177
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
4178
+ } catch (h2) {
4179
+ const t3 = this.chapterStatus.get(a2);
4174
4180
  if ("loaded" !== t3 && "password-required" !== t3) {
4175
- const t4 = o2 instanceof Error ? o2.message : `Failed to load chapter "${r2}"`;
4176
- this.logger.error(e7.id, "LoadChapter", t4, o2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", t4);
4181
+ const t4 = h2 instanceof Error ? h2.message : `Failed to load chapter "${a2}"`;
4182
+ this.logger.error(e7.id, "LoadChapter", t4, h2), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", t4);
4177
4183
  }
4178
- return this.chapterStatus.get(r2) ?? "error";
4184
+ return this.chapterStatus.get(a2) ?? "error";
4179
4185
  }
4180
4186
  }
4181
4187
  waitForTerminalStatus(e14) {
@@ -5248,12 +5254,17 @@ const pr = class e9 extends BasePlugin {
5248
5254
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
5249
5255
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
5250
5256
  const s2 = this.findChapter(e15.chapterId);
5251
- if (s2) {
5252
- if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
5253
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
5254
- });
5255
- else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
5257
+ if (!s2) continue;
5258
+ if (!this.ensureVisibleChapterUrls(s2)) {
5259
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
5260
+ continue;
5256
5261
  }
5262
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
5263
+ let n9 = false;
5264
+ const r2 = () => {
5265
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
5266
+ };
5267
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
5257
5268
  }
5258
5269
  }
5259
5270
  prepareChapterForSegmentLoad(e14) {
@@ -5287,13 +5298,6 @@ const pr = class e9 extends BasePlugin {
5287
5298
  }
5288
5299
  if (0 === a2.size) return;
5289
5300
  const l = this.virtualPageMap.atIndex(r2);
5290
- if (l) {
5291
- const e15 = this.findChapter(l.chapterId);
5292
- if (e15) {
5293
- const t3 = or(e15, l.localPageIndex);
5294
- o2(l.chapterId, t3 + 1);
5295
- }
5296
- }
5297
5301
  for (const d2 of a2) this.chapterLastUsed.set(d2, n9);
5298
5302
  const h2 = [...i2.entries()].map(([e15, t3]) => {
5299
5303
  const [s3, n10] = e15.split(":");
@@ -5343,21 +5347,21 @@ const pr = class e9 extends BasePlugin {
5343
5347
  ensureSegmentLoaded(e14, t2) {
5344
5348
  return this.loadSegmentNow(e14, t2);
5345
5349
  }
5346
- loadSegmentNow(e14, t2) {
5350
+ loadSegmentNow(e14, t2, s2) {
5347
5351
  this.prepareChapterForSegmentLoad(e14);
5348
- const s2 = this.findChapter(e14);
5349
- if (!s2) return Promise.resolve("error");
5350
- const n9 = ar(s2);
5351
- if (!n9) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(s2);
5352
- if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
5353
- const r2 = n9.segments[t2], a2 = r2.documentId;
5354
- if (this.documentManager.provides().isDocumentOpen(a2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
5355
- const i2 = this.pendingSegmentLoadPromises.get(a2);
5356
- if (i2) return i2;
5357
- const o2 = this.startLoadSegment(s2, r2).finally(() => {
5358
- this.pendingSegmentLoadPromises.delete(a2);
5352
+ const n9 = this.findChapter(e14);
5353
+ if (!n9) return Promise.resolve("error");
5354
+ const r2 = ar(n9);
5355
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
5356
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
5357
+ const a2 = r2.segments[t2], i2 = a2.documentId;
5358
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
5359
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
5360
+ if (o2) return o2;
5361
+ const l = this.startLoadSegment(n9, a2, s2).finally(() => {
5362
+ this.pendingSegmentLoadPromises.delete(i2);
5359
5363
  });
5360
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
5364
+ return this.pendingSegmentLoadPromises.set(i2, l), l;
5361
5365
  }
5362
5366
  ensureSingleDocumentChapter(e14) {
5363
5367
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -5431,24 +5435,27 @@ const pr = class e9 extends BasePlugin {
5431
5435
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(t2, s2);
5432
5436
  throw new Error(`Chapter "${t2.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
5433
5437
  }
5434
- async startLoadSegment(t2, s2) {
5435
- var n9;
5436
- const r2 = t2.chapterId, a2 = (null == s2 ? void 0 : s2.index) ?? 0, i2 = (null == s2 ? void 0 : s2.documentId) ?? r2;
5437
- this.chapterErrorMessages.delete(r2), this.anySegmentOpen(r2) || this.updateStatus(r2, "loading"), this.chapterLastUsed.set(r2, Date.now());
5438
+ async startLoadSegment(t2, s2, n9) {
5439
+ var r2;
5440
+ const a2 = t2.chapterId, i2 = (null == s2 ? void 0 : s2.index) ?? 0, o2 = (null == s2 ? void 0 : s2.documentId) ?? a2;
5441
+ let l = false;
5442
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
5438
5443
  try {
5439
- const e14 = t2.encrypted && (null == (n9 = this.passwordProvider) ? void 0 : n9.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, o2 = await this.resolvePdfPayload(t2, a2), l = this.documentManager.provides(), h2 = "url" in o2 ? l.openDocumentUrl({ documentId: i2, url: o2.url, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }) : l.openDocumentBuffer({ documentId: i2, buffer: o2.buffer, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
5440
- h2.wait((t4) => e15(t4), (e16) => t3(e16));
5444
+ const e14 = t2.encrypted && (null == (r2 = this.passwordProvider) ? void 0 : r2.getCachedPassword) ? this.passwordProvider.getCachedPassword(a2) : null, h2 = await this.resolvePdfPayload(t2, i2), d2 = this.documentManager.provides(), c2 = "url" in h2 ? d2.openDocumentUrl({ documentId: o2, url: h2.url, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }) : d2.openDocumentBuffer({ documentId: o2, buffer: h2.buffer, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false });
5445
+ l || (l = true, null == n9 || n9());
5446
+ const u2 = await new Promise((e15, t3) => {
5447
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
5441
5448
  });
5442
5449
  return await new Promise((e15, t3) => {
5443
- d2.task.wait(() => e15(), (e16) => t3(e16));
5444
- }), l.isDocumentOpen(i2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
5445
- } catch (o2) {
5446
- const t3 = this.chapterStatus.get(r2);
5450
+ u2.task.wait(() => e15(), (e16) => t3(e16));
5451
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
5452
+ } catch (h2) {
5453
+ const t3 = this.chapterStatus.get(a2);
5447
5454
  if ("loaded" !== t3 && "password-required" !== t3) {
5448
- const t4 = o2 instanceof Error ? o2.message : `Failed to load chapter "${r2}"`;
5449
- this.logger.error(e9.id, "LoadChapter", t4, o2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", t4);
5455
+ const t4 = h2 instanceof Error ? h2.message : `Failed to load chapter "${a2}"`;
5456
+ this.logger.error(e9.id, "LoadChapter", t4, h2), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", t4);
5450
5457
  }
5451
- return this.chapterStatus.get(r2) ?? "error";
5458
+ return this.chapterStatus.get(a2) ?? "error";
5452
5459
  }
5453
5460
  }
5454
5461
  waitForTerminalStatus(e14) {
@@ -6072,12 +6079,17 @@ const Tr = class e10 extends BasePlugin {
6072
6079
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
6073
6080
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
6074
6081
  const s2 = this.findChapter(e15.chapterId);
6075
- if (s2) {
6076
- if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
6077
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
6078
- });
6079
- else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
6082
+ if (!s2) continue;
6083
+ if (!this.ensureVisibleChapterUrls(s2)) {
6084
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
6085
+ continue;
6080
6086
  }
6087
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
6088
+ let n9 = false;
6089
+ const r2 = () => {
6090
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
6091
+ };
6092
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
6081
6093
  }
6082
6094
  }
6083
6095
  prepareChapterForSegmentLoad(e14) {
@@ -6111,13 +6123,6 @@ const Tr = class e10 extends BasePlugin {
6111
6123
  }
6112
6124
  if (0 === a2.size) return;
6113
6125
  const l = this.virtualPageMap.atIndex(r2);
6114
- if (l) {
6115
- const e15 = this.findChapter(l.chapterId);
6116
- if (e15) {
6117
- const t3 = kr(e15, l.localPageIndex);
6118
- o2(l.chapterId, t3 + 1);
6119
- }
6120
- }
6121
6126
  for (const d2 of a2) this.chapterLastUsed.set(d2, n9);
6122
6127
  const h2 = [...i2.entries()].map(([e15, t3]) => {
6123
6128
  const [s3, n10] = e15.split(":");
@@ -6167,21 +6172,21 @@ const Tr = class e10 extends BasePlugin {
6167
6172
  ensureSegmentLoaded(e14, t2) {
6168
6173
  return this.loadSegmentNow(e14, t2);
6169
6174
  }
6170
- loadSegmentNow(e14, t2) {
6175
+ loadSegmentNow(e14, t2, s2) {
6171
6176
  this.prepareChapterForSegmentLoad(e14);
6172
- const s2 = this.findChapter(e14);
6173
- if (!s2) return Promise.resolve("error");
6174
- const n9 = Lr(s2);
6175
- if (!n9) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(s2);
6176
- if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
6177
- const r2 = n9.segments[t2], a2 = r2.documentId;
6178
- if (this.documentManager.provides().isDocumentOpen(a2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
6179
- const i2 = this.pendingSegmentLoadPromises.get(a2);
6180
- if (i2) return i2;
6181
- const o2 = this.startLoadSegment(s2, r2).finally(() => {
6182
- this.pendingSegmentLoadPromises.delete(a2);
6177
+ const n9 = this.findChapter(e14);
6178
+ if (!n9) return Promise.resolve("error");
6179
+ const r2 = Lr(n9);
6180
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
6181
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
6182
+ const a2 = r2.segments[t2], i2 = a2.documentId;
6183
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
6184
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
6185
+ if (o2) return o2;
6186
+ const l = this.startLoadSegment(n9, a2, s2).finally(() => {
6187
+ this.pendingSegmentLoadPromises.delete(i2);
6183
6188
  });
6184
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
6189
+ return this.pendingSegmentLoadPromises.set(i2, l), l;
6185
6190
  }
6186
6191
  ensureSingleDocumentChapter(e14) {
6187
6192
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -6255,24 +6260,27 @@ const Tr = class e10 extends BasePlugin {
6255
6260
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(t2, s2);
6256
6261
  throw new Error(`Chapter "${t2.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
6257
6262
  }
6258
- async startLoadSegment(t2, s2) {
6259
- var n9;
6260
- const r2 = t2.chapterId, a2 = (null == s2 ? void 0 : s2.index) ?? 0, i2 = (null == s2 ? void 0 : s2.documentId) ?? r2;
6261
- this.chapterErrorMessages.delete(r2), this.anySegmentOpen(r2) || this.updateStatus(r2, "loading"), this.chapterLastUsed.set(r2, Date.now());
6263
+ async startLoadSegment(t2, s2, n9) {
6264
+ var r2;
6265
+ const a2 = t2.chapterId, i2 = (null == s2 ? void 0 : s2.index) ?? 0, o2 = (null == s2 ? void 0 : s2.documentId) ?? a2;
6266
+ let l = false;
6267
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
6262
6268
  try {
6263
- const e14 = t2.encrypted && (null == (n9 = this.passwordProvider) ? void 0 : n9.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, o2 = await this.resolvePdfPayload(t2, a2), l = this.documentManager.provides(), h2 = "url" in o2 ? l.openDocumentUrl({ documentId: i2, url: o2.url, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }) : l.openDocumentBuffer({ documentId: i2, buffer: o2.buffer, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
6264
- h2.wait((t4) => e15(t4), (e16) => t3(e16));
6269
+ const e14 = t2.encrypted && (null == (r2 = this.passwordProvider) ? void 0 : r2.getCachedPassword) ? this.passwordProvider.getCachedPassword(a2) : null, h2 = await this.resolvePdfPayload(t2, i2), d2 = this.documentManager.provides(), c2 = "url" in h2 ? d2.openDocumentUrl({ documentId: o2, url: h2.url, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }) : d2.openDocumentBuffer({ documentId: o2, buffer: h2.buffer, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false });
6270
+ l || (l = true, null == n9 || n9());
6271
+ const u2 = await new Promise((e15, t3) => {
6272
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
6265
6273
  });
6266
6274
  return await new Promise((e15, t3) => {
6267
- d2.task.wait(() => e15(), (e16) => t3(e16));
6268
- }), l.isDocumentOpen(i2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
6269
- } catch (o2) {
6270
- const t3 = this.chapterStatus.get(r2);
6275
+ u2.task.wait(() => e15(), (e16) => t3(e16));
6276
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
6277
+ } catch (h2) {
6278
+ const t3 = this.chapterStatus.get(a2);
6271
6279
  if ("loaded" !== t3 && "password-required" !== t3) {
6272
- const t4 = o2 instanceof Error ? o2.message : `Failed to load chapter "${r2}"`;
6273
- this.logger.error(e10.id, "LoadChapter", t4, o2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", t4);
6280
+ const t4 = h2 instanceof Error ? h2.message : `Failed to load chapter "${a2}"`;
6281
+ this.logger.error(e10.id, "LoadChapter", t4, h2), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", t4);
6274
6282
  }
6275
- return this.chapterStatus.get(r2) ?? "error";
6283
+ return this.chapterStatus.get(a2) ?? "error";
6276
6284
  }
6277
6285
  }
6278
6286
  waitForTerminalStatus(e14) {
@@ -6684,12 +6692,17 @@ const sa = class e11 extends BasePlugin {
6684
6692
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
6685
6693
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
6686
6694
  const s2 = this.findChapter(e15.chapterId);
6687
- if (s2) {
6688
- if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
6689
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
6690
- });
6691
- else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
6695
+ if (!s2) continue;
6696
+ if (!this.ensureVisibleChapterUrls(s2)) {
6697
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
6698
+ continue;
6692
6699
  }
6700
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
6701
+ let n9 = false;
6702
+ const r2 = () => {
6703
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
6704
+ };
6705
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
6693
6706
  }
6694
6707
  }
6695
6708
  prepareChapterForSegmentLoad(e14) {
@@ -6723,13 +6736,6 @@ const sa = class e11 extends BasePlugin {
6723
6736
  }
6724
6737
  if (0 === a2.size) return;
6725
6738
  const l = this.virtualPageMap.atIndex(r2);
6726
- if (l) {
6727
- const e15 = this.findChapter(l.chapterId);
6728
- if (e15) {
6729
- const t3 = Zr(e15, l.localPageIndex);
6730
- o2(l.chapterId, t3 + 1);
6731
- }
6732
- }
6733
6739
  for (const d2 of a2) this.chapterLastUsed.set(d2, n9);
6734
6740
  const h2 = [...i2.entries()].map(([e15, t3]) => {
6735
6741
  const [s3, n10] = e15.split(":");
@@ -6779,21 +6785,21 @@ const sa = class e11 extends BasePlugin {
6779
6785
  ensureSegmentLoaded(e14, t2) {
6780
6786
  return this.loadSegmentNow(e14, t2);
6781
6787
  }
6782
- loadSegmentNow(e14, t2) {
6788
+ loadSegmentNow(e14, t2, s2) {
6783
6789
  this.prepareChapterForSegmentLoad(e14);
6784
- const s2 = this.findChapter(e14);
6785
- if (!s2) return Promise.resolve("error");
6786
- const n9 = jr(s2);
6787
- if (!n9) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(s2);
6788
- if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
6789
- const r2 = n9.segments[t2], a2 = r2.documentId;
6790
- if (this.documentManager.provides().isDocumentOpen(a2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
6791
- const i2 = this.pendingSegmentLoadPromises.get(a2);
6792
- if (i2) return i2;
6793
- const o2 = this.startLoadSegment(s2, r2).finally(() => {
6794
- this.pendingSegmentLoadPromises.delete(a2);
6790
+ const n9 = this.findChapter(e14);
6791
+ if (!n9) return Promise.resolve("error");
6792
+ const r2 = jr(n9);
6793
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
6794
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
6795
+ const a2 = r2.segments[t2], i2 = a2.documentId;
6796
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
6797
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
6798
+ if (o2) return o2;
6799
+ const l = this.startLoadSegment(n9, a2, s2).finally(() => {
6800
+ this.pendingSegmentLoadPromises.delete(i2);
6795
6801
  });
6796
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
6802
+ return this.pendingSegmentLoadPromises.set(i2, l), l;
6797
6803
  }
6798
6804
  ensureSingleDocumentChapter(e14) {
6799
6805
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -6867,24 +6873,27 @@ const sa = class e11 extends BasePlugin {
6867
6873
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(t2, s2);
6868
6874
  throw new Error(`Chapter "${t2.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
6869
6875
  }
6870
- async startLoadSegment(t2, s2) {
6871
- var n9;
6872
- const r2 = t2.chapterId, a2 = (null == s2 ? void 0 : s2.index) ?? 0, i2 = (null == s2 ? void 0 : s2.documentId) ?? r2;
6873
- this.chapterErrorMessages.delete(r2), this.anySegmentOpen(r2) || this.updateStatus(r2, "loading"), this.chapterLastUsed.set(r2, Date.now());
6876
+ async startLoadSegment(t2, s2, n9) {
6877
+ var r2;
6878
+ const a2 = t2.chapterId, i2 = (null == s2 ? void 0 : s2.index) ?? 0, o2 = (null == s2 ? void 0 : s2.documentId) ?? a2;
6879
+ let l = false;
6880
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
6874
6881
  try {
6875
- const e14 = t2.encrypted && (null == (n9 = this.passwordProvider) ? void 0 : n9.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, o2 = await this.resolvePdfPayload(t2, a2), l = this.documentManager.provides(), h2 = "url" in o2 ? l.openDocumentUrl({ documentId: i2, url: o2.url, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }) : l.openDocumentBuffer({ documentId: i2, buffer: o2.buffer, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
6876
- h2.wait((t4) => e15(t4), (e16) => t3(e16));
6882
+ const e14 = t2.encrypted && (null == (r2 = this.passwordProvider) ? void 0 : r2.getCachedPassword) ? this.passwordProvider.getCachedPassword(a2) : null, h2 = await this.resolvePdfPayload(t2, i2), d2 = this.documentManager.provides(), c2 = "url" in h2 ? d2.openDocumentUrl({ documentId: o2, url: h2.url, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false }) : d2.openDocumentBuffer({ documentId: o2, buffer: h2.buffer, name: s2 ? `${t2.title} (${s2.index + 1})` : t2.title, password: e14 ?? void 0, autoActivate: false });
6883
+ l || (l = true, null == n9 || n9());
6884
+ const u2 = await new Promise((e15, t3) => {
6885
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
6877
6886
  });
6878
6887
  return await new Promise((e15, t3) => {
6879
- d2.task.wait(() => e15(), (e16) => t3(e16));
6880
- }), l.isDocumentOpen(i2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
6881
- } catch (o2) {
6882
- const t3 = this.chapterStatus.get(r2);
6888
+ u2.task.wait(() => e15(), (e16) => t3(e16));
6889
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
6890
+ } catch (h2) {
6891
+ const t3 = this.chapterStatus.get(a2);
6883
6892
  if ("loaded" !== t3 && "password-required" !== t3) {
6884
- const t4 = o2 instanceof Error ? o2.message : `Failed to load chapter "${r2}"`;
6885
- this.logger.error(e11.id, "LoadChapter", t4, o2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", t4);
6893
+ const t4 = h2 instanceof Error ? h2.message : `Failed to load chapter "${a2}"`;
6894
+ this.logger.error(e11.id, "LoadChapter", t4, h2), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", t4);
6886
6895
  }
6887
- return this.chapterStatus.get(r2) ?? "error";
6896
+ return this.chapterStatus.get(a2) ?? "error";
6888
6897
  }
6889
6898
  }
6890
6899
  waitForTerminalStatus(e14) {
@@ -8040,12 +8049,17 @@ const B = class n extends BasePlugin {
8040
8049
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
8041
8050
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
8042
8051
  const s2 = this.findChapter(e15.chapterId);
8043
- if (s2) {
8044
- if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
8045
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
8046
- });
8047
- else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
8052
+ if (!s2) continue;
8053
+ if (!this.ensureVisibleChapterUrls(s2)) {
8054
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
8055
+ continue;
8048
8056
  }
8057
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
8058
+ let n9 = false;
8059
+ const r2 = () => {
8060
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
8061
+ };
8062
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
8049
8063
  }
8050
8064
  }
8051
8065
  prepareChapterForSegmentLoad(e14) {
@@ -8079,13 +8093,6 @@ const B = class n extends BasePlugin {
8079
8093
  }
8080
8094
  if (0 === a2.size) return;
8081
8095
  const h2 = this.virtualPageMap.atIndex(r2);
8082
- if (h2) {
8083
- const e15 = this.findChapter(h2.chapterId);
8084
- if (e15) {
8085
- const t3 = T(e15, h2.localPageIndex);
8086
- o2(h2.chapterId, t3 + 1);
8087
- }
8088
- }
8089
8096
  for (const d2 of a2) this.chapterLastUsed.set(d2, n9);
8090
8097
  const l = [...i2.entries()].map(([e15, t3]) => {
8091
8098
  const [s3, n10] = e15.split(":");
@@ -8135,21 +8142,21 @@ const B = class n extends BasePlugin {
8135
8142
  ensureSegmentLoaded(e14, t2) {
8136
8143
  return this.loadSegmentNow(e14, t2);
8137
8144
  }
8138
- loadSegmentNow(e14, t2) {
8145
+ loadSegmentNow(e14, t2, s2) {
8139
8146
  this.prepareChapterForSegmentLoad(e14);
8140
- const s2 = this.findChapter(e14);
8141
- if (!s2) return Promise.resolve("error");
8142
- const n9 = E(s2);
8143
- if (!n9) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(s2);
8144
- if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
8145
- const r2 = n9.segments[t2], a2 = r2.documentId;
8146
- if (this.documentManager.provides().isDocumentOpen(a2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
8147
- const i2 = this.pendingSegmentLoadPromises.get(a2);
8148
- if (i2) return i2;
8149
- const o2 = this.startLoadSegment(s2, r2).finally(() => {
8150
- this.pendingSegmentLoadPromises.delete(a2);
8147
+ const n9 = this.findChapter(e14);
8148
+ if (!n9) return Promise.resolve("error");
8149
+ const r2 = E(n9);
8150
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
8151
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
8152
+ const a2 = r2.segments[t2], i2 = a2.documentId;
8153
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
8154
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
8155
+ if (o2) return o2;
8156
+ const h2 = this.startLoadSegment(n9, a2, s2).finally(() => {
8157
+ this.pendingSegmentLoadPromises.delete(i2);
8151
8158
  });
8152
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
8159
+ return this.pendingSegmentLoadPromises.set(i2, h2), h2;
8153
8160
  }
8154
8161
  ensureSingleDocumentChapter(e14) {
8155
8162
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -8223,24 +8230,27 @@ const B = class n extends BasePlugin {
8223
8230
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(e14, t2);
8224
8231
  throw new Error(`Chapter "${e14.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
8225
8232
  }
8226
- async startLoadSegment(e14, t2) {
8227
- var s2;
8228
- const r2 = e14.chapterId, a2 = (null == t2 ? void 0 : t2.index) ?? 0, i2 = (null == t2 ? void 0 : t2.documentId) ?? r2;
8229
- this.chapterErrorMessages.delete(r2), this.anySegmentOpen(r2) || this.updateStatus(r2, "loading"), this.chapterLastUsed.set(r2, Date.now());
8233
+ async startLoadSegment(e14, t2, s2) {
8234
+ var r2;
8235
+ const a2 = e14.chapterId, i2 = (null == t2 ? void 0 : t2.index) ?? 0, o2 = (null == t2 ? void 0 : t2.documentId) ?? a2;
8236
+ let h2 = false;
8237
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
8230
8238
  try {
8231
- const n9 = e14.encrypted && (null == (s2 = this.passwordProvider) ? void 0 : s2.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, o2 = await this.resolvePdfPayload(e14, a2), h2 = this.documentManager.provides(), l = "url" in o2 ? h2.openDocumentUrl({ documentId: i2, url: o2.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : h2.openDocumentBuffer({ documentId: i2, buffer: o2.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
8232
- l.wait((t4) => e15(t4), (e16) => t3(e16));
8239
+ const n9 = e14.encrypted && (null == (r2 = this.passwordProvider) ? void 0 : r2.getCachedPassword) ? this.passwordProvider.getCachedPassword(a2) : null, l = await this.resolvePdfPayload(e14, i2), d2 = this.documentManager.provides(), c2 = "url" in l ? d2.openDocumentUrl({ documentId: o2, url: l.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : d2.openDocumentBuffer({ documentId: o2, buffer: l.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false });
8240
+ h2 || (h2 = true, null == s2 || s2());
8241
+ const u2 = await new Promise((e15, t3) => {
8242
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
8233
8243
  });
8234
8244
  return await new Promise((e15, t3) => {
8235
- d2.task.wait(() => e15(), (e16) => t3(e16));
8236
- }), h2.isDocumentOpen(i2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
8237
- } catch (o2) {
8238
- const e15 = this.chapterStatus.get(r2);
8245
+ u2.task.wait(() => e15(), (e16) => t3(e16));
8246
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
8247
+ } catch (l) {
8248
+ const e15 = this.chapterStatus.get(a2);
8239
8249
  if ("loaded" !== e15 && "password-required" !== e15) {
8240
- const e16 = o2 instanceof Error ? o2.message : `Failed to load chapter "${r2}"`;
8241
- this.logger.error(n.id, "LoadChapter", e16, o2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", e16);
8250
+ const e16 = l instanceof Error ? l.message : `Failed to load chapter "${a2}"`;
8251
+ this.logger.error(n.id, "LoadChapter", e16, l), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", e16);
8242
8252
  }
8243
- return this.chapterStatus.get(r2) ?? "error";
8253
+ return this.chapterStatus.get(a2) ?? "error";
8244
8254
  }
8245
8255
  }
8246
8256
  waitForTerminalStatus(e14) {
@@ -8652,12 +8662,17 @@ const ce = class n2 extends BasePlugin {
8652
8662
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
8653
8663
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
8654
8664
  const s2 = this.findChapter(e15.chapterId);
8655
- if (s2) {
8656
- if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
8657
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
8658
- });
8659
- else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
8665
+ if (!s2) continue;
8666
+ if (!this.ensureVisibleChapterUrls(s2)) {
8667
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
8668
+ continue;
8660
8669
  }
8670
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
8671
+ let n9 = false;
8672
+ const r2 = () => {
8673
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
8674
+ };
8675
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
8661
8676
  }
8662
8677
  }
8663
8678
  prepareChapterForSegmentLoad(e14) {
@@ -8691,13 +8706,6 @@ const ce = class n2 extends BasePlugin {
8691
8706
  }
8692
8707
  if (0 === a2.size) return;
8693
8708
  const h2 = this.virtualPageMap.atIndex(r2);
8694
- if (h2) {
8695
- const e15 = this.findChapter(h2.chapterId);
8696
- if (e15) {
8697
- const t3 = ae(e15, h2.localPageIndex);
8698
- o2(h2.chapterId, t3 + 1);
8699
- }
8700
- }
8701
8709
  for (const d2 of a2) this.chapterLastUsed.set(d2, n9);
8702
8710
  const l = [...i2.entries()].map(([e15, t3]) => {
8703
8711
  const [s3, n10] = e15.split(":");
@@ -8747,21 +8755,21 @@ const ce = class n2 extends BasePlugin {
8747
8755
  ensureSegmentLoaded(e14, t2) {
8748
8756
  return this.loadSegmentNow(e14, t2);
8749
8757
  }
8750
- loadSegmentNow(e14, t2) {
8758
+ loadSegmentNow(e14, t2, s2) {
8751
8759
  this.prepareChapterForSegmentLoad(e14);
8752
- const s2 = this.findChapter(e14);
8753
- if (!s2) return Promise.resolve("error");
8754
- const n9 = ne(s2);
8755
- if (!n9) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(s2);
8756
- if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
8757
- const r2 = n9.segments[t2], a2 = r2.documentId;
8758
- if (this.documentManager.provides().isDocumentOpen(a2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
8759
- const i2 = this.pendingSegmentLoadPromises.get(a2);
8760
- if (i2) return i2;
8761
- const o2 = this.startLoadSegment(s2, r2).finally(() => {
8762
- this.pendingSegmentLoadPromises.delete(a2);
8760
+ const n9 = this.findChapter(e14);
8761
+ if (!n9) return Promise.resolve("error");
8762
+ const r2 = ne(n9);
8763
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
8764
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
8765
+ const a2 = r2.segments[t2], i2 = a2.documentId;
8766
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
8767
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
8768
+ if (o2) return o2;
8769
+ const h2 = this.startLoadSegment(n9, a2, s2).finally(() => {
8770
+ this.pendingSegmentLoadPromises.delete(i2);
8763
8771
  });
8764
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
8772
+ return this.pendingSegmentLoadPromises.set(i2, h2), h2;
8765
8773
  }
8766
8774
  ensureSingleDocumentChapter(e14) {
8767
8775
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -8835,24 +8843,27 @@ const ce = class n2 extends BasePlugin {
8835
8843
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(e14, t2);
8836
8844
  throw new Error(`Chapter "${e14.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
8837
8845
  }
8838
- async startLoadSegment(e14, t2) {
8839
- var s2;
8840
- const r2 = e14.chapterId, a2 = (null == t2 ? void 0 : t2.index) ?? 0, i2 = (null == t2 ? void 0 : t2.documentId) ?? r2;
8841
- this.chapterErrorMessages.delete(r2), this.anySegmentOpen(r2) || this.updateStatus(r2, "loading"), this.chapterLastUsed.set(r2, Date.now());
8846
+ async startLoadSegment(e14, t2, s2) {
8847
+ var r2;
8848
+ const a2 = e14.chapterId, i2 = (null == t2 ? void 0 : t2.index) ?? 0, o2 = (null == t2 ? void 0 : t2.documentId) ?? a2;
8849
+ let h2 = false;
8850
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
8842
8851
  try {
8843
- const n9 = e14.encrypted && (null == (s2 = this.passwordProvider) ? void 0 : s2.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, o2 = await this.resolvePdfPayload(e14, a2), h2 = this.documentManager.provides(), l = "url" in o2 ? h2.openDocumentUrl({ documentId: i2, url: o2.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : h2.openDocumentBuffer({ documentId: i2, buffer: o2.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
8844
- l.wait((t4) => e15(t4), (e16) => t3(e16));
8852
+ const n9 = e14.encrypted && (null == (r2 = this.passwordProvider) ? void 0 : r2.getCachedPassword) ? this.passwordProvider.getCachedPassword(a2) : null, l = await this.resolvePdfPayload(e14, i2), d2 = this.documentManager.provides(), c2 = "url" in l ? d2.openDocumentUrl({ documentId: o2, url: l.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : d2.openDocumentBuffer({ documentId: o2, buffer: l.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false });
8853
+ h2 || (h2 = true, null == s2 || s2());
8854
+ const u2 = await new Promise((e15, t3) => {
8855
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
8845
8856
  });
8846
8857
  return await new Promise((e15, t3) => {
8847
- d2.task.wait(() => e15(), (e16) => t3(e16));
8848
- }), h2.isDocumentOpen(i2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
8849
- } catch (o2) {
8850
- const e15 = this.chapterStatus.get(r2);
8858
+ u2.task.wait(() => e15(), (e16) => t3(e16));
8859
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
8860
+ } catch (l) {
8861
+ const e15 = this.chapterStatus.get(a2);
8851
8862
  if ("loaded" !== e15 && "password-required" !== e15) {
8852
- const e16 = o2 instanceof Error ? o2.message : `Failed to load chapter "${r2}"`;
8853
- this.logger.error(n2.id, "LoadChapter", e16, o2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", e16);
8863
+ const e16 = l instanceof Error ? l.message : `Failed to load chapter "${a2}"`;
8864
+ this.logger.error(n2.id, "LoadChapter", e16, l), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", e16);
8854
8865
  }
8855
- return this.chapterStatus.get(r2) ?? "error";
8866
+ return this.chapterStatus.get(a2) ?? "error";
8856
8867
  }
8857
8868
  }
8858
8869
  waitForTerminalStatus(e14) {
@@ -9452,12 +9463,17 @@ const Ee = class n3 extends BasePlugin {
9452
9463
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
9453
9464
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
9454
9465
  const s2 = this.findChapter(e15.chapterId);
9455
- if (s2) {
9456
- if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
9457
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
9458
- });
9459
- else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
9466
+ if (!s2) continue;
9467
+ if (!this.ensureVisibleChapterUrls(s2)) {
9468
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
9469
+ continue;
9460
9470
  }
9471
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
9472
+ let n9 = false;
9473
+ const r2 = () => {
9474
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
9475
+ };
9476
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
9461
9477
  }
9462
9478
  }
9463
9479
  prepareChapterForSegmentLoad(e14) {
@@ -9491,13 +9507,6 @@ const Ee = class n3 extends BasePlugin {
9491
9507
  }
9492
9508
  if (0 === a2.size) return;
9493
9509
  const h2 = this.virtualPageMap.atIndex(r2);
9494
- if (h2) {
9495
- const e15 = this.findChapter(h2.chapterId);
9496
- if (e15) {
9497
- const t3 = Le(e15, h2.localPageIndex);
9498
- o2(h2.chapterId, t3 + 1);
9499
- }
9500
- }
9501
9510
  for (const d2 of a2) this.chapterLastUsed.set(d2, n9);
9502
9511
  const l = [...i2.entries()].map(([e15, t3]) => {
9503
9512
  const [s3, n10] = e15.split(":");
@@ -9547,21 +9556,21 @@ const Ee = class n3 extends BasePlugin {
9547
9556
  ensureSegmentLoaded(e14, t2) {
9548
9557
  return this.loadSegmentNow(e14, t2);
9549
9558
  }
9550
- loadSegmentNow(e14, t2) {
9559
+ loadSegmentNow(e14, t2, s2) {
9551
9560
  this.prepareChapterForSegmentLoad(e14);
9552
- const s2 = this.findChapter(e14);
9553
- if (!s2) return Promise.resolve("error");
9554
- const n9 = Me(s2);
9555
- if (!n9) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(s2);
9556
- if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
9557
- const r2 = n9.segments[t2], a2 = r2.documentId;
9558
- if (this.documentManager.provides().isDocumentOpen(a2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
9559
- const i2 = this.pendingSegmentLoadPromises.get(a2);
9560
- if (i2) return i2;
9561
- const o2 = this.startLoadSegment(s2, r2).finally(() => {
9562
- this.pendingSegmentLoadPromises.delete(a2);
9561
+ const n9 = this.findChapter(e14);
9562
+ if (!n9) return Promise.resolve("error");
9563
+ const r2 = Me(n9);
9564
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
9565
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
9566
+ const a2 = r2.segments[t2], i2 = a2.documentId;
9567
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
9568
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
9569
+ if (o2) return o2;
9570
+ const h2 = this.startLoadSegment(n9, a2, s2).finally(() => {
9571
+ this.pendingSegmentLoadPromises.delete(i2);
9563
9572
  });
9564
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
9573
+ return this.pendingSegmentLoadPromises.set(i2, h2), h2;
9565
9574
  }
9566
9575
  ensureSingleDocumentChapter(e14) {
9567
9576
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -9635,24 +9644,27 @@ const Ee = class n3 extends BasePlugin {
9635
9644
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(e14, t2);
9636
9645
  throw new Error(`Chapter "${e14.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
9637
9646
  }
9638
- async startLoadSegment(e14, t2) {
9639
- var s2;
9640
- const r2 = e14.chapterId, a2 = (null == t2 ? void 0 : t2.index) ?? 0, i2 = (null == t2 ? void 0 : t2.documentId) ?? r2;
9641
- this.chapterErrorMessages.delete(r2), this.anySegmentOpen(r2) || this.updateStatus(r2, "loading"), this.chapterLastUsed.set(r2, Date.now());
9647
+ async startLoadSegment(e14, t2, s2) {
9648
+ var r2;
9649
+ const a2 = e14.chapterId, i2 = (null == t2 ? void 0 : t2.index) ?? 0, o2 = (null == t2 ? void 0 : t2.documentId) ?? a2;
9650
+ let h2 = false;
9651
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
9642
9652
  try {
9643
- const n9 = e14.encrypted && (null == (s2 = this.passwordProvider) ? void 0 : s2.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, o2 = await this.resolvePdfPayload(e14, a2), h2 = this.documentManager.provides(), l = "url" in o2 ? h2.openDocumentUrl({ documentId: i2, url: o2.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : h2.openDocumentBuffer({ documentId: i2, buffer: o2.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
9644
- l.wait((t4) => e15(t4), (e16) => t3(e16));
9653
+ const n9 = e14.encrypted && (null == (r2 = this.passwordProvider) ? void 0 : r2.getCachedPassword) ? this.passwordProvider.getCachedPassword(a2) : null, l = await this.resolvePdfPayload(e14, i2), d2 = this.documentManager.provides(), c2 = "url" in l ? d2.openDocumentUrl({ documentId: o2, url: l.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : d2.openDocumentBuffer({ documentId: o2, buffer: l.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false });
9654
+ h2 || (h2 = true, null == s2 || s2());
9655
+ const u2 = await new Promise((e15, t3) => {
9656
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
9645
9657
  });
9646
9658
  return await new Promise((e15, t3) => {
9647
- d2.task.wait(() => e15(), (e16) => t3(e16));
9648
- }), h2.isDocumentOpen(i2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
9649
- } catch (o2) {
9650
- const e15 = this.chapterStatus.get(r2);
9659
+ u2.task.wait(() => e15(), (e16) => t3(e16));
9660
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
9661
+ } catch (l) {
9662
+ const e15 = this.chapterStatus.get(a2);
9651
9663
  if ("loaded" !== e15 && "password-required" !== e15) {
9652
- const e16 = o2 instanceof Error ? o2.message : `Failed to load chapter "${r2}"`;
9653
- this.logger.error(n3.id, "LoadChapter", e16, o2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", e16);
9664
+ const e16 = l instanceof Error ? l.message : `Failed to load chapter "${a2}"`;
9665
+ this.logger.error(n3.id, "LoadChapter", e16, l), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", e16);
9654
9666
  }
9655
- return this.chapterStatus.get(r2) ?? "error";
9667
+ return this.chapterStatus.get(a2) ?? "error";
9656
9668
  }
9657
9669
  }
9658
9670
  waitForTerminalStatus(e14) {
@@ -10064,12 +10076,17 @@ const et2 = class n4 extends BasePlugin {
10064
10076
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
10065
10077
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
10066
10078
  const s2 = this.findChapter(e15.chapterId);
10067
- if (s2) {
10068
- if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
10069
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
10070
- });
10071
- else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
10079
+ if (!s2) continue;
10080
+ if (!this.ensureVisibleChapterUrls(s2)) {
10081
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
10082
+ continue;
10072
10083
  }
10084
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
10085
+ let n9 = false;
10086
+ const r2 = () => {
10087
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
10088
+ };
10089
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
10073
10090
  }
10074
10091
  }
10075
10092
  prepareChapterForSegmentLoad(e14) {
@@ -10103,13 +10120,6 @@ const et2 = class n4 extends BasePlugin {
10103
10120
  }
10104
10121
  if (0 === a2.size) return;
10105
10122
  const h2 = this.virtualPageMap.atIndex(r2);
10106
- if (h2) {
10107
- const e15 = this.findChapter(h2.chapterId);
10108
- if (e15) {
10109
- const t3 = je(e15, h2.localPageIndex);
10110
- o2(h2.chapterId, t3 + 1);
10111
- }
10112
- }
10113
10123
  for (const d2 of a2) this.chapterLastUsed.set(d2, n9);
10114
10124
  const l = [...i2.entries()].map(([e15, t3]) => {
10115
10125
  const [s3, n10] = e15.split(":");
@@ -10159,21 +10169,21 @@ const et2 = class n4 extends BasePlugin {
10159
10169
  ensureSegmentLoaded(e14, t2) {
10160
10170
  return this.loadSegmentNow(e14, t2);
10161
10171
  }
10162
- loadSegmentNow(e14, t2) {
10172
+ loadSegmentNow(e14, t2, s2) {
10163
10173
  this.prepareChapterForSegmentLoad(e14);
10164
- const s2 = this.findChapter(e14);
10165
- if (!s2) return Promise.resolve("error");
10166
- const n9 = Ke(s2);
10167
- if (!n9) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(s2);
10168
- if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
10169
- const r2 = n9.segments[t2], a2 = r2.documentId;
10170
- if (this.documentManager.provides().isDocumentOpen(a2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
10171
- const i2 = this.pendingSegmentLoadPromises.get(a2);
10172
- if (i2) return i2;
10173
- const o2 = this.startLoadSegment(s2, r2).finally(() => {
10174
- this.pendingSegmentLoadPromises.delete(a2);
10174
+ const n9 = this.findChapter(e14);
10175
+ if (!n9) return Promise.resolve("error");
10176
+ const r2 = Ke(n9);
10177
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
10178
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
10179
+ const a2 = r2.segments[t2], i2 = a2.documentId;
10180
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
10181
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
10182
+ if (o2) return o2;
10183
+ const h2 = this.startLoadSegment(n9, a2, s2).finally(() => {
10184
+ this.pendingSegmentLoadPromises.delete(i2);
10175
10185
  });
10176
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
10186
+ return this.pendingSegmentLoadPromises.set(i2, h2), h2;
10177
10187
  }
10178
10188
  ensureSingleDocumentChapter(e14) {
10179
10189
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -10247,24 +10257,27 @@ const et2 = class n4 extends BasePlugin {
10247
10257
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(e14, t2);
10248
10258
  throw new Error(`Chapter "${e14.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
10249
10259
  }
10250
- async startLoadSegment(e14, t2) {
10251
- var s2;
10252
- const r2 = e14.chapterId, a2 = (null == t2 ? void 0 : t2.index) ?? 0, i2 = (null == t2 ? void 0 : t2.documentId) ?? r2;
10253
- this.chapterErrorMessages.delete(r2), this.anySegmentOpen(r2) || this.updateStatus(r2, "loading"), this.chapterLastUsed.set(r2, Date.now());
10260
+ async startLoadSegment(e14, t2, s2) {
10261
+ var r2;
10262
+ const a2 = e14.chapterId, i2 = (null == t2 ? void 0 : t2.index) ?? 0, o2 = (null == t2 ? void 0 : t2.documentId) ?? a2;
10263
+ let h2 = false;
10264
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
10254
10265
  try {
10255
- const n9 = e14.encrypted && (null == (s2 = this.passwordProvider) ? void 0 : s2.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, o2 = await this.resolvePdfPayload(e14, a2), h2 = this.documentManager.provides(), l = "url" in o2 ? h2.openDocumentUrl({ documentId: i2, url: o2.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : h2.openDocumentBuffer({ documentId: i2, buffer: o2.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
10256
- l.wait((t4) => e15(t4), (e16) => t3(e16));
10266
+ const n9 = e14.encrypted && (null == (r2 = this.passwordProvider) ? void 0 : r2.getCachedPassword) ? this.passwordProvider.getCachedPassword(a2) : null, l = await this.resolvePdfPayload(e14, i2), d2 = this.documentManager.provides(), c2 = "url" in l ? d2.openDocumentUrl({ documentId: o2, url: l.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : d2.openDocumentBuffer({ documentId: o2, buffer: l.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false });
10267
+ h2 || (h2 = true, null == s2 || s2());
10268
+ const u2 = await new Promise((e15, t3) => {
10269
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
10257
10270
  });
10258
10271
  return await new Promise((e15, t3) => {
10259
- d2.task.wait(() => e15(), (e16) => t3(e16));
10260
- }), h2.isDocumentOpen(i2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
10261
- } catch (o2) {
10262
- const e15 = this.chapterStatus.get(r2);
10272
+ u2.task.wait(() => e15(), (e16) => t3(e16));
10273
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
10274
+ } catch (l) {
10275
+ const e15 = this.chapterStatus.get(a2);
10263
10276
  if ("loaded" !== e15 && "password-required" !== e15) {
10264
- const e16 = o2 instanceof Error ? o2.message : `Failed to load chapter "${r2}"`;
10265
- this.logger.error(n4.id, "LoadChapter", e16, o2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", e16);
10277
+ const e16 = l instanceof Error ? l.message : `Failed to load chapter "${a2}"`;
10278
+ this.logger.error(n4.id, "LoadChapter", e16, l), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", e16);
10266
10279
  }
10267
- return this.chapterStatus.get(r2) ?? "error";
10280
+ return this.chapterStatus.get(a2) ?? "error";
10268
10281
  }
10269
10282
  }
10270
10283
  waitForTerminalStatus(e14) {
@@ -10979,12 +10992,17 @@ const Mt = class n6 extends BasePlugin {
10979
10992
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
10980
10993
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
10981
10994
  const s2 = this.findChapter(e15.chapterId);
10982
- if (s2) {
10983
- if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
10984
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
10985
- });
10986
- else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
10995
+ if (!s2) continue;
10996
+ if (!this.ensureVisibleChapterUrls(s2)) {
10997
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
10998
+ continue;
10987
10999
  }
11000
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
11001
+ let n9 = false;
11002
+ const r2 = () => {
11003
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
11004
+ };
11005
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
10988
11006
  }
10989
11007
  }
10990
11008
  prepareChapterForSegmentLoad(e14) {
@@ -11018,13 +11036,6 @@ const Mt = class n6 extends BasePlugin {
11018
11036
  }
11019
11037
  if (0 === a2.size) return;
11020
11038
  const h2 = this.virtualPageMap.atIndex(r2);
11021
- if (h2) {
11022
- const e15 = this.findChapter(h2.chapterId);
11023
- if (e15) {
11024
- const t3 = Pt(e15, h2.localPageIndex);
11025
- o2(h2.chapterId, t3 + 1);
11026
- }
11027
- }
11028
11039
  for (const d2 of a2) this.chapterLastUsed.set(d2, n9);
11029
11040
  const l = [...i2.entries()].map(([e15, t3]) => {
11030
11041
  const [s3, n10] = e15.split(":");
@@ -11074,21 +11085,21 @@ const Mt = class n6 extends BasePlugin {
11074
11085
  ensureSegmentLoaded(e14, t2) {
11075
11086
  return this.loadSegmentNow(e14, t2);
11076
11087
  }
11077
- loadSegmentNow(e14, t2) {
11088
+ loadSegmentNow(e14, t2, s2) {
11078
11089
  this.prepareChapterForSegmentLoad(e14);
11079
- const s2 = this.findChapter(e14);
11080
- if (!s2) return Promise.resolve("error");
11081
- const n9 = vt(s2);
11082
- if (!n9) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(s2);
11083
- if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
11084
- const r2 = n9.segments[t2], a2 = r2.documentId;
11085
- if (this.documentManager.provides().isDocumentOpen(a2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
11086
- const i2 = this.pendingSegmentLoadPromises.get(a2);
11087
- if (i2) return i2;
11088
- const o2 = this.startLoadSegment(s2, r2).finally(() => {
11089
- this.pendingSegmentLoadPromises.delete(a2);
11090
+ const n9 = this.findChapter(e14);
11091
+ if (!n9) return Promise.resolve("error");
11092
+ const r2 = vt(n9);
11093
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
11094
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
11095
+ const a2 = r2.segments[t2], i2 = a2.documentId;
11096
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
11097
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
11098
+ if (o2) return o2;
11099
+ const h2 = this.startLoadSegment(n9, a2, s2).finally(() => {
11100
+ this.pendingSegmentLoadPromises.delete(i2);
11090
11101
  });
11091
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
11102
+ return this.pendingSegmentLoadPromises.set(i2, h2), h2;
11092
11103
  }
11093
11104
  ensureSingleDocumentChapter(e14) {
11094
11105
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -11162,24 +11173,27 @@ const Mt = class n6 extends BasePlugin {
11162
11173
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(e14, t2);
11163
11174
  throw new Error(`Chapter "${e14.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
11164
11175
  }
11165
- async startLoadSegment(e14, t2) {
11166
- var s2;
11167
- const r2 = e14.chapterId, a2 = (null == t2 ? void 0 : t2.index) ?? 0, i2 = (null == t2 ? void 0 : t2.documentId) ?? r2;
11168
- this.chapterErrorMessages.delete(r2), this.anySegmentOpen(r2) || this.updateStatus(r2, "loading"), this.chapterLastUsed.set(r2, Date.now());
11176
+ async startLoadSegment(e14, t2, s2) {
11177
+ var r2;
11178
+ const a2 = e14.chapterId, i2 = (null == t2 ? void 0 : t2.index) ?? 0, o2 = (null == t2 ? void 0 : t2.documentId) ?? a2;
11179
+ let h2 = false;
11180
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
11169
11181
  try {
11170
- const n9 = e14.encrypted && (null == (s2 = this.passwordProvider) ? void 0 : s2.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, o2 = await this.resolvePdfPayload(e14, a2), h2 = this.documentManager.provides(), l = "url" in o2 ? h2.openDocumentUrl({ documentId: i2, url: o2.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : h2.openDocumentBuffer({ documentId: i2, buffer: o2.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
11171
- l.wait((t4) => e15(t4), (e16) => t3(e16));
11182
+ const n9 = e14.encrypted && (null == (r2 = this.passwordProvider) ? void 0 : r2.getCachedPassword) ? this.passwordProvider.getCachedPassword(a2) : null, l = await this.resolvePdfPayload(e14, i2), d2 = this.documentManager.provides(), c2 = "url" in l ? d2.openDocumentUrl({ documentId: o2, url: l.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : d2.openDocumentBuffer({ documentId: o2, buffer: l.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false });
11183
+ h2 || (h2 = true, null == s2 || s2());
11184
+ const u2 = await new Promise((e15, t3) => {
11185
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
11172
11186
  });
11173
11187
  return await new Promise((e15, t3) => {
11174
- d2.task.wait(() => e15(), (e16) => t3(e16));
11175
- }), h2.isDocumentOpen(i2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
11176
- } catch (o2) {
11177
- const e15 = this.chapterStatus.get(r2);
11188
+ u2.task.wait(() => e15(), (e16) => t3(e16));
11189
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
11190
+ } catch (l) {
11191
+ const e15 = this.chapterStatus.get(a2);
11178
11192
  if ("loaded" !== e15 && "password-required" !== e15) {
11179
- const e16 = o2 instanceof Error ? o2.message : `Failed to load chapter "${r2}"`;
11180
- this.logger.error(n6.id, "LoadChapter", e16, o2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", e16);
11193
+ const e16 = l instanceof Error ? l.message : `Failed to load chapter "${a2}"`;
11194
+ this.logger.error(n6.id, "LoadChapter", e16, l), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", e16);
11181
11195
  }
11182
- return this.chapterStatus.get(r2) ?? "error";
11196
+ return this.chapterStatus.get(a2) ?? "error";
11183
11197
  }
11184
11198
  }
11185
11199
  waitForTerminalStatus(e14) {
@@ -11591,12 +11605,17 @@ const Kt = class n7 extends BasePlugin {
11591
11605
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
11592
11606
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
11593
11607
  const s2 = this.findChapter(e15.chapterId);
11594
- if (s2) {
11595
- if (this.ensureVisibleChapterUrls(s2)) t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++, this.loadSegmentNow(e15.chapterId, e15.segmentIndex).finally(() => {
11596
- this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog();
11597
- });
11598
- else if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
11608
+ if (!s2) continue;
11609
+ if (!this.ensureVisibleChapterUrls(s2)) {
11610
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
11611
+ continue;
11599
11612
  }
11613
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
11614
+ let n9 = false;
11615
+ const r2 = () => {
11616
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
11617
+ };
11618
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
11600
11619
  }
11601
11620
  }
11602
11621
  prepareChapterForSegmentLoad(e14) {
@@ -11630,13 +11649,6 @@ const Kt = class n7 extends BasePlugin {
11630
11649
  }
11631
11650
  if (0 === a2.size) return;
11632
11651
  const h2 = this.virtualPageMap.atIndex(r2);
11633
- if (h2) {
11634
- const e15 = this.findChapter(h2.chapterId);
11635
- if (e15) {
11636
- const t3 = Gt(e15, h2.localPageIndex);
11637
- o2(h2.chapterId, t3 + 1);
11638
- }
11639
- }
11640
11652
  for (const d2 of a2) this.chapterLastUsed.set(d2, n9);
11641
11653
  const l = [...i2.entries()].map(([e15, t3]) => {
11642
11654
  const [s3, n10] = e15.split(":");
@@ -11686,21 +11698,21 @@ const Kt = class n7 extends BasePlugin {
11686
11698
  ensureSegmentLoaded(e14, t2) {
11687
11699
  return this.loadSegmentNow(e14, t2);
11688
11700
  }
11689
- loadSegmentNow(e14, t2) {
11701
+ loadSegmentNow(e14, t2, s2) {
11690
11702
  this.prepareChapterForSegmentLoad(e14);
11691
- const s2 = this.findChapter(e14);
11692
- if (!s2) return Promise.resolve("error");
11693
- const n9 = Ot(s2);
11694
- if (!n9) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(s2);
11695
- if (t2 < 0 || t2 >= n9.segments.length) return Promise.resolve("error");
11696
- const r2 = n9.segments[t2], a2 = r2.documentId;
11697
- if (this.documentManager.provides().isDocumentOpen(a2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
11698
- const i2 = this.pendingSegmentLoadPromises.get(a2);
11699
- if (i2) return i2;
11700
- const o2 = this.startLoadSegment(s2, r2).finally(() => {
11701
- this.pendingSegmentLoadPromises.delete(a2);
11703
+ const n9 = this.findChapter(e14);
11704
+ if (!n9) return Promise.resolve("error");
11705
+ const r2 = Ot(n9);
11706
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
11707
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
11708
+ const a2 = r2.segments[t2], i2 = a2.documentId;
11709
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
11710
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
11711
+ if (o2) return o2;
11712
+ const h2 = this.startLoadSegment(n9, a2, s2).finally(() => {
11713
+ this.pendingSegmentLoadPromises.delete(i2);
11702
11714
  });
11703
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
11715
+ return this.pendingSegmentLoadPromises.set(i2, h2), h2;
11704
11716
  }
11705
11717
  ensureSingleDocumentChapter(e14) {
11706
11718
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -11774,24 +11786,27 @@ const Kt = class n7 extends BasePlugin {
11774
11786
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(e14, t2);
11775
11787
  throw new Error(`Chapter "${e14.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
11776
11788
  }
11777
- async startLoadSegment(e14, t2) {
11778
- var s2;
11779
- const r2 = e14.chapterId, a2 = (null == t2 ? void 0 : t2.index) ?? 0, i2 = (null == t2 ? void 0 : t2.documentId) ?? r2;
11780
- this.chapterErrorMessages.delete(r2), this.anySegmentOpen(r2) || this.updateStatus(r2, "loading"), this.chapterLastUsed.set(r2, Date.now());
11789
+ async startLoadSegment(e14, t2, s2) {
11790
+ var r2;
11791
+ const a2 = e14.chapterId, i2 = (null == t2 ? void 0 : t2.index) ?? 0, o2 = (null == t2 ? void 0 : t2.documentId) ?? a2;
11792
+ let h2 = false;
11793
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
11781
11794
  try {
11782
- const n9 = e14.encrypted && (null == (s2 = this.passwordProvider) ? void 0 : s2.getCachedPassword) ? this.passwordProvider.getCachedPassword(r2) : null, o2 = await this.resolvePdfPayload(e14, a2), h2 = this.documentManager.provides(), l = "url" in o2 ? h2.openDocumentUrl({ documentId: i2, url: o2.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : h2.openDocumentBuffer({ documentId: i2, buffer: o2.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }), d2 = await new Promise((e15, t3) => {
11783
- l.wait((t4) => e15(t4), (e16) => t3(e16));
11795
+ const n9 = e14.encrypted && (null == (r2 = this.passwordProvider) ? void 0 : r2.getCachedPassword) ? this.passwordProvider.getCachedPassword(a2) : null, l = await this.resolvePdfPayload(e14, i2), d2 = this.documentManager.provides(), c2 = "url" in l ? d2.openDocumentUrl({ documentId: o2, url: l.url, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false }) : d2.openDocumentBuffer({ documentId: o2, buffer: l.buffer, name: t2 ? `${e14.title} (${t2.index + 1})` : e14.title, password: n9 ?? void 0, autoActivate: false });
11796
+ h2 || (h2 = true, null == s2 || s2());
11797
+ const u2 = await new Promise((e15, t3) => {
11798
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
11784
11799
  });
11785
11800
  return await new Promise((e15, t3) => {
11786
- d2.task.wait(() => e15(), (e16) => t3(e16));
11787
- }), h2.isDocumentOpen(i2) && this.syncChapterStatusFromDocuments(r2), this.waitForTerminalStatus(r2);
11788
- } catch (o2) {
11789
- const e15 = this.chapterStatus.get(r2);
11801
+ u2.task.wait(() => e15(), (e16) => t3(e16));
11802
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
11803
+ } catch (l) {
11804
+ const e15 = this.chapterStatus.get(a2);
11790
11805
  if ("loaded" !== e15 && "password-required" !== e15) {
11791
- const e16 = o2 instanceof Error ? o2.message : `Failed to load chapter "${r2}"`;
11792
- this.logger.error(n7.id, "LoadChapter", e16, o2), 0 !== a2 || this.isAnySegmentWorkPending(r2) || this.updateStatus(r2, "error", e16);
11806
+ const e16 = l instanceof Error ? l.message : `Failed to load chapter "${a2}"`;
11807
+ this.logger.error(n7.id, "LoadChapter", e16, l), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", e16);
11793
11808
  }
11794
- return this.chapterStatus.get(r2) ?? "error";
11809
+ return this.chapterStatus.get(a2) ?? "error";
11795
11810
  }
11796
11811
  }
11797
11812
  waitForTerminalStatus(e14) {
@@ -12901,10 +12916,14 @@ const _ChapterManagerPlugin = class _ChapterManagerPlugin2 extends BasePlugin {
12901
12916
  stalled = 0;
12902
12917
  this.prepareChapterForSegmentLoad(t2.chapterId);
12903
12918
  this.activeVisibleSegmentLoads++;
12904
- void this.loadSegmentNow(t2.chapterId, t2.segmentIndex).finally(() => {
12919
+ let released = false;
12920
+ const releaseSlot = () => {
12921
+ if (released) return;
12922
+ released = true;
12905
12923
  this.activeVisibleSegmentLoads--;
12906
12924
  this.pumpVisibleSegmentBacklog();
12907
- });
12925
+ };
12926
+ void this.loadSegmentNow(t2.chapterId, t2.segmentIndex, releaseSlot).finally(releaseSlot);
12908
12927
  }
12909
12928
  }
12910
12929
  prepareChapterForSegmentLoad(chapterId) {
@@ -12950,13 +12969,6 @@ const _ChapterManagerPlugin = class _ChapterManagerPlugin2 extends BasePlugin {
12950
12969
  }
12951
12970
  if (visibleChapters.size === 0) return;
12952
12971
  const focalLoc = this.virtualPageMap.atIndex(focalIdx);
12953
- if (focalLoc) {
12954
- const focalChapter = this.findChapter(focalLoc.chapterId);
12955
- if (focalChapter) {
12956
- const focalSeg = i(focalChapter, focalLoc.localPageIndex);
12957
- setSegPriority(focalLoc.chapterId, focalSeg + 1);
12958
- }
12959
- }
12960
12972
  for (const cid of visibleChapters) {
12961
12973
  this.chapterLastUsed.set(cid, now);
12962
12974
  }
@@ -13026,7 +13038,7 @@ const _ChapterManagerPlugin = class _ChapterManagerPlugin2 extends BasePlugin {
13026
13038
  ensureSegmentLoaded(chapterId, segmentIndex) {
13027
13039
  return this.loadSegmentNow(chapterId, segmentIndex);
13028
13040
  }
13029
- loadSegmentNow(chapterId, segmentIndex) {
13041
+ loadSegmentNow(chapterId, segmentIndex, onDispatched) {
13030
13042
  this.prepareChapterForSegmentLoad(chapterId);
13031
13043
  const chapter = this.findChapter(chapterId);
13032
13044
  if (!chapter) return Promise.resolve("error");
@@ -13050,7 +13062,7 @@ const _ChapterManagerPlugin = class _ChapterManagerPlugin2 extends BasePlugin {
13050
13062
  }
13051
13063
  const existing = this.pendingSegmentLoadPromises.get(documentId);
13052
13064
  if (existing) return existing;
13053
- const promise = this.startLoadSegment(chapter, segment).finally(() => {
13065
+ const promise = this.startLoadSegment(chapter, segment, onDispatched).finally(() => {
13054
13066
  this.pendingSegmentLoadPromises.delete(documentId);
13055
13067
  });
13056
13068
  this.pendingSegmentLoadPromises.set(documentId, promise);
@@ -13175,11 +13187,17 @@ const _ChapterManagerPlugin = class _ChapterManagerPlugin2 extends BasePlugin {
13175
13187
  `Chapter "${chapter.chapterId}" has no PDF source; configure source or chapterPdfLoader`
13176
13188
  );
13177
13189
  }
13178
- async startLoadSegment(chapter, segment) {
13190
+ async startLoadSegment(chapter, segment, onDispatched) {
13179
13191
  var _a2;
13180
13192
  const chapterId = chapter.chapterId;
13181
13193
  const segmentIndex = (segment == null ? void 0 : segment.index) ?? 0;
13182
13194
  const documentId = (segment == null ? void 0 : segment.documentId) ?? chapterId;
13195
+ let dispatched = false;
13196
+ const markDispatched = () => {
13197
+ if (dispatched) return;
13198
+ dispatched = true;
13199
+ onDispatched == null ? void 0 : onDispatched();
13200
+ };
13183
13201
  this.chapterErrorMessages.delete(chapterId);
13184
13202
  if (!this.anySegmentOpen(chapterId)) {
13185
13203
  this.updateStatus(chapterId, "loading");
@@ -13202,6 +13220,7 @@ const _ChapterManagerPlugin = class _ChapterManagerPlugin2 extends BasePlugin {
13202
13220
  password: cachedPwd ?? void 0,
13203
13221
  autoActivate: false
13204
13222
  });
13223
+ markDispatched();
13205
13224
  const openResult = await new Promise((resolve, reject) => {
13206
13225
  openTask.wait((res) => resolve(res), (err) => reject(err));
13207
13226
  });