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.d.cts CHANGED
@@ -2156,6 +2156,8 @@ interface LoadDocumentOptions {
2156
2156
  enableStreaming?: boolean;
2157
2157
  /** Cache the document data (default: true) */
2158
2158
  cacheDocument?: boolean;
2159
+ /** AbortSignal for cancellation */
2160
+ signal?: AbortSignal;
2159
2161
  }
2160
2162
  interface LoadDocumentResult {
2161
2163
  document: PDFDocumentProxy;
package/dist/index.d.ts CHANGED
@@ -2156,6 +2156,8 @@ interface LoadDocumentOptions {
2156
2156
  enableStreaming?: boolean;
2157
2157
  /** Cache the document data (default: true) */
2158
2158
  cacheDocument?: boolean;
2159
+ /** AbortSignal for cancellation */
2160
+ signal?: AbortSignal;
2159
2161
  }
2160
2162
  interface LoadDocumentResult {
2161
2163
  document: PDFDocumentProxy;
package/dist/index.js CHANGED
@@ -63,16 +63,28 @@ async function loadDocument(options) {
63
63
  onProgress,
64
64
  enableRangeRequests = true,
65
65
  enableStreaming = true,
66
- cacheDocument = true
66
+ cacheDocument = true,
67
+ signal
67
68
  } = options;
69
+ if (signal?.aborted) {
70
+ throw new DOMException("Aborted", "AbortError");
71
+ }
68
72
  await initializePDFJS({ workerSrc });
73
+ if (signal?.aborted) {
74
+ throw new DOMException("Aborted", "AbortError");
75
+ }
69
76
  const cacheKey = typeof src === "string" ? src : null;
70
77
  if (cacheKey && cacheDocument && documentCache.has(cacheKey)) {
71
78
  const cachedDoc = documentCache.get(cacheKey);
72
- return {
73
- document: cachedDoc,
74
- numPages: cachedDoc.numPages
75
- };
79
+ try {
80
+ const numPages = cachedDoc.numPages;
81
+ return {
82
+ document: cachedDoc,
83
+ numPages
84
+ };
85
+ } catch {
86
+ documentCache.delete(cacheKey);
87
+ }
76
88
  }
77
89
  const loadingParams = {
78
90
  password,
@@ -93,12 +105,37 @@ async function loadDocument(options) {
93
105
  loadingParams.data = src;
94
106
  }
95
107
  const loadingTask = pdfjsLib.getDocument(loadingParams);
108
+ let abortHandler = null;
109
+ if (signal) {
110
+ abortHandler = () => {
111
+ loadingTask.destroy();
112
+ };
113
+ signal.addEventListener("abort", abortHandler);
114
+ }
96
115
  if (onProgress) {
97
116
  loadingTask.onProgress = ({ loaded, total }) => {
98
117
  onProgress({ loaded, total });
99
118
  };
100
119
  }
101
- const document2 = await loadingTask.promise;
120
+ let document2;
121
+ try {
122
+ document2 = await loadingTask.promise;
123
+ } catch (error) {
124
+ if (signal && abortHandler) {
125
+ signal.removeEventListener("abort", abortHandler);
126
+ }
127
+ if (signal?.aborted) {
128
+ throw new DOMException("Aborted", "AbortError");
129
+ }
130
+ throw error;
131
+ }
132
+ if (signal && abortHandler) {
133
+ signal.removeEventListener("abort", abortHandler);
134
+ }
135
+ if (signal?.aborted) {
136
+ document2.destroy();
137
+ throw new DOMException("Aborted", "AbortError");
138
+ }
102
139
  if (cacheKey && cacheDocument) {
103
140
  documentCache.set(cacheKey, document2);
104
141
  }
@@ -9972,18 +10009,36 @@ var init_PDFViewerClient = __esm({
9972
10009
  viewerStore.getState().setError(null);
9973
10010
  setLoadState("idle");
9974
10011
  }, [viewerStore]);
10012
+ const abortControllerRef = useRef19(null);
9975
10013
  useEffect22(() => {
9976
10014
  mountedRef.current = true;
9977
10015
  return () => {
9978
10016
  mountedRef.current = false;
10017
+ if (abortControllerRef.current) {
10018
+ abortControllerRef.current.abort();
10019
+ abortControllerRef.current = null;
10020
+ }
10021
+ srcIdRef.current = null;
10022
+ const currentDoc = viewerStore.getState().document;
10023
+ if (currentDoc) {
10024
+ currentDoc.destroy();
10025
+ }
10026
+ viewerStore.getState().setDocument(null);
10027
+ viewerStore.getState().setLoading(false);
10028
+ viewerStore.getState().setError(null);
9979
10029
  };
9980
- }, []);
10030
+ }, [viewerStore]);
9981
10031
  useEffect22(() => {
9982
10032
  if (srcIdRef.current === srcId && viewerStore.getState().document) {
9983
10033
  return;
9984
10034
  }
9985
10035
  const loadId = srcId;
9986
10036
  srcIdRef.current = srcId;
10037
+ if (abortControllerRef.current) {
10038
+ abortControllerRef.current.abort();
10039
+ }
10040
+ const abortController = new AbortController();
10041
+ abortControllerRef.current = abortController;
9987
10042
  const currentDoc = viewerStore.getState().document;
9988
10043
  if (currentDoc) {
9989
10044
  currentDoc.destroy();
@@ -9993,14 +10048,15 @@ var init_PDFViewerClient = __esm({
9993
10048
  viewerStore.getState().setError(null);
9994
10049
  setLoadState("loading");
9995
10050
  const loadDoc = async () => {
9996
- if (!mountedRef.current) return;
10051
+ if (!mountedRef.current || abortController.signal.aborted) return;
9997
10052
  try {
9998
10053
  viewerStore.getState().setLoadingProgress({ phase: "fetching" });
9999
10054
  const { document: document2, numPages } = await loadDocument({
10000
10055
  src,
10001
10056
  workerSrc,
10057
+ signal: abortController.signal,
10002
10058
  onProgress: ({ loaded, total }) => {
10003
- if (mountedRef.current && srcIdRef.current === loadId) {
10059
+ if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
10004
10060
  const percent = total > 0 ? Math.round(loaded / total * 100) : void 0;
10005
10061
  viewerStore.getState().setLoadingProgress({
10006
10062
  phase: "fetching",
@@ -10011,10 +10067,10 @@ var init_PDFViewerClient = __esm({
10011
10067
  }
10012
10068
  }
10013
10069
  });
10014
- if (mountedRef.current && srcIdRef.current === loadId) {
10070
+ if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
10015
10071
  viewerStore.getState().setLoadingProgress({ phase: "parsing", percent: 100 });
10016
10072
  }
10017
- if (mountedRef.current && srcIdRef.current === loadId) {
10073
+ if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
10018
10074
  viewerStore.getState().setDocument(document2);
10019
10075
  setLoadState("loaded");
10020
10076
  if (initialPage !== 1) {
@@ -10032,6 +10088,13 @@ var init_PDFViewerClient = __esm({
10032
10088
  document2.destroy();
10033
10089
  }
10034
10090
  } catch (err) {
10091
+ if (err instanceof DOMException && err.name === "AbortError") {
10092
+ return;
10093
+ }
10094
+ const errorMessage = err instanceof Error ? err.message : String(err);
10095
+ if (abortController.signal.aborted || errorMessage.includes("network error") || errorMessage.includes("aborted")) {
10096
+ return;
10097
+ }
10035
10098
  if (mountedRef.current && srcIdRef.current === loadId) {
10036
10099
  const error2 = err instanceof Error ? err : new Error("Failed to load document");
10037
10100
  viewerStore.getState().setError(error2);
@@ -10043,6 +10106,7 @@ var init_PDFViewerClient = __esm({
10043
10106
  };
10044
10107
  loadDoc();
10045
10108
  return () => {
10109
+ abortController.abort();
10046
10110
  };
10047
10111
  }, [srcId, src, workerSrc, initialPage, initialScale, viewerStore]);
10048
10112
  const prevPageRef = useRef19(currentPage);