pdfjs-reader-core 0.2.9 → 0.2.11

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
@@ -86,16 +86,28 @@ async function loadDocument(options) {
86
86
  onProgress,
87
87
  enableRangeRequests = true,
88
88
  enableStreaming = true,
89
- cacheDocument = true
89
+ cacheDocument = true,
90
+ signal
90
91
  } = options;
92
+ if (signal?.aborted) {
93
+ throw new DOMException("Aborted", "AbortError");
94
+ }
91
95
  await initializePDFJS({ workerSrc });
96
+ if (signal?.aborted) {
97
+ throw new DOMException("Aborted", "AbortError");
98
+ }
92
99
  const cacheKey = typeof src === "string" ? src : null;
93
100
  if (cacheKey && cacheDocument && documentCache.has(cacheKey)) {
94
101
  const cachedDoc = documentCache.get(cacheKey);
95
- return {
96
- document: cachedDoc,
97
- numPages: cachedDoc.numPages
98
- };
102
+ try {
103
+ const numPages = cachedDoc.numPages;
104
+ return {
105
+ document: cachedDoc,
106
+ numPages
107
+ };
108
+ } catch {
109
+ documentCache.delete(cacheKey);
110
+ }
99
111
  }
100
112
  const loadingParams = {
101
113
  password,
@@ -116,12 +128,37 @@ async function loadDocument(options) {
116
128
  loadingParams.data = src;
117
129
  }
118
130
  const loadingTask = pdfjsLib.getDocument(loadingParams);
131
+ let abortHandler = null;
132
+ if (signal) {
133
+ abortHandler = () => {
134
+ loadingTask.destroy();
135
+ };
136
+ signal.addEventListener("abort", abortHandler);
137
+ }
119
138
  if (onProgress) {
120
139
  loadingTask.onProgress = ({ loaded, total }) => {
121
140
  onProgress({ loaded, total });
122
141
  };
123
142
  }
124
- const document2 = await loadingTask.promise;
143
+ let document2;
144
+ try {
145
+ document2 = await loadingTask.promise;
146
+ } catch (error) {
147
+ if (signal && abortHandler) {
148
+ signal.removeEventListener("abort", abortHandler);
149
+ }
150
+ if (signal?.aborted) {
151
+ throw new DOMException("Aborted", "AbortError");
152
+ }
153
+ throw error;
154
+ }
155
+ if (signal && abortHandler) {
156
+ signal.removeEventListener("abort", abortHandler);
157
+ }
158
+ if (signal?.aborted) {
159
+ document2.destroy();
160
+ throw new DOMException("Aborted", "AbortError");
161
+ }
125
162
  if (cacheKey && cacheDocument) {
126
163
  documentCache.set(cacheKey, document2);
127
164
  }
@@ -4737,7 +4774,11 @@ var init_ThumbnailPanel = __esm({
4737
4774
  }
4738
4775
  } catch (error) {
4739
4776
  if (!cancelled) {
4740
- console.error(`Error rendering thumbnail for page ${pageNumber}:`, error);
4777
+ const errorMessage = error instanceof Error ? error.message : String(error);
4778
+ const isDocumentDestroyed = errorMessage.includes("destroyed") || errorMessage.includes("sendWithStream") || errorMessage.includes("sendWithPromise") || errorMessage.includes("Cannot read properties of null");
4779
+ if (!isDocumentDestroyed) {
4780
+ console.error(`Error rendering thumbnail for page ${pageNumber}:`, error);
4781
+ }
4741
4782
  }
4742
4783
  }
4743
4784
  };
@@ -8457,7 +8498,8 @@ var init_DocumentContainer = __esm({
8457
8498
  } catch (error) {
8458
8499
  if (!cancelled) {
8459
8500
  const errorMessage = error instanceof Error ? error.message : String(error);
8460
- if (!errorMessage.includes("destroyed") && !errorMessage.includes("sendWithStream")) {
8501
+ const isDocumentDestroyed = errorMessage.includes("destroyed") || errorMessage.includes("sendWithStream") || errorMessage.includes("sendWithPromise") || errorMessage.includes("Cannot read properties of null");
8502
+ if (!isDocumentDestroyed) {
8461
8503
  console.error("Error loading page:", error);
8462
8504
  }
8463
8505
  }
@@ -8736,7 +8778,11 @@ var init_VirtualizedDocumentContainer = __esm({
8736
8778
  newPageObjects.set(pageNum, page);
8737
8779
  hasChanges = true;
8738
8780
  } catch (error) {
8739
- console.error(`Error loading page ${pageNum}:`, error);
8781
+ const errorMessage = error instanceof Error ? error.message : String(error);
8782
+ const isDocumentDestroyed = errorMessage.includes("destroyed") || errorMessage.includes("sendWithStream") || errorMessage.includes("sendWithPromise") || errorMessage.includes("Cannot read properties of null");
8783
+ if (!isDocumentDestroyed) {
8784
+ console.error(`Error loading page ${pageNum}:`, error);
8785
+ }
8740
8786
  }
8741
8787
  }
8742
8788
  }
@@ -9114,7 +9160,11 @@ var init_DualPageContainer = __esm({
9114
9160
  }
9115
9161
  } catch (error) {
9116
9162
  if (!cancelled) {
9117
- console.error("Error loading pages:", error);
9163
+ const errorMessage = error instanceof Error ? error.message : String(error);
9164
+ const isDocumentDestroyed = errorMessage.includes("destroyed") || errorMessage.includes("sendWithStream") || errorMessage.includes("sendWithPromise") || errorMessage.includes("Cannot read properties of null");
9165
+ if (!isDocumentDestroyed) {
9166
+ console.error("Error loading pages:", error);
9167
+ }
9118
9168
  }
9119
9169
  } finally {
9120
9170
  if (!cancelled) {
@@ -9989,18 +10039,36 @@ var init_PDFViewerClient = __esm({
9989
10039
  viewerStore.getState().setError(null);
9990
10040
  setLoadState("idle");
9991
10041
  }, [viewerStore]);
10042
+ const abortControllerRef = (0, import_react41.useRef)(null);
9992
10043
  (0, import_react41.useEffect)(() => {
9993
10044
  mountedRef.current = true;
9994
10045
  return () => {
9995
10046
  mountedRef.current = false;
10047
+ if (abortControllerRef.current) {
10048
+ abortControllerRef.current.abort();
10049
+ abortControllerRef.current = null;
10050
+ }
10051
+ srcIdRef.current = null;
10052
+ const currentDoc = viewerStore.getState().document;
10053
+ if (currentDoc) {
10054
+ currentDoc.destroy();
10055
+ }
10056
+ viewerStore.getState().setDocument(null);
10057
+ viewerStore.getState().setLoading(false);
10058
+ viewerStore.getState().setError(null);
9996
10059
  };
9997
- }, []);
10060
+ }, [viewerStore]);
9998
10061
  (0, import_react41.useEffect)(() => {
9999
10062
  if (srcIdRef.current === srcId && viewerStore.getState().document) {
10000
10063
  return;
10001
10064
  }
10002
10065
  const loadId = srcId;
10003
10066
  srcIdRef.current = srcId;
10067
+ if (abortControllerRef.current) {
10068
+ abortControllerRef.current.abort();
10069
+ }
10070
+ const abortController = new AbortController();
10071
+ abortControllerRef.current = abortController;
10004
10072
  const currentDoc = viewerStore.getState().document;
10005
10073
  if (currentDoc) {
10006
10074
  currentDoc.destroy();
@@ -10010,14 +10078,15 @@ var init_PDFViewerClient = __esm({
10010
10078
  viewerStore.getState().setError(null);
10011
10079
  setLoadState("loading");
10012
10080
  const loadDoc = async () => {
10013
- if (!mountedRef.current) return;
10081
+ if (!mountedRef.current || abortController.signal.aborted) return;
10014
10082
  try {
10015
10083
  viewerStore.getState().setLoadingProgress({ phase: "fetching" });
10016
10084
  const { document: document2, numPages } = await loadDocument({
10017
10085
  src,
10018
10086
  workerSrc,
10087
+ signal: abortController.signal,
10019
10088
  onProgress: ({ loaded, total }) => {
10020
- if (mountedRef.current && srcIdRef.current === loadId) {
10089
+ if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
10021
10090
  const percent = total > 0 ? Math.round(loaded / total * 100) : void 0;
10022
10091
  viewerStore.getState().setLoadingProgress({
10023
10092
  phase: "fetching",
@@ -10028,10 +10097,10 @@ var init_PDFViewerClient = __esm({
10028
10097
  }
10029
10098
  }
10030
10099
  });
10031
- if (mountedRef.current && srcIdRef.current === loadId) {
10100
+ if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
10032
10101
  viewerStore.getState().setLoadingProgress({ phase: "parsing", percent: 100 });
10033
10102
  }
10034
- if (mountedRef.current && srcIdRef.current === loadId) {
10103
+ if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
10035
10104
  viewerStore.getState().setDocument(document2);
10036
10105
  setLoadState("loaded");
10037
10106
  if (initialPage !== 1) {
@@ -10049,6 +10118,13 @@ var init_PDFViewerClient = __esm({
10049
10118
  document2.destroy();
10050
10119
  }
10051
10120
  } catch (err) {
10121
+ if (err instanceof DOMException && err.name === "AbortError") {
10122
+ return;
10123
+ }
10124
+ const errorMessage = err instanceof Error ? err.message : String(err);
10125
+ if (abortController.signal.aborted || errorMessage.includes("network error") || errorMessage.includes("aborted")) {
10126
+ return;
10127
+ }
10052
10128
  if (mountedRef.current && srcIdRef.current === loadId) {
10053
10129
  const error2 = err instanceof Error ? err : new Error("Failed to load document");
10054
10130
  viewerStore.getState().setError(error2);
@@ -10060,6 +10136,7 @@ var init_PDFViewerClient = __esm({
10060
10136
  };
10061
10137
  loadDoc();
10062
10138
  return () => {
10139
+ abortController.abort();
10063
10140
  };
10064
10141
  }, [srcId, src, workerSrc, initialPage, initialScale, viewerStore]);
10065
10142
  const prevPageRef = (0, import_react41.useRef)(currentPage);
@@ -11750,7 +11827,11 @@ var PDFThumbnailNav = (0, import_react51.memo)(function PDFThumbnailNav2({
11750
11827
  });
11751
11828
  }
11752
11829
  } catch (error) {
11753
- console.error(`Failed to render thumbnail for page ${pageNum}:`, error);
11830
+ const errorMessage = error instanceof Error ? error.message : String(error);
11831
+ const isDocumentDestroyed = errorMessage.includes("destroyed") || errorMessage.includes("sendWithStream") || errorMessage.includes("sendWithPromise") || errorMessage.includes("Cannot read properties of null");
11832
+ if (!isDocumentDestroyed) {
11833
+ console.error(`Failed to render thumbnail for page ${pageNum}:`, error);
11834
+ }
11754
11835
  } finally {
11755
11836
  renderQueueRef.current.delete(pageNum);
11756
11837
  }