pdfjs-reader-core 0.2.13 → 0.2.15

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,10 +101,14 @@ async function loadDocument(options) {
101
101
  const cachedDoc = documentCache.get(cacheKey);
102
102
  try {
103
103
  const numPages = cachedDoc.numPages;
104
- return {
105
- document: cachedDoc,
106
- numPages
107
- };
104
+ const isDestroyed = cachedDoc._transport?.destroyed || cachedDoc.destroyed;
105
+ if (numPages > 0 && !isDestroyed) {
106
+ return {
107
+ document: cachedDoc,
108
+ numPages
109
+ };
110
+ }
111
+ documentCache.delete(cacheKey);
108
112
  } catch {
109
113
  documentCache.delete(cacheKey);
110
114
  }
@@ -182,6 +186,20 @@ async function getOutline(document2) {
182
186
  async function getMetadata(document2) {
183
187
  return document2.getMetadata();
184
188
  }
189
+ function clearDocumentCache(url) {
190
+ if (url) {
191
+ const doc = documentCache.get(url);
192
+ if (doc) {
193
+ doc.destroy();
194
+ documentCache.delete(url);
195
+ }
196
+ } else {
197
+ for (const doc of documentCache.values()) {
198
+ doc.destroy();
199
+ }
200
+ documentCache.clear();
201
+ }
202
+ }
185
203
  var documentCache;
186
204
  var init_document_loader = __esm({
187
205
  "src/utils/document-loader.ts"() {
@@ -10169,6 +10187,7 @@ var init_PDFViewerClient = __esm({
10169
10187
  setLoadState("idle");
10170
10188
  }, [viewerStore]);
10171
10189
  const abortControllerRef = (0, import_react41.useRef)(null);
10190
+ const currentSrcRef = (0, import_react41.useRef)(null);
10172
10191
  (0, import_react41.useEffect)(() => {
10173
10192
  mountedRef.current = true;
10174
10193
  return () => {
@@ -10177,10 +10196,17 @@ var init_PDFViewerClient = __esm({
10177
10196
  abortControllerRef.current.abort();
10178
10197
  abortControllerRef.current = null;
10179
10198
  }
10199
+ if (currentSrcRef.current && typeof currentSrcRef.current === "string") {
10200
+ clearDocumentCache(currentSrcRef.current);
10201
+ }
10180
10202
  srcIdRef.current = null;
10203
+ currentSrcRef.current = null;
10181
10204
  const currentDoc = viewerStore.getState().document;
10182
10205
  if (currentDoc) {
10183
- currentDoc.destroy();
10206
+ try {
10207
+ currentDoc.destroy();
10208
+ } catch {
10209
+ }
10184
10210
  }
10185
10211
  viewerStore.getState().setDocument(null);
10186
10212
  viewerStore.getState().setLoading(false);
@@ -10193,6 +10219,7 @@ var init_PDFViewerClient = __esm({
10193
10219
  }
10194
10220
  const loadId = srcId;
10195
10221
  srcIdRef.current = srcId;
10222
+ currentSrcRef.current = src;
10196
10223
  if (abortControllerRef.current) {
10197
10224
  abortControllerRef.current.abort();
10198
10225
  }
@@ -10200,23 +10227,41 @@ var init_PDFViewerClient = __esm({
10200
10227
  abortControllerRef.current = abortController;
10201
10228
  const currentDoc = viewerStore.getState().document;
10202
10229
  if (currentDoc) {
10203
- currentDoc.destroy();
10204
- viewerStore.getState().setDocument(null);
10230
+ try {
10231
+ currentDoc.destroy();
10232
+ } catch {
10233
+ }
10205
10234
  }
10206
- viewerStore.getState().setLoading(true, { phase: "initializing" });
10207
- viewerStore.getState().setError(null);
10235
+ viewerStore.setState({
10236
+ document: null,
10237
+ isLoading: true,
10238
+ loadingProgress: { phase: "fetching" },
10239
+ error: null
10240
+ });
10208
10241
  setLoadState("loading");
10242
+ let lastProgressUpdate = 0;
10243
+ let lastPercent = -1;
10244
+ const PROGRESS_THROTTLE_MS = 100;
10245
+ const PROGRESS_MIN_CHANGE = 5;
10209
10246
  const loadDoc = async () => {
10210
10247
  if (!mountedRef.current || abortController.signal.aborted) return;
10211
10248
  try {
10212
- viewerStore.getState().setLoadingProgress({ phase: "fetching" });
10213
10249
  const { document: document2, numPages } = await loadDocument({
10214
10250
  src,
10215
10251
  workerSrc,
10216
10252
  signal: abortController.signal,
10217
10253
  onProgress: ({ loaded, total }) => {
10218
- if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
10219
- 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;
10220
10265
  viewerStore.getState().setLoadingProgress({
10221
10266
  phase: "fetching",
10222
10267
  percent,
@@ -10226,21 +10271,22 @@ var init_PDFViewerClient = __esm({
10226
10271
  }
10227
10272
  }
10228
10273
  });
10229
- if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
10230
- viewerStore.getState().setLoadingProgress({ phase: "parsing", percent: 100 });
10231
- }
10232
10274
  if (mountedRef.current && srcIdRef.current === loadId && !abortController.signal.aborted) {
10233
10275
  viewerStore.getState().setDocument(document2);
10234
10276
  setLoadState("loaded");
10235
- if (initialPage !== 1) {
10236
- viewerStore.getState().goToPage(initialPage);
10237
- }
10238
- if (typeof initialScale === "number") {
10239
- viewerStore.getState().setScale(initialScale);
10240
- } else if (initialScale === "auto" || initialScale === "page-width") {
10241
- viewerStore.getState().setScale(1);
10242
- } else if (initialScale === "page-fit") {
10243
- 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
+ }
10244
10290
  }
10245
10291
  onDocumentLoadRef.current?.({ document: document2, numPages });
10246
10292
  } else {
@@ -10257,7 +10303,6 @@ var init_PDFViewerClient = __esm({
10257
10303
  if (mountedRef.current && srcIdRef.current === loadId) {
10258
10304
  const error2 = err instanceof Error ? err : new Error("Failed to load document");
10259
10305
  viewerStore.getState().setError(error2);
10260
- viewerStore.getState().setLoading(false);
10261
10306
  setLoadState("error");
10262
10307
  onErrorRef.current?.(error2);
10263
10308
  }