pdfjs-reader-core 0.2.14 → 0.2.16

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
@@ -78,13 +78,14 @@ async function loadDocument(options) {
78
78
  const cachedDoc = documentCache.get(cacheKey);
79
79
  try {
80
80
  const numPages = cachedDoc.numPages;
81
- if (numPages > 0) {
82
- await cachedDoc.getPage(1);
81
+ const isDestroyed = cachedDoc._transport?.destroyed || cachedDoc.destroyed;
82
+ if (numPages > 0 && !isDestroyed) {
83
83
  return {
84
84
  document: cachedDoc,
85
85
  numPages
86
86
  };
87
87
  }
88
+ documentCache.delete(cacheKey);
88
89
  } catch {
89
90
  documentCache.delete(cacheKey);
90
91
  }
@@ -10196,23 +10197,41 @@ var init_PDFViewerClient = __esm({
10196
10197
  abortControllerRef.current = abortController;
10197
10198
  const currentDoc = viewerStore.getState().document;
10198
10199
  if (currentDoc) {
10199
- currentDoc.destroy();
10200
- viewerStore.getState().setDocument(null);
10200
+ try {
10201
+ currentDoc.destroy();
10202
+ } catch {
10203
+ }
10201
10204
  }
10202
- viewerStore.getState().setLoading(true, { phase: "initializing" });
10203
- viewerStore.getState().setError(null);
10205
+ viewerStore.setState({
10206
+ document: null,
10207
+ isLoading: true,
10208
+ loadingProgress: { phase: "fetching" },
10209
+ error: null
10210
+ });
10204
10211
  setLoadState("loading");
10212
+ let lastProgressUpdate = 0;
10213
+ let lastPercent = -1;
10214
+ const PROGRESS_THROTTLE_MS = 100;
10215
+ const PROGRESS_MIN_CHANGE = 5;
10205
10216
  const loadDoc = async () => {
10206
10217
  if (!mountedRef.current || abortController.signal.aborted) return;
10207
10218
  try {
10208
- viewerStore.getState().setLoadingProgress({ phase: "fetching" });
10209
10219
  const { document: document2, numPages } = await loadDocument({
10210
10220
  src,
10211
10221
  workerSrc,
10212
10222
  signal: abortController.signal,
10213
10223
  onProgress: ({ loaded, total }) => {
10214
- if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
10215
- const percent = total > 0 ? Math.round(loaded / total * 100) : void 0;
10224
+ if (!mountedRef.current || srcIdRef.current !== loadId || abortController.signal.aborted) {
10225
+ return;
10226
+ }
10227
+ const now = Date.now();
10228
+ const percent = total > 0 ? Math.round(loaded / total * 100) : 0;
10229
+ const timePassed = now - lastProgressUpdate >= PROGRESS_THROTTLE_MS;
10230
+ const percentChanged = Math.abs(percent - lastPercent) >= PROGRESS_MIN_CHANGE;
10231
+ const isComplete = percent >= 100;
10232
+ if (timePassed && percentChanged || isComplete) {
10233
+ lastProgressUpdate = now;
10234
+ lastPercent = percent;
10216
10235
  viewerStore.getState().setLoadingProgress({
10217
10236
  phase: "fetching",
10218
10237
  percent,
@@ -10222,21 +10241,22 @@ var init_PDFViewerClient = __esm({
10222
10241
  }
10223
10242
  }
10224
10243
  });
10225
- if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
10226
- viewerStore.getState().setLoadingProgress({ phase: "parsing", percent: 100 });
10227
- }
10228
10244
  if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
10229
10245
  viewerStore.getState().setDocument(document2);
10230
10246
  setLoadState("loaded");
10231
- if (initialPage !== 1) {
10232
- viewerStore.getState().goToPage(initialPage);
10233
- }
10234
- if (typeof initialScale === "number") {
10235
- viewerStore.getState().setScale(initialScale);
10236
- } else if (initialScale === "auto" || initialScale === "page-width") {
10237
- viewerStore.getState().setScale(1);
10238
- } else if (initialScale === "page-fit") {
10239
- viewerStore.getState().setScale(0.75);
10247
+ if (initialPage !== 1 || typeof initialScale === "number" || initialScale === "page-fit") {
10248
+ const updates = {};
10249
+ if (initialPage !== 1) {
10250
+ updates.currentPage = Math.max(1, Math.min(initialPage, numPages));
10251
+ }
10252
+ if (typeof initialScale === "number") {
10253
+ updates.scale = initialScale;
10254
+ } else if (initialScale === "page-fit") {
10255
+ updates.scale = 0.75;
10256
+ }
10257
+ if (Object.keys(updates).length > 0) {
10258
+ viewerStore.setState(updates);
10259
+ }
10240
10260
  }
10241
10261
  onDocumentLoadRef.current?.({ document: document2, numPages });
10242
10262
  } else {
@@ -10253,7 +10273,6 @@ var init_PDFViewerClient = __esm({
10253
10273
  if (mountedRef.current && srcIdRef.current === loadId) {
10254
10274
  const error2 = err instanceof Error ? err : new Error("Failed to load document");
10255
10275
  viewerStore.getState().setError(error2);
10256
- viewerStore.getState().setLoading(false);
10257
10276
  setLoadState("error");
10258
10277
  onErrorRef.current?.(error2);
10259
10278
  }