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

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) {
@@ -514,21 +519,21 @@ const st$1 = class e extends BasePlugin {
514
519
  ensureSegmentLoaded(e14, t2) {
515
520
  return this.loadSegmentNow(e14, t2);
516
521
  }
517
- loadSegmentNow(e14, t2) {
522
+ loadSegmentNow(e14, t2, s2) {
518
523
  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);
524
+ const n9 = this.findChapter(e14);
525
+ if (!n9) return Promise.resolve("error");
526
+ const r2 = je$1(n9);
527
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
528
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
529
+ const a2 = r2.segments[t2], i2 = a2.documentId;
530
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
531
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
532
+ if (o2) return o2;
533
+ const l = this.startLoadSegment(n9, a2, s2).finally(() => {
534
+ this.pendingSegmentLoadPromises.delete(i2);
530
535
  });
531
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
536
+ return this.pendingSegmentLoadPromises.set(i2, l), l;
532
537
  }
533
538
  ensureSingleDocumentChapter(e14) {
534
539
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -602,24 +607,27 @@ const st$1 = class e extends BasePlugin {
602
607
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(t2, s2);
603
608
  throw new Error(`Chapter "${t2.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
604
609
  }
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());
610
+ async startLoadSegment(t2, s2, n9) {
611
+ var r2;
612
+ const a2 = t2.chapterId, i2 = (null == s2 ? void 0 : s2.index) ?? 0, o2 = (null == s2 ? void 0 : s2.documentId) ?? a2;
613
+ let l = false;
614
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
609
615
  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));
616
+ 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 });
617
+ l || (l = true, null == n9 || n9());
618
+ const u2 = await new Promise((e15, t3) => {
619
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
612
620
  });
613
621
  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);
622
+ u2.task.wait(() => e15(), (e16) => t3(e16));
623
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
624
+ } catch (h2) {
625
+ const t3 = this.chapterStatus.get(a2);
618
626
  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);
627
+ const t4 = h2 instanceof Error ? h2.message : `Failed to load chapter "${a2}"`;
628
+ this.logger.error(e.id, "LoadChapter", t4, h2), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", t4);
621
629
  }
622
- return this.chapterStatus.get(r2) ?? "error";
630
+ return this.chapterStatus.get(a2) ?? "error";
623
631
  }
624
632
  }
625
633
  waitForTerminalStatus(e14) {
@@ -1032,12 +1040,17 @@ const xt$1 = class e2 extends BasePlugin {
1032
1040
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
1033
1041
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
1034
1042
  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;
1043
+ if (!s2) continue;
1044
+ if (!this.ensureVisibleChapterUrls(s2)) {
1045
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
1046
+ continue;
1040
1047
  }
1048
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
1049
+ let n9 = false;
1050
+ const r2 = () => {
1051
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
1052
+ };
1053
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
1041
1054
  }
1042
1055
  }
1043
1056
  prepareChapterForSegmentLoad(e14) {
@@ -1127,21 +1140,21 @@ const xt$1 = class e2 extends BasePlugin {
1127
1140
  ensureSegmentLoaded(e14, t2) {
1128
1141
  return this.loadSegmentNow(e14, t2);
1129
1142
  }
1130
- loadSegmentNow(e14, t2) {
1143
+ loadSegmentNow(e14, t2, s2) {
1131
1144
  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);
1145
+ const n9 = this.findChapter(e14);
1146
+ if (!n9) return Promise.resolve("error");
1147
+ const r2 = Pt$1(n9);
1148
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
1149
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
1150
+ const a2 = r2.segments[t2], i2 = a2.documentId;
1151
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
1152
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
1153
+ if (o2) return o2;
1154
+ const l = this.startLoadSegment(n9, a2, s2).finally(() => {
1155
+ this.pendingSegmentLoadPromises.delete(i2);
1143
1156
  });
1144
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
1157
+ return this.pendingSegmentLoadPromises.set(i2, l), l;
1145
1158
  }
1146
1159
  ensureSingleDocumentChapter(e14) {
1147
1160
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -1215,24 +1228,27 @@ const xt$1 = class e2 extends BasePlugin {
1215
1228
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(t2, s2);
1216
1229
  throw new Error(`Chapter "${t2.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
1217
1230
  }
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());
1231
+ async startLoadSegment(t2, s2, n9) {
1232
+ var r2;
1233
+ const a2 = t2.chapterId, i2 = (null == s2 ? void 0 : s2.index) ?? 0, o2 = (null == s2 ? void 0 : s2.documentId) ?? a2;
1234
+ let l = false;
1235
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
1222
1236
  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));
1237
+ 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 });
1238
+ l || (l = true, null == n9 || n9());
1239
+ const u2 = await new Promise((e15, t3) => {
1240
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
1225
1241
  });
1226
1242
  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);
1243
+ u2.task.wait(() => e15(), (e16) => t3(e16));
1244
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
1245
+ } catch (h2) {
1246
+ const t3 = this.chapterStatus.get(a2);
1231
1247
  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);
1248
+ const t4 = h2 instanceof Error ? h2.message : `Failed to load chapter "${a2}"`;
1249
+ this.logger.error(e2.id, "LoadChapter", t4, h2), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", t4);
1234
1250
  }
1235
- return this.chapterStatus.get(r2) ?? "error";
1251
+ return this.chapterStatus.get(a2) ?? "error";
1236
1252
  }
1237
1253
  }
1238
1254
  waitForTerminalStatus(e14) {
@@ -1835,12 +1851,17 @@ const jt$1 = class e3 extends BasePlugin {
1835
1851
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
1836
1852
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
1837
1853
  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;
1854
+ if (!s2) continue;
1855
+ if (!this.ensureVisibleChapterUrls(s2)) {
1856
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
1857
+ continue;
1843
1858
  }
1859
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
1860
+ let n9 = false;
1861
+ const r2 = () => {
1862
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
1863
+ };
1864
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
1844
1865
  }
1845
1866
  }
1846
1867
  prepareChapterForSegmentLoad(e14) {
@@ -1930,21 +1951,21 @@ const jt$1 = class e3 extends BasePlugin {
1930
1951
  ensureSegmentLoaded(e14, t2) {
1931
1952
  return this.loadSegmentNow(e14, t2);
1932
1953
  }
1933
- loadSegmentNow(e14, t2) {
1954
+ loadSegmentNow(e14, t2, s2) {
1934
1955
  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);
1956
+ const n9 = this.findChapter(e14);
1957
+ if (!n9) return Promise.resolve("error");
1958
+ const r2 = zt$1(n9);
1959
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
1960
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
1961
+ const a2 = r2.segments[t2], i2 = a2.documentId;
1962
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
1963
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
1964
+ if (o2) return o2;
1965
+ const l = this.startLoadSegment(n9, a2, s2).finally(() => {
1966
+ this.pendingSegmentLoadPromises.delete(i2);
1946
1967
  });
1947
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
1968
+ return this.pendingSegmentLoadPromises.set(i2, l), l;
1948
1969
  }
1949
1970
  ensureSingleDocumentChapter(e14) {
1950
1971
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -2018,24 +2039,27 @@ const jt$1 = class e3 extends BasePlugin {
2018
2039
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(t2, s2);
2019
2040
  throw new Error(`Chapter "${t2.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
2020
2041
  }
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());
2042
+ async startLoadSegment(t2, s2, n9) {
2043
+ var r2;
2044
+ const a2 = t2.chapterId, i2 = (null == s2 ? void 0 : s2.index) ?? 0, o2 = (null == s2 ? void 0 : s2.documentId) ?? a2;
2045
+ let l = false;
2046
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
2025
2047
  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));
2048
+ 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 });
2049
+ l || (l = true, null == n9 || n9());
2050
+ const u2 = await new Promise((e15, t3) => {
2051
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
2028
2052
  });
2029
2053
  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);
2054
+ u2.task.wait(() => e15(), (e16) => t3(e16));
2055
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
2056
+ } catch (h2) {
2057
+ const t3 = this.chapterStatus.get(a2);
2034
2058
  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);
2059
+ const t4 = h2 instanceof Error ? h2.message : `Failed to load chapter "${a2}"`;
2060
+ this.logger.error(e3.id, "LoadChapter", t4, h2), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", t4);
2037
2061
  }
2038
- return this.chapterStatus.get(r2) ?? "error";
2062
+ return this.chapterStatus.get(a2) ?? "error";
2039
2063
  }
2040
2064
  }
2041
2065
  waitForTerminalStatus(e14) {
@@ -2447,12 +2471,17 @@ const ms = class e4 extends BasePlugin {
2447
2471
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
2448
2472
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
2449
2473
  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;
2474
+ if (!s2) continue;
2475
+ if (!this.ensureVisibleChapterUrls(s2)) {
2476
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
2477
+ continue;
2455
2478
  }
2479
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
2480
+ let n9 = false;
2481
+ const r2 = () => {
2482
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
2483
+ };
2484
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
2456
2485
  }
2457
2486
  }
2458
2487
  prepareChapterForSegmentLoad(e14) {
@@ -2542,21 +2571,21 @@ const ms = class e4 extends BasePlugin {
2542
2571
  ensureSegmentLoaded(e14, t2) {
2543
2572
  return this.loadSegmentNow(e14, t2);
2544
2573
  }
2545
- loadSegmentNow(e14, t2) {
2574
+ loadSegmentNow(e14, t2, s2) {
2546
2575
  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);
2576
+ const n9 = this.findChapter(e14);
2577
+ if (!n9) return Promise.resolve("error");
2578
+ const r2 = os(n9);
2579
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
2580
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
2581
+ const a2 = r2.segments[t2], i2 = a2.documentId;
2582
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
2583
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
2584
+ if (o2) return o2;
2585
+ const l = this.startLoadSegment(n9, a2, s2).finally(() => {
2586
+ this.pendingSegmentLoadPromises.delete(i2);
2558
2587
  });
2559
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
2588
+ return this.pendingSegmentLoadPromises.set(i2, l), l;
2560
2589
  }
2561
2590
  ensureSingleDocumentChapter(e14) {
2562
2591
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -2630,24 +2659,27 @@ const ms = class e4 extends BasePlugin {
2630
2659
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(t2, s2);
2631
2660
  throw new Error(`Chapter "${t2.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
2632
2661
  }
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());
2662
+ async startLoadSegment(t2, s2, n9) {
2663
+ var r2;
2664
+ const a2 = t2.chapterId, i2 = (null == s2 ? void 0 : s2.index) ?? 0, o2 = (null == s2 ? void 0 : s2.documentId) ?? a2;
2665
+ let l = false;
2666
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
2637
2667
  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));
2668
+ 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 });
2669
+ l || (l = true, null == n9 || n9());
2670
+ const u2 = await new Promise((e15, t3) => {
2671
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
2640
2672
  });
2641
2673
  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);
2674
+ u2.task.wait(() => e15(), (e16) => t3(e16));
2675
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
2676
+ } catch (h2) {
2677
+ const t3 = this.chapterStatus.get(a2);
2646
2678
  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);
2679
+ const t4 = h2 instanceof Error ? h2.message : `Failed to load chapter "${a2}"`;
2680
+ this.logger.error(e4.id, "LoadChapter", t4, h2), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", t4);
2649
2681
  }
2650
- return this.chapterStatus.get(r2) ?? "error";
2682
+ return this.chapterStatus.get(a2) ?? "error";
2651
2683
  }
2652
2684
  }
2653
2685
  waitForTerminalStatus(e14) {
@@ -3363,12 +3395,17 @@ const zs$1 = class e6 extends BasePlugin {
3363
3395
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
3364
3396
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
3365
3397
  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;
3398
+ if (!s2) continue;
3399
+ if (!this.ensureVisibleChapterUrls(s2)) {
3400
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
3401
+ continue;
3371
3402
  }
3403
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
3404
+ let n9 = false;
3405
+ const r2 = () => {
3406
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
3407
+ };
3408
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
3372
3409
  }
3373
3410
  }
3374
3411
  prepareChapterForSegmentLoad(e14) {
@@ -3458,21 +3495,21 @@ const zs$1 = class e6 extends BasePlugin {
3458
3495
  ensureSegmentLoaded(e14, t2) {
3459
3496
  return this.loadSegmentNow(e14, t2);
3460
3497
  }
3461
- loadSegmentNow(e14, t2) {
3498
+ loadSegmentNow(e14, t2, s2) {
3462
3499
  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);
3500
+ const n9 = this.findChapter(e14);
3501
+ if (!n9) return Promise.resolve("error");
3502
+ const r2 = Es$1(n9);
3503
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
3504
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
3505
+ const a2 = r2.segments[t2], i2 = a2.documentId;
3506
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
3507
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
3508
+ if (o2) return o2;
3509
+ const l = this.startLoadSegment(n9, a2, s2).finally(() => {
3510
+ this.pendingSegmentLoadPromises.delete(i2);
3474
3511
  });
3475
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
3512
+ return this.pendingSegmentLoadPromises.set(i2, l), l;
3476
3513
  }
3477
3514
  ensureSingleDocumentChapter(e14) {
3478
3515
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -3546,24 +3583,27 @@ const zs$1 = class e6 extends BasePlugin {
3546
3583
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(t2, s2);
3547
3584
  throw new Error(`Chapter "${t2.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
3548
3585
  }
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());
3586
+ async startLoadSegment(t2, s2, n9) {
3587
+ var r2;
3588
+ const a2 = t2.chapterId, i2 = (null == s2 ? void 0 : s2.index) ?? 0, o2 = (null == s2 ? void 0 : s2.documentId) ?? a2;
3589
+ let l = false;
3590
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
3553
3591
  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));
3592
+ 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 });
3593
+ l || (l = true, null == n9 || n9());
3594
+ const u2 = await new Promise((e15, t3) => {
3595
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
3556
3596
  });
3557
3597
  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);
3598
+ u2.task.wait(() => e15(), (e16) => t3(e16));
3599
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
3600
+ } catch (h2) {
3601
+ const t3 = this.chapterStatus.get(a2);
3562
3602
  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);
3603
+ const t4 = h2 instanceof Error ? h2.message : `Failed to load chapter "${a2}"`;
3604
+ this.logger.error(e6.id, "LoadChapter", t4, h2), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", t4);
3565
3605
  }
3566
- return this.chapterStatus.get(r2) ?? "error";
3606
+ return this.chapterStatus.get(a2) ?? "error";
3567
3607
  }
3568
3608
  }
3569
3609
  waitForTerminalStatus(e14) {
@@ -3975,12 +4015,17 @@ const ln = class e7 extends BasePlugin {
3975
4015
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
3976
4016
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
3977
4017
  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;
4018
+ if (!s2) continue;
4019
+ if (!this.ensureVisibleChapterUrls(s2)) {
4020
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
4021
+ continue;
3983
4022
  }
4023
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
4024
+ let n9 = false;
4025
+ const r2 = () => {
4026
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
4027
+ };
4028
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
3984
4029
  }
3985
4030
  }
3986
4031
  prepareChapterForSegmentLoad(e14) {
@@ -4070,21 +4115,21 @@ const ln = class e7 extends BasePlugin {
4070
4115
  ensureSegmentLoaded(e14, t2) {
4071
4116
  return this.loadSegmentNow(e14, t2);
4072
4117
  }
4073
- loadSegmentNow(e14, t2) {
4118
+ loadSegmentNow(e14, t2, s2) {
4074
4119
  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);
4120
+ const n9 = this.findChapter(e14);
4121
+ if (!n9) return Promise.resolve("error");
4122
+ const r2 = Js(n9);
4123
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
4124
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
4125
+ const a2 = r2.segments[t2], i2 = a2.documentId;
4126
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
4127
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
4128
+ if (o2) return o2;
4129
+ const l = this.startLoadSegment(n9, a2, s2).finally(() => {
4130
+ this.pendingSegmentLoadPromises.delete(i2);
4086
4131
  });
4087
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
4132
+ return this.pendingSegmentLoadPromises.set(i2, l), l;
4088
4133
  }
4089
4134
  ensureSingleDocumentChapter(e14) {
4090
4135
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -4158,24 +4203,27 @@ const ln = class e7 extends BasePlugin {
4158
4203
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(t2, s2);
4159
4204
  throw new Error(`Chapter "${t2.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
4160
4205
  }
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());
4206
+ async startLoadSegment(t2, s2, n9) {
4207
+ var r2;
4208
+ const a2 = t2.chapterId, i2 = (null == s2 ? void 0 : s2.index) ?? 0, o2 = (null == s2 ? void 0 : s2.documentId) ?? a2;
4209
+ let l = false;
4210
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
4165
4211
  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));
4212
+ 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 });
4213
+ l || (l = true, null == n9 || n9());
4214
+ const u2 = await new Promise((e15, t3) => {
4215
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
4168
4216
  });
4169
4217
  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);
4218
+ u2.task.wait(() => e15(), (e16) => t3(e16));
4219
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
4220
+ } catch (h2) {
4221
+ const t3 = this.chapterStatus.get(a2);
4174
4222
  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);
4223
+ const t4 = h2 instanceof Error ? h2.message : `Failed to load chapter "${a2}"`;
4224
+ this.logger.error(e7.id, "LoadChapter", t4, h2), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", t4);
4177
4225
  }
4178
- return this.chapterStatus.get(r2) ?? "error";
4226
+ return this.chapterStatus.get(a2) ?? "error";
4179
4227
  }
4180
4228
  }
4181
4229
  waitForTerminalStatus(e14) {
@@ -5248,12 +5296,17 @@ const pr = class e9 extends BasePlugin {
5248
5296
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
5249
5297
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
5250
5298
  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;
5299
+ if (!s2) continue;
5300
+ if (!this.ensureVisibleChapterUrls(s2)) {
5301
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
5302
+ continue;
5256
5303
  }
5304
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
5305
+ let n9 = false;
5306
+ const r2 = () => {
5307
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
5308
+ };
5309
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
5257
5310
  }
5258
5311
  }
5259
5312
  prepareChapterForSegmentLoad(e14) {
@@ -5343,21 +5396,21 @@ const pr = class e9 extends BasePlugin {
5343
5396
  ensureSegmentLoaded(e14, t2) {
5344
5397
  return this.loadSegmentNow(e14, t2);
5345
5398
  }
5346
- loadSegmentNow(e14, t2) {
5399
+ loadSegmentNow(e14, t2, s2) {
5347
5400
  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);
5401
+ const n9 = this.findChapter(e14);
5402
+ if (!n9) return Promise.resolve("error");
5403
+ const r2 = ar(n9);
5404
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
5405
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
5406
+ const a2 = r2.segments[t2], i2 = a2.documentId;
5407
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
5408
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
5409
+ if (o2) return o2;
5410
+ const l = this.startLoadSegment(n9, a2, s2).finally(() => {
5411
+ this.pendingSegmentLoadPromises.delete(i2);
5359
5412
  });
5360
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
5413
+ return this.pendingSegmentLoadPromises.set(i2, l), l;
5361
5414
  }
5362
5415
  ensureSingleDocumentChapter(e14) {
5363
5416
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -5431,24 +5484,27 @@ const pr = class e9 extends BasePlugin {
5431
5484
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(t2, s2);
5432
5485
  throw new Error(`Chapter "${t2.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
5433
5486
  }
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());
5487
+ async startLoadSegment(t2, s2, n9) {
5488
+ var r2;
5489
+ const a2 = t2.chapterId, i2 = (null == s2 ? void 0 : s2.index) ?? 0, o2 = (null == s2 ? void 0 : s2.documentId) ?? a2;
5490
+ let l = false;
5491
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
5438
5492
  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));
5493
+ 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 });
5494
+ l || (l = true, null == n9 || n9());
5495
+ const u2 = await new Promise((e15, t3) => {
5496
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
5441
5497
  });
5442
5498
  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);
5499
+ u2.task.wait(() => e15(), (e16) => t3(e16));
5500
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
5501
+ } catch (h2) {
5502
+ const t3 = this.chapterStatus.get(a2);
5447
5503
  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);
5504
+ const t4 = h2 instanceof Error ? h2.message : `Failed to load chapter "${a2}"`;
5505
+ this.logger.error(e9.id, "LoadChapter", t4, h2), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", t4);
5450
5506
  }
5451
- return this.chapterStatus.get(r2) ?? "error";
5507
+ return this.chapterStatus.get(a2) ?? "error";
5452
5508
  }
5453
5509
  }
5454
5510
  waitForTerminalStatus(e14) {
@@ -6072,12 +6128,17 @@ const Tr = class e10 extends BasePlugin {
6072
6128
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
6073
6129
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
6074
6130
  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;
6131
+ if (!s2) continue;
6132
+ if (!this.ensureVisibleChapterUrls(s2)) {
6133
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
6134
+ continue;
6080
6135
  }
6136
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
6137
+ let n9 = false;
6138
+ const r2 = () => {
6139
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
6140
+ };
6141
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
6081
6142
  }
6082
6143
  }
6083
6144
  prepareChapterForSegmentLoad(e14) {
@@ -6167,21 +6228,21 @@ const Tr = class e10 extends BasePlugin {
6167
6228
  ensureSegmentLoaded(e14, t2) {
6168
6229
  return this.loadSegmentNow(e14, t2);
6169
6230
  }
6170
- loadSegmentNow(e14, t2) {
6231
+ loadSegmentNow(e14, t2, s2) {
6171
6232
  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);
6233
+ const n9 = this.findChapter(e14);
6234
+ if (!n9) return Promise.resolve("error");
6235
+ const r2 = Lr(n9);
6236
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
6237
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
6238
+ const a2 = r2.segments[t2], i2 = a2.documentId;
6239
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
6240
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
6241
+ if (o2) return o2;
6242
+ const l = this.startLoadSegment(n9, a2, s2).finally(() => {
6243
+ this.pendingSegmentLoadPromises.delete(i2);
6183
6244
  });
6184
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
6245
+ return this.pendingSegmentLoadPromises.set(i2, l), l;
6185
6246
  }
6186
6247
  ensureSingleDocumentChapter(e14) {
6187
6248
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -6255,24 +6316,27 @@ const Tr = class e10 extends BasePlugin {
6255
6316
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(t2, s2);
6256
6317
  throw new Error(`Chapter "${t2.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
6257
6318
  }
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());
6319
+ async startLoadSegment(t2, s2, n9) {
6320
+ var r2;
6321
+ const a2 = t2.chapterId, i2 = (null == s2 ? void 0 : s2.index) ?? 0, o2 = (null == s2 ? void 0 : s2.documentId) ?? a2;
6322
+ let l = false;
6323
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
6262
6324
  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));
6325
+ 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 });
6326
+ l || (l = true, null == n9 || n9());
6327
+ const u2 = await new Promise((e15, t3) => {
6328
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
6265
6329
  });
6266
6330
  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);
6331
+ u2.task.wait(() => e15(), (e16) => t3(e16));
6332
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
6333
+ } catch (h2) {
6334
+ const t3 = this.chapterStatus.get(a2);
6271
6335
  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);
6336
+ const t4 = h2 instanceof Error ? h2.message : `Failed to load chapter "${a2}"`;
6337
+ this.logger.error(e10.id, "LoadChapter", t4, h2), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", t4);
6274
6338
  }
6275
- return this.chapterStatus.get(r2) ?? "error";
6339
+ return this.chapterStatus.get(a2) ?? "error";
6276
6340
  }
6277
6341
  }
6278
6342
  waitForTerminalStatus(e14) {
@@ -6684,12 +6748,17 @@ const sa = class e11 extends BasePlugin {
6684
6748
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
6685
6749
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
6686
6750
  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;
6751
+ if (!s2) continue;
6752
+ if (!this.ensureVisibleChapterUrls(s2)) {
6753
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
6754
+ continue;
6692
6755
  }
6756
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
6757
+ let n9 = false;
6758
+ const r2 = () => {
6759
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
6760
+ };
6761
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
6693
6762
  }
6694
6763
  }
6695
6764
  prepareChapterForSegmentLoad(e14) {
@@ -6779,21 +6848,21 @@ const sa = class e11 extends BasePlugin {
6779
6848
  ensureSegmentLoaded(e14, t2) {
6780
6849
  return this.loadSegmentNow(e14, t2);
6781
6850
  }
6782
- loadSegmentNow(e14, t2) {
6851
+ loadSegmentNow(e14, t2, s2) {
6783
6852
  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);
6853
+ const n9 = this.findChapter(e14);
6854
+ if (!n9) return Promise.resolve("error");
6855
+ const r2 = jr(n9);
6856
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
6857
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
6858
+ const a2 = r2.segments[t2], i2 = a2.documentId;
6859
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
6860
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
6861
+ if (o2) return o2;
6862
+ const l = this.startLoadSegment(n9, a2, s2).finally(() => {
6863
+ this.pendingSegmentLoadPromises.delete(i2);
6795
6864
  });
6796
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
6865
+ return this.pendingSegmentLoadPromises.set(i2, l), l;
6797
6866
  }
6798
6867
  ensureSingleDocumentChapter(e14) {
6799
6868
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -6867,24 +6936,27 @@ const sa = class e11 extends BasePlugin {
6867
6936
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(t2, s2);
6868
6937
  throw new Error(`Chapter "${t2.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
6869
6938
  }
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());
6939
+ async startLoadSegment(t2, s2, n9) {
6940
+ var r2;
6941
+ const a2 = t2.chapterId, i2 = (null == s2 ? void 0 : s2.index) ?? 0, o2 = (null == s2 ? void 0 : s2.documentId) ?? a2;
6942
+ let l = false;
6943
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
6874
6944
  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));
6945
+ 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 });
6946
+ l || (l = true, null == n9 || n9());
6947
+ const u2 = await new Promise((e15, t3) => {
6948
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
6877
6949
  });
6878
6950
  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);
6951
+ u2.task.wait(() => e15(), (e16) => t3(e16));
6952
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
6953
+ } catch (h2) {
6954
+ const t3 = this.chapterStatus.get(a2);
6883
6955
  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);
6956
+ const t4 = h2 instanceof Error ? h2.message : `Failed to load chapter "${a2}"`;
6957
+ this.logger.error(e11.id, "LoadChapter", t4, h2), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", t4);
6886
6958
  }
6887
- return this.chapterStatus.get(r2) ?? "error";
6959
+ return this.chapterStatus.get(a2) ?? "error";
6888
6960
  }
6889
6961
  }
6890
6962
  waitForTerminalStatus(e14) {
@@ -8040,12 +8112,17 @@ const B = class n extends BasePlugin {
8040
8112
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
8041
8113
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
8042
8114
  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;
8115
+ if (!s2) continue;
8116
+ if (!this.ensureVisibleChapterUrls(s2)) {
8117
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
8118
+ continue;
8048
8119
  }
8120
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
8121
+ let n9 = false;
8122
+ const r2 = () => {
8123
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
8124
+ };
8125
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
8049
8126
  }
8050
8127
  }
8051
8128
  prepareChapterForSegmentLoad(e14) {
@@ -8135,21 +8212,21 @@ const B = class n extends BasePlugin {
8135
8212
  ensureSegmentLoaded(e14, t2) {
8136
8213
  return this.loadSegmentNow(e14, t2);
8137
8214
  }
8138
- loadSegmentNow(e14, t2) {
8215
+ loadSegmentNow(e14, t2, s2) {
8139
8216
  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);
8217
+ const n9 = this.findChapter(e14);
8218
+ if (!n9) return Promise.resolve("error");
8219
+ const r2 = E(n9);
8220
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
8221
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
8222
+ const a2 = r2.segments[t2], i2 = a2.documentId;
8223
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
8224
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
8225
+ if (o2) return o2;
8226
+ const h2 = this.startLoadSegment(n9, a2, s2).finally(() => {
8227
+ this.pendingSegmentLoadPromises.delete(i2);
8151
8228
  });
8152
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
8229
+ return this.pendingSegmentLoadPromises.set(i2, h2), h2;
8153
8230
  }
8154
8231
  ensureSingleDocumentChapter(e14) {
8155
8232
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -8223,24 +8300,27 @@ const B = class n extends BasePlugin {
8223
8300
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(e14, t2);
8224
8301
  throw new Error(`Chapter "${e14.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
8225
8302
  }
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());
8303
+ async startLoadSegment(e14, t2, s2) {
8304
+ var r2;
8305
+ const a2 = e14.chapterId, i2 = (null == t2 ? void 0 : t2.index) ?? 0, o2 = (null == t2 ? void 0 : t2.documentId) ?? a2;
8306
+ let h2 = false;
8307
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
8230
8308
  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));
8309
+ 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 });
8310
+ h2 || (h2 = true, null == s2 || s2());
8311
+ const u2 = await new Promise((e15, t3) => {
8312
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
8233
8313
  });
8234
8314
  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);
8315
+ u2.task.wait(() => e15(), (e16) => t3(e16));
8316
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
8317
+ } catch (l) {
8318
+ const e15 = this.chapterStatus.get(a2);
8239
8319
  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);
8320
+ const e16 = l instanceof Error ? l.message : `Failed to load chapter "${a2}"`;
8321
+ this.logger.error(n.id, "LoadChapter", e16, l), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", e16);
8242
8322
  }
8243
- return this.chapterStatus.get(r2) ?? "error";
8323
+ return this.chapterStatus.get(a2) ?? "error";
8244
8324
  }
8245
8325
  }
8246
8326
  waitForTerminalStatus(e14) {
@@ -8652,12 +8732,17 @@ const ce = class n2 extends BasePlugin {
8652
8732
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
8653
8733
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
8654
8734
  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;
8735
+ if (!s2) continue;
8736
+ if (!this.ensureVisibleChapterUrls(s2)) {
8737
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
8738
+ continue;
8660
8739
  }
8740
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
8741
+ let n9 = false;
8742
+ const r2 = () => {
8743
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
8744
+ };
8745
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
8661
8746
  }
8662
8747
  }
8663
8748
  prepareChapterForSegmentLoad(e14) {
@@ -8747,21 +8832,21 @@ const ce = class n2 extends BasePlugin {
8747
8832
  ensureSegmentLoaded(e14, t2) {
8748
8833
  return this.loadSegmentNow(e14, t2);
8749
8834
  }
8750
- loadSegmentNow(e14, t2) {
8835
+ loadSegmentNow(e14, t2, s2) {
8751
8836
  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);
8837
+ const n9 = this.findChapter(e14);
8838
+ if (!n9) return Promise.resolve("error");
8839
+ const r2 = ne(n9);
8840
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
8841
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
8842
+ const a2 = r2.segments[t2], i2 = a2.documentId;
8843
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
8844
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
8845
+ if (o2) return o2;
8846
+ const h2 = this.startLoadSegment(n9, a2, s2).finally(() => {
8847
+ this.pendingSegmentLoadPromises.delete(i2);
8763
8848
  });
8764
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
8849
+ return this.pendingSegmentLoadPromises.set(i2, h2), h2;
8765
8850
  }
8766
8851
  ensureSingleDocumentChapter(e14) {
8767
8852
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -8835,24 +8920,27 @@ const ce = class n2 extends BasePlugin {
8835
8920
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(e14, t2);
8836
8921
  throw new Error(`Chapter "${e14.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
8837
8922
  }
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());
8923
+ async startLoadSegment(e14, t2, s2) {
8924
+ var r2;
8925
+ const a2 = e14.chapterId, i2 = (null == t2 ? void 0 : t2.index) ?? 0, o2 = (null == t2 ? void 0 : t2.documentId) ?? a2;
8926
+ let h2 = false;
8927
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
8842
8928
  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));
8929
+ 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 });
8930
+ h2 || (h2 = true, null == s2 || s2());
8931
+ const u2 = await new Promise((e15, t3) => {
8932
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
8845
8933
  });
8846
8934
  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);
8935
+ u2.task.wait(() => e15(), (e16) => t3(e16));
8936
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
8937
+ } catch (l) {
8938
+ const e15 = this.chapterStatus.get(a2);
8851
8939
  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);
8940
+ const e16 = l instanceof Error ? l.message : `Failed to load chapter "${a2}"`;
8941
+ this.logger.error(n2.id, "LoadChapter", e16, l), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", e16);
8854
8942
  }
8855
- return this.chapterStatus.get(r2) ?? "error";
8943
+ return this.chapterStatus.get(a2) ?? "error";
8856
8944
  }
8857
8945
  }
8858
8946
  waitForTerminalStatus(e14) {
@@ -9452,12 +9540,17 @@ const Ee = class n3 extends BasePlugin {
9452
9540
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
9453
9541
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
9454
9542
  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;
9543
+ if (!s2) continue;
9544
+ if (!this.ensureVisibleChapterUrls(s2)) {
9545
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
9546
+ continue;
9460
9547
  }
9548
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
9549
+ let n9 = false;
9550
+ const r2 = () => {
9551
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
9552
+ };
9553
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
9461
9554
  }
9462
9555
  }
9463
9556
  prepareChapterForSegmentLoad(e14) {
@@ -9547,21 +9640,21 @@ const Ee = class n3 extends BasePlugin {
9547
9640
  ensureSegmentLoaded(e14, t2) {
9548
9641
  return this.loadSegmentNow(e14, t2);
9549
9642
  }
9550
- loadSegmentNow(e14, t2) {
9643
+ loadSegmentNow(e14, t2, s2) {
9551
9644
  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);
9645
+ const n9 = this.findChapter(e14);
9646
+ if (!n9) return Promise.resolve("error");
9647
+ const r2 = Me(n9);
9648
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
9649
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
9650
+ const a2 = r2.segments[t2], i2 = a2.documentId;
9651
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
9652
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
9653
+ if (o2) return o2;
9654
+ const h2 = this.startLoadSegment(n9, a2, s2).finally(() => {
9655
+ this.pendingSegmentLoadPromises.delete(i2);
9563
9656
  });
9564
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
9657
+ return this.pendingSegmentLoadPromises.set(i2, h2), h2;
9565
9658
  }
9566
9659
  ensureSingleDocumentChapter(e14) {
9567
9660
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -9635,24 +9728,27 @@ const Ee = class n3 extends BasePlugin {
9635
9728
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(e14, t2);
9636
9729
  throw new Error(`Chapter "${e14.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
9637
9730
  }
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());
9731
+ async startLoadSegment(e14, t2, s2) {
9732
+ var r2;
9733
+ const a2 = e14.chapterId, i2 = (null == t2 ? void 0 : t2.index) ?? 0, o2 = (null == t2 ? void 0 : t2.documentId) ?? a2;
9734
+ let h2 = false;
9735
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
9642
9736
  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));
9737
+ 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 });
9738
+ h2 || (h2 = true, null == s2 || s2());
9739
+ const u2 = await new Promise((e15, t3) => {
9740
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
9645
9741
  });
9646
9742
  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);
9743
+ u2.task.wait(() => e15(), (e16) => t3(e16));
9744
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
9745
+ } catch (l) {
9746
+ const e15 = this.chapterStatus.get(a2);
9651
9747
  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);
9748
+ const e16 = l instanceof Error ? l.message : `Failed to load chapter "${a2}"`;
9749
+ this.logger.error(n3.id, "LoadChapter", e16, l), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", e16);
9654
9750
  }
9655
- return this.chapterStatus.get(r2) ?? "error";
9751
+ return this.chapterStatus.get(a2) ?? "error";
9656
9752
  }
9657
9753
  }
9658
9754
  waitForTerminalStatus(e14) {
@@ -10064,12 +10160,17 @@ const et2 = class n4 extends BasePlugin {
10064
10160
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
10065
10161
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
10066
10162
  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;
10163
+ if (!s2) continue;
10164
+ if (!this.ensureVisibleChapterUrls(s2)) {
10165
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
10166
+ continue;
10072
10167
  }
10168
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
10169
+ let n9 = false;
10170
+ const r2 = () => {
10171
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
10172
+ };
10173
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
10073
10174
  }
10074
10175
  }
10075
10176
  prepareChapterForSegmentLoad(e14) {
@@ -10159,21 +10260,21 @@ const et2 = class n4 extends BasePlugin {
10159
10260
  ensureSegmentLoaded(e14, t2) {
10160
10261
  return this.loadSegmentNow(e14, t2);
10161
10262
  }
10162
- loadSegmentNow(e14, t2) {
10263
+ loadSegmentNow(e14, t2, s2) {
10163
10264
  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);
10265
+ const n9 = this.findChapter(e14);
10266
+ if (!n9) return Promise.resolve("error");
10267
+ const r2 = Ke(n9);
10268
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
10269
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
10270
+ const a2 = r2.segments[t2], i2 = a2.documentId;
10271
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
10272
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
10273
+ if (o2) return o2;
10274
+ const h2 = this.startLoadSegment(n9, a2, s2).finally(() => {
10275
+ this.pendingSegmentLoadPromises.delete(i2);
10175
10276
  });
10176
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
10277
+ return this.pendingSegmentLoadPromises.set(i2, h2), h2;
10177
10278
  }
10178
10279
  ensureSingleDocumentChapter(e14) {
10179
10280
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -10247,24 +10348,27 @@ const et2 = class n4 extends BasePlugin {
10247
10348
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(e14, t2);
10248
10349
  throw new Error(`Chapter "${e14.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
10249
10350
  }
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());
10351
+ async startLoadSegment(e14, t2, s2) {
10352
+ var r2;
10353
+ const a2 = e14.chapterId, i2 = (null == t2 ? void 0 : t2.index) ?? 0, o2 = (null == t2 ? void 0 : t2.documentId) ?? a2;
10354
+ let h2 = false;
10355
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
10254
10356
  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));
10357
+ 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 });
10358
+ h2 || (h2 = true, null == s2 || s2());
10359
+ const u2 = await new Promise((e15, t3) => {
10360
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
10257
10361
  });
10258
10362
  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);
10363
+ u2.task.wait(() => e15(), (e16) => t3(e16));
10364
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
10365
+ } catch (l) {
10366
+ const e15 = this.chapterStatus.get(a2);
10263
10367
  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);
10368
+ const e16 = l instanceof Error ? l.message : `Failed to load chapter "${a2}"`;
10369
+ this.logger.error(n4.id, "LoadChapter", e16, l), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", e16);
10266
10370
  }
10267
- return this.chapterStatus.get(r2) ?? "error";
10371
+ return this.chapterStatus.get(a2) ?? "error";
10268
10372
  }
10269
10373
  }
10270
10374
  waitForTerminalStatus(e14) {
@@ -10979,12 +11083,17 @@ const Mt = class n6 extends BasePlugin {
10979
11083
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
10980
11084
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
10981
11085
  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;
11086
+ if (!s2) continue;
11087
+ if (!this.ensureVisibleChapterUrls(s2)) {
11088
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
11089
+ continue;
10987
11090
  }
11091
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
11092
+ let n9 = false;
11093
+ const r2 = () => {
11094
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
11095
+ };
11096
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
10988
11097
  }
10989
11098
  }
10990
11099
  prepareChapterForSegmentLoad(e14) {
@@ -11074,21 +11183,21 @@ const Mt = class n6 extends BasePlugin {
11074
11183
  ensureSegmentLoaded(e14, t2) {
11075
11184
  return this.loadSegmentNow(e14, t2);
11076
11185
  }
11077
- loadSegmentNow(e14, t2) {
11186
+ loadSegmentNow(e14, t2, s2) {
11078
11187
  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);
11188
+ const n9 = this.findChapter(e14);
11189
+ if (!n9) return Promise.resolve("error");
11190
+ const r2 = vt(n9);
11191
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
11192
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
11193
+ const a2 = r2.segments[t2], i2 = a2.documentId;
11194
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
11195
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
11196
+ if (o2) return o2;
11197
+ const h2 = this.startLoadSegment(n9, a2, s2).finally(() => {
11198
+ this.pendingSegmentLoadPromises.delete(i2);
11090
11199
  });
11091
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
11200
+ return this.pendingSegmentLoadPromises.set(i2, h2), h2;
11092
11201
  }
11093
11202
  ensureSingleDocumentChapter(e14) {
11094
11203
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -11162,24 +11271,27 @@ const Mt = class n6 extends BasePlugin {
11162
11271
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(e14, t2);
11163
11272
  throw new Error(`Chapter "${e14.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
11164
11273
  }
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());
11274
+ async startLoadSegment(e14, t2, s2) {
11275
+ var r2;
11276
+ const a2 = e14.chapterId, i2 = (null == t2 ? void 0 : t2.index) ?? 0, o2 = (null == t2 ? void 0 : t2.documentId) ?? a2;
11277
+ let h2 = false;
11278
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
11169
11279
  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));
11280
+ 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 });
11281
+ h2 || (h2 = true, null == s2 || s2());
11282
+ const u2 = await new Promise((e15, t3) => {
11283
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
11172
11284
  });
11173
11285
  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);
11286
+ u2.task.wait(() => e15(), (e16) => t3(e16));
11287
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
11288
+ } catch (l) {
11289
+ const e15 = this.chapterStatus.get(a2);
11178
11290
  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);
11291
+ const e16 = l instanceof Error ? l.message : `Failed to load chapter "${a2}"`;
11292
+ this.logger.error(n6.id, "LoadChapter", e16, l), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", e16);
11181
11293
  }
11182
- return this.chapterStatus.get(r2) ?? "error";
11294
+ return this.chapterStatus.get(a2) ?? "error";
11183
11295
  }
11184
11296
  }
11185
11297
  waitForTerminalStatus(e14) {
@@ -11591,12 +11703,17 @@ const Kt = class n7 extends BasePlugin {
11591
11703
  if (this.isSegmentDocumentOpen(e15.chapterId, e15.segmentIndex)) continue;
11592
11704
  if (this.isSegmentLoadPending(e15.chapterId, e15.segmentIndex)) continue;
11593
11705
  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;
11706
+ if (!s2) continue;
11707
+ if (!this.ensureVisibleChapterUrls(s2)) {
11708
+ if (this.visibleSegmentBacklog.push(e15), t2++, t2 >= this.visibleSegmentBacklog.length) break;
11709
+ continue;
11599
11710
  }
11711
+ t2 = 0, this.prepareChapterForSegmentLoad(e15.chapterId), this.activeVisibleSegmentLoads++;
11712
+ let n9 = false;
11713
+ const r2 = () => {
11714
+ n9 || (n9 = true, this.activeVisibleSegmentLoads--, this.pumpVisibleSegmentBacklog());
11715
+ };
11716
+ this.loadSegmentNow(e15.chapterId, e15.segmentIndex, r2).finally(r2);
11600
11717
  }
11601
11718
  }
11602
11719
  prepareChapterForSegmentLoad(e14) {
@@ -11686,21 +11803,21 @@ const Kt = class n7 extends BasePlugin {
11686
11803
  ensureSegmentLoaded(e14, t2) {
11687
11804
  return this.loadSegmentNow(e14, t2);
11688
11805
  }
11689
- loadSegmentNow(e14, t2) {
11806
+ loadSegmentNow(e14, t2, s2) {
11690
11807
  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);
11808
+ const n9 = this.findChapter(e14);
11809
+ if (!n9) return Promise.resolve("error");
11810
+ const r2 = Ot(n9);
11811
+ if (!r2) return 0 !== t2 ? Promise.resolve(this.isSegmentDocumentOpen(e14, 0) ? "loaded" : "error") : this.ensureSingleDocumentChapter(n9);
11812
+ if (t2 < 0 || t2 >= r2.segments.length) return Promise.resolve("error");
11813
+ const a2 = r2.segments[t2], i2 = a2.documentId;
11814
+ if (this.documentManager.provides().isDocumentOpen(i2)) return this.chapterLastUsed.set(e14, Date.now()), 0 === t2 && this.syncChapterStatusFromDocuments(e14), Promise.resolve("loaded");
11815
+ const o2 = this.pendingSegmentLoadPromises.get(i2);
11816
+ if (o2) return o2;
11817
+ const h2 = this.startLoadSegment(n9, a2, s2).finally(() => {
11818
+ this.pendingSegmentLoadPromises.delete(i2);
11702
11819
  });
11703
- return this.pendingSegmentLoadPromises.set(a2, o2), o2;
11820
+ return this.pendingSegmentLoadPromises.set(i2, h2), h2;
11704
11821
  }
11705
11822
  ensureSingleDocumentChapter(e14) {
11706
11823
  const t2 = e14.chapterId, s2 = this.pendingChapterLoadPromises.get(t2);
@@ -11774,24 +11891,27 @@ const Kt = class n7 extends BasePlugin {
11774
11891
  if (null == r2 ? void 0 : r2.loadPdf) return r2.loadPdf(e14, t2);
11775
11892
  throw new Error(`Chapter "${e14.chapterId}" has no PDF source; configure source or chapterPdfLoader`);
11776
11893
  }
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());
11894
+ async startLoadSegment(e14, t2, s2) {
11895
+ var r2;
11896
+ const a2 = e14.chapterId, i2 = (null == t2 ? void 0 : t2.index) ?? 0, o2 = (null == t2 ? void 0 : t2.documentId) ?? a2;
11897
+ let h2 = false;
11898
+ this.chapterErrorMessages.delete(a2), this.anySegmentOpen(a2) || this.updateStatus(a2, "loading"), this.chapterLastUsed.set(a2, Date.now());
11781
11899
  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));
11900
+ 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 });
11901
+ h2 || (h2 = true, null == s2 || s2());
11902
+ const u2 = await new Promise((e15, t3) => {
11903
+ c2.wait((t4) => e15(t4), (e16) => t3(e16));
11784
11904
  });
11785
11905
  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);
11906
+ u2.task.wait(() => e15(), (e16) => t3(e16));
11907
+ }), d2.isDocumentOpen(o2) && this.syncChapterStatusFromDocuments(a2), this.waitForTerminalStatus(a2);
11908
+ } catch (l) {
11909
+ const e15 = this.chapterStatus.get(a2);
11790
11910
  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);
11911
+ const e16 = l instanceof Error ? l.message : `Failed to load chapter "${a2}"`;
11912
+ this.logger.error(n7.id, "LoadChapter", e16, l), 0 !== i2 || this.isAnySegmentWorkPending(a2) || this.updateStatus(a2, "error", e16);
11793
11913
  }
11794
- return this.chapterStatus.get(r2) ?? "error";
11914
+ return this.chapterStatus.get(a2) ?? "error";
11795
11915
  }
11796
11916
  }
11797
11917
  waitForTerminalStatus(e14) {
@@ -12901,10 +13021,14 @@ const _ChapterManagerPlugin = class _ChapterManagerPlugin2 extends BasePlugin {
12901
13021
  stalled = 0;
12902
13022
  this.prepareChapterForSegmentLoad(t2.chapterId);
12903
13023
  this.activeVisibleSegmentLoads++;
12904
- void this.loadSegmentNow(t2.chapterId, t2.segmentIndex).finally(() => {
13024
+ let released = false;
13025
+ const releaseSlot = () => {
13026
+ if (released) return;
13027
+ released = true;
12905
13028
  this.activeVisibleSegmentLoads--;
12906
13029
  this.pumpVisibleSegmentBacklog();
12907
- });
13030
+ };
13031
+ void this.loadSegmentNow(t2.chapterId, t2.segmentIndex, releaseSlot).finally(releaseSlot);
12908
13032
  }
12909
13033
  }
12910
13034
  prepareChapterForSegmentLoad(chapterId) {
@@ -13026,7 +13150,7 @@ const _ChapterManagerPlugin = class _ChapterManagerPlugin2 extends BasePlugin {
13026
13150
  ensureSegmentLoaded(chapterId, segmentIndex) {
13027
13151
  return this.loadSegmentNow(chapterId, segmentIndex);
13028
13152
  }
13029
- loadSegmentNow(chapterId, segmentIndex) {
13153
+ loadSegmentNow(chapterId, segmentIndex, onDispatched) {
13030
13154
  this.prepareChapterForSegmentLoad(chapterId);
13031
13155
  const chapter = this.findChapter(chapterId);
13032
13156
  if (!chapter) return Promise.resolve("error");
@@ -13050,7 +13174,7 @@ const _ChapterManagerPlugin = class _ChapterManagerPlugin2 extends BasePlugin {
13050
13174
  }
13051
13175
  const existing = this.pendingSegmentLoadPromises.get(documentId);
13052
13176
  if (existing) return existing;
13053
- const promise = this.startLoadSegment(chapter, segment).finally(() => {
13177
+ const promise = this.startLoadSegment(chapter, segment, onDispatched).finally(() => {
13054
13178
  this.pendingSegmentLoadPromises.delete(documentId);
13055
13179
  });
13056
13180
  this.pendingSegmentLoadPromises.set(documentId, promise);
@@ -13175,11 +13299,17 @@ const _ChapterManagerPlugin = class _ChapterManagerPlugin2 extends BasePlugin {
13175
13299
  `Chapter "${chapter.chapterId}" has no PDF source; configure source or chapterPdfLoader`
13176
13300
  );
13177
13301
  }
13178
- async startLoadSegment(chapter, segment) {
13302
+ async startLoadSegment(chapter, segment, onDispatched) {
13179
13303
  var _a2;
13180
13304
  const chapterId = chapter.chapterId;
13181
13305
  const segmentIndex = (segment == null ? void 0 : segment.index) ?? 0;
13182
13306
  const documentId = (segment == null ? void 0 : segment.documentId) ?? chapterId;
13307
+ let dispatched = false;
13308
+ const markDispatched = () => {
13309
+ if (dispatched) return;
13310
+ dispatched = true;
13311
+ onDispatched == null ? void 0 : onDispatched();
13312
+ };
13183
13313
  this.chapterErrorMessages.delete(chapterId);
13184
13314
  if (!this.anySegmentOpen(chapterId)) {
13185
13315
  this.updateStatus(chapterId, "loading");
@@ -13202,6 +13332,7 @@ const _ChapterManagerPlugin = class _ChapterManagerPlugin2 extends BasePlugin {
13202
13332
  password: cachedPwd ?? void 0,
13203
13333
  autoActivate: false
13204
13334
  });
13335
+ markDispatched();
13205
13336
  const openResult = await new Promise((resolve, reject) => {
13206
13337
  openTask.wait((res) => resolve(res), (err) => reject(err));
13207
13338
  });