pdfjs-reader-core 0.2.10 → 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
  }
@@ -10002,18 +10039,36 @@ var init_PDFViewerClient = __esm({
10002
10039
  viewerStore.getState().setError(null);
10003
10040
  setLoadState("idle");
10004
10041
  }, [viewerStore]);
10042
+ const abortControllerRef = (0, import_react41.useRef)(null);
10005
10043
  (0, import_react41.useEffect)(() => {
10006
10044
  mountedRef.current = true;
10007
10045
  return () => {
10008
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);
10009
10059
  };
10010
- }, []);
10060
+ }, [viewerStore]);
10011
10061
  (0, import_react41.useEffect)(() => {
10012
10062
  if (srcIdRef.current === srcId && viewerStore.getState().document) {
10013
10063
  return;
10014
10064
  }
10015
10065
  const loadId = srcId;
10016
10066
  srcIdRef.current = srcId;
10067
+ if (abortControllerRef.current) {
10068
+ abortControllerRef.current.abort();
10069
+ }
10070
+ const abortController = new AbortController();
10071
+ abortControllerRef.current = abortController;
10017
10072
  const currentDoc = viewerStore.getState().document;
10018
10073
  if (currentDoc) {
10019
10074
  currentDoc.destroy();
@@ -10023,14 +10078,15 @@ var init_PDFViewerClient = __esm({
10023
10078
  viewerStore.getState().setError(null);
10024
10079
  setLoadState("loading");
10025
10080
  const loadDoc = async () => {
10026
- if (!mountedRef.current) return;
10081
+ if (!mountedRef.current || abortController.signal.aborted) return;
10027
10082
  try {
10028
10083
  viewerStore.getState().setLoadingProgress({ phase: "fetching" });
10029
10084
  const { document: document2, numPages } = await loadDocument({
10030
10085
  src,
10031
10086
  workerSrc,
10087
+ signal: abortController.signal,
10032
10088
  onProgress: ({ loaded, total }) => {
10033
- if (mountedRef.current && srcIdRef.current === loadId) {
10089
+ if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
10034
10090
  const percent = total > 0 ? Math.round(loaded / total * 100) : void 0;
10035
10091
  viewerStore.getState().setLoadingProgress({
10036
10092
  phase: "fetching",
@@ -10041,10 +10097,10 @@ var init_PDFViewerClient = __esm({
10041
10097
  }
10042
10098
  }
10043
10099
  });
10044
- if (mountedRef.current && srcIdRef.current === loadId) {
10100
+ if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
10045
10101
  viewerStore.getState().setLoadingProgress({ phase: "parsing", percent: 100 });
10046
10102
  }
10047
- if (mountedRef.current && srcIdRef.current === loadId) {
10103
+ if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
10048
10104
  viewerStore.getState().setDocument(document2);
10049
10105
  setLoadState("loaded");
10050
10106
  if (initialPage !== 1) {
@@ -10062,6 +10118,13 @@ var init_PDFViewerClient = __esm({
10062
10118
  document2.destroy();
10063
10119
  }
10064
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
+ }
10065
10128
  if (mountedRef.current && srcIdRef.current === loadId) {
10066
10129
  const error2 = err instanceof Error ? err : new Error("Failed to load document");
10067
10130
  viewerStore.getState().setError(error2);
@@ -10073,6 +10136,7 @@ var init_PDFViewerClient = __esm({
10073
10136
  };
10074
10137
  loadDoc();
10075
10138
  return () => {
10139
+ abortController.abort();
10076
10140
  };
10077
10141
  }, [srcId, src, workerSrc, initialPage, initialScale, viewerStore]);
10078
10142
  const prevPageRef = (0, import_react41.useRef)(currentPage);