pdfjs-reader-core 0.1.3 → 0.1.4

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
@@ -338,13 +338,21 @@ function createViewerStore(initialOverrides = {}) {
338
338
  ...initialOverrides,
339
339
  // Document actions
340
340
  setDocument: (document2) => {
341
- set({
342
- document: document2,
343
- numPages: document2.numPages,
344
- isLoading: false,
345
- error: null,
346
- currentPage: 1
347
- });
341
+ if (document2) {
342
+ set({
343
+ document: document2,
344
+ numPages: document2.numPages,
345
+ isLoading: false,
346
+ error: null,
347
+ currentPage: 1
348
+ });
349
+ } else {
350
+ set({
351
+ document: null,
352
+ numPages: 0,
353
+ isLoading: false
354
+ });
355
+ }
348
356
  },
349
357
  setLoading: (isLoading) => {
350
358
  set({ isLoading });
@@ -8777,6 +8785,17 @@ var PDFViewerClient_exports = {};
8777
8785
  __export(PDFViewerClient_exports, {
8778
8786
  PDFViewerClient: () => PDFViewerClient
8779
8787
  });
8788
+ function getSrcIdentifier(src) {
8789
+ if (typeof src === "string") {
8790
+ return src;
8791
+ }
8792
+ const data = src instanceof ArrayBuffer ? new Uint8Array(src) : src;
8793
+ const len = data.byteLength;
8794
+ if (len === 0) return "empty";
8795
+ const first = Array.from(data.slice(0, 4)).map((b) => b.toString(16).padStart(2, "0")).join("");
8796
+ const last = Array.from(data.slice(-4)).map((b) => b.toString(16).padStart(2, "0")).join("");
8797
+ return `binary:${len}:${first}:${last}`;
8798
+ }
8780
8799
  var import_react39, import_jsx_runtime25, PDFViewerInner, PDFViewerClient;
8781
8800
  var init_PDFViewerClient = __esm({
8782
8801
  "src/components/PDFViewer/PDFViewerClient.tsx"() {
@@ -8808,8 +8827,8 @@ var init_PDFViewerClient = __esm({
8808
8827
  className
8809
8828
  }) {
8810
8829
  const { viewerStore } = usePDFViewerStores();
8811
- const loadingRef = (0, import_react39.useRef)(false);
8812
- const srcRef = (0, import_react39.useRef)(src);
8830
+ const mountedRef = (0, import_react39.useRef)(true);
8831
+ const [, setLoadState] = (0, import_react39.useState)("idle");
8813
8832
  const onDocumentLoadRef = (0, import_react39.useRef)(onDocumentLoad);
8814
8833
  const onErrorRef = (0, import_react39.useRef)(onError);
8815
8834
  const onPageChangeRef = (0, import_react39.useRef)(onPageChange);
@@ -8818,42 +8837,49 @@ var init_PDFViewerClient = __esm({
8818
8837
  onErrorRef.current = onError;
8819
8838
  onPageChangeRef.current = onPageChange;
8820
8839
  onScaleChangeRef.current = onScaleChange;
8840
+ const srcIdRef = (0, import_react39.useRef)(null);
8821
8841
  const currentPage = useViewerStore((s) => s.currentPage);
8822
8842
  const scale = useViewerStore((s) => s.scale);
8823
8843
  const theme = useViewerStore((s) => s.theme);
8824
8844
  const isLoading = useViewerStore((s) => s.isLoading);
8825
8845
  const error = useViewerStore((s) => s.error);
8826
8846
  const sidebarOpen = useViewerStore((s) => s.sidebarOpen);
8827
- const [retryTrigger, setRetryTrigger] = (0, import_react39.useState)(0);
8847
+ const srcId = getSrcIdentifier(src);
8828
8848
  const handleRetry = (0, import_react39.useCallback)(() => {
8829
- loadingRef.current = false;
8830
- srcRef.current = null;
8849
+ srcIdRef.current = null;
8831
8850
  viewerStore.getState().setError(null);
8832
- setRetryTrigger((c) => c + 1);
8833
- }, [viewerStore, src]);
8851
+ setLoadState("idle");
8852
+ }, [viewerStore]);
8834
8853
  (0, import_react39.useEffect)(() => {
8835
- if (loadingRef.current) {
8836
- return;
8837
- }
8838
- if (srcRef.current === src && viewerStore.getState().document) {
8854
+ mountedRef.current = true;
8855
+ return () => {
8856
+ mountedRef.current = false;
8857
+ };
8858
+ }, []);
8859
+ (0, import_react39.useEffect)(() => {
8860
+ if (srcIdRef.current === srcId && viewerStore.getState().document) {
8839
8861
  return;
8840
8862
  }
8841
- srcRef.current = src;
8842
- loadingRef.current = true;
8863
+ const loadId = srcId;
8864
+ srcIdRef.current = srcId;
8843
8865
  const currentDoc = viewerStore.getState().document;
8844
8866
  if (currentDoc) {
8845
- viewerStore.getState().reset();
8867
+ currentDoc.destroy();
8868
+ viewerStore.getState().setDocument(null);
8846
8869
  }
8847
8870
  const loadDoc = async () => {
8871
+ if (!mountedRef.current) return;
8848
8872
  try {
8849
8873
  viewerStore.getState().setLoading(true);
8850
8874
  viewerStore.getState().setError(null);
8875
+ setLoadState("loading");
8851
8876
  const { document: document2, numPages } = await loadDocument({
8852
8877
  src,
8853
8878
  workerSrc
8854
8879
  });
8855
- if (srcRef.current === src) {
8880
+ if (mountedRef.current && srcIdRef.current === loadId) {
8856
8881
  viewerStore.getState().setDocument(document2);
8882
+ setLoadState("loaded");
8857
8883
  if (initialPage !== 1) {
8858
8884
  viewerStore.getState().goToPage(initialPage);
8859
8885
  }
@@ -8865,25 +8891,19 @@ var init_PDFViewerClient = __esm({
8865
8891
  document2.destroy();
8866
8892
  }
8867
8893
  } catch (err) {
8868
- if (srcRef.current === src) {
8894
+ if (mountedRef.current && srcIdRef.current === loadId) {
8869
8895
  const error2 = err instanceof Error ? err : new Error("Failed to load document");
8870
8896
  viewerStore.getState().setError(error2);
8897
+ viewerStore.getState().setLoading(false);
8898
+ setLoadState("error");
8871
8899
  onErrorRef.current?.(error2);
8872
8900
  }
8873
- } finally {
8874
- loadingRef.current = false;
8875
8901
  }
8876
8902
  };
8877
8903
  loadDoc();
8878
8904
  return () => {
8879
- srcRef.current = null;
8880
- };
8881
- }, [src, workerSrc, initialPage, initialScale, viewerStore, retryTrigger]);
8882
- (0, import_react39.useEffect)(() => {
8883
- return () => {
8884
- viewerStore.getState().reset();
8885
8905
  };
8886
- }, [viewerStore]);
8906
+ }, [srcId, src, workerSrc, initialPage, initialScale, viewerStore]);
8887
8907
  const prevPageRef = (0, import_react39.useRef)(currentPage);
8888
8908
  (0, import_react39.useEffect)(() => {
8889
8909
  if (prevPageRef.current !== currentPage) {