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.cjs CHANGED
@@ -101,13 +101,14 @@ async function loadDocument(options) {
101
101
  const cachedDoc = documentCache.get(cacheKey);
102
102
  try {
103
103
  const numPages = cachedDoc.numPages;
104
- if (numPages > 0) {
105
- await cachedDoc.getPage(1);
104
+ const isDestroyed = cachedDoc._transport?.destroyed || cachedDoc.destroyed;
105
+ if (numPages > 0 && !isDestroyed) {
106
106
  return {
107
107
  document: cachedDoc,
108
108
  numPages
109
109
  };
110
110
  }
111
+ documentCache.delete(cacheKey);
111
112
  } catch {
112
113
  documentCache.delete(cacheKey);
113
114
  }
@@ -10226,23 +10227,41 @@ var init_PDFViewerClient = __esm({
10226
10227
  abortControllerRef.current = abortController;
10227
10228
  const currentDoc = viewerStore.getState().document;
10228
10229
  if (currentDoc) {
10229
- currentDoc.destroy();
10230
- viewerStore.getState().setDocument(null);
10230
+ try {
10231
+ currentDoc.destroy();
10232
+ } catch {
10233
+ }
10231
10234
  }
10232
- viewerStore.getState().setLoading(true, { phase: "initializing" });
10233
- viewerStore.getState().setError(null);
10235
+ viewerStore.setState({
10236
+ document: null,
10237
+ isLoading: true,
10238
+ loadingProgress: { phase: "fetching" },
10239
+ error: null
10240
+ });
10234
10241
  setLoadState("loading");
10242
+ let lastProgressUpdate = 0;
10243
+ let lastPercent = -1;
10244
+ const PROGRESS_THROTTLE_MS = 100;
10245
+ const PROGRESS_MIN_CHANGE = 5;
10235
10246
  const loadDoc = async () => {
10236
10247
  if (!mountedRef.current || abortController.signal.aborted) return;
10237
10248
  try {
10238
- viewerStore.getState().setLoadingProgress({ phase: "fetching" });
10239
10249
  const { document: document2, numPages } = await loadDocument({
10240
10250
  src,
10241
10251
  workerSrc,
10242
10252
  signal: abortController.signal,
10243
10253
  onProgress: ({ loaded, total }) => {
10244
- if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
10245
- const percent = total > 0 ? Math.round(loaded / total * 100) : void 0;
10254
+ if (!mountedRef.current || srcIdRef.current !== loadId || abortController.signal.aborted) {
10255
+ return;
10256
+ }
10257
+ const now = Date.now();
10258
+ const percent = total > 0 ? Math.round(loaded / total * 100) : 0;
10259
+ const timePassed = now - lastProgressUpdate >= PROGRESS_THROTTLE_MS;
10260
+ const percentChanged = Math.abs(percent - lastPercent) >= PROGRESS_MIN_CHANGE;
10261
+ const isComplete = percent >= 100;
10262
+ if (timePassed && percentChanged || isComplete) {
10263
+ lastProgressUpdate = now;
10264
+ lastPercent = percent;
10246
10265
  viewerStore.getState().setLoadingProgress({
10247
10266
  phase: "fetching",
10248
10267
  percent,
@@ -10252,21 +10271,22 @@ var init_PDFViewerClient = __esm({
10252
10271
  }
10253
10272
  }
10254
10273
  });
10255
- if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
10256
- viewerStore.getState().setLoadingProgress({ phase: "parsing", percent: 100 });
10257
- }
10258
10274
  if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
10259
10275
  viewerStore.getState().setDocument(document2);
10260
10276
  setLoadState("loaded");
10261
- if (initialPage !== 1) {
10262
- viewerStore.getState().goToPage(initialPage);
10263
- }
10264
- if (typeof initialScale === "number") {
10265
- viewerStore.getState().setScale(initialScale);
10266
- } else if (initialScale === "auto" || initialScale === "page-width") {
10267
- viewerStore.getState().setScale(1);
10268
- } else if (initialScale === "page-fit") {
10269
- viewerStore.getState().setScale(0.75);
10277
+ if (initialPage !== 1 || typeof initialScale === "number" || initialScale === "page-fit") {
10278
+ const updates = {};
10279
+ if (initialPage !== 1) {
10280
+ updates.currentPage = Math.max(1, Math.min(initialPage, numPages));
10281
+ }
10282
+ if (typeof initialScale === "number") {
10283
+ updates.scale = initialScale;
10284
+ } else if (initialScale === "page-fit") {
10285
+ updates.scale = 0.75;
10286
+ }
10287
+ if (Object.keys(updates).length > 0) {
10288
+ viewerStore.setState(updates);
10289
+ }
10270
10290
  }
10271
10291
  onDocumentLoadRef.current?.({ document: document2, numPages });
10272
10292
  } else {
@@ -10283,7 +10303,6 @@ var init_PDFViewerClient = __esm({
10283
10303
  if (mountedRef.current && srcIdRef.current === loadId) {
10284
10304
  const error2 = err instanceof Error ? err : new Error("Failed to load document");
10285
10305
  viewerStore.getState().setError(error2);
10286
- viewerStore.getState().setLoading(false);
10287
10306
  setLoadState("error");
10288
10307
  onErrorRef.current?.(error2);
10289
10308
  }