Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

2.9 KB
Raw Blame
import { useEffect, useRef, useState } from "react";
import { type Logger, type PdfEngine } from "@embedpdf/models";
import type { FontFallbackConfig } from "@embedpdf/engines/pdfium-worker-engine";
import { startEagerWasmCompilation } from "@editor/services/wasmPrecompiler";

interface LocalPdfiumEngineOptions {
  wasmUrl: string;
  logger?: Logger;
  encoderPoolSize?: number;
  fontFallback?: FontFallbackConfig | null;
}

/**
 * Destroy an engine once its documents are closed. Teardown has to be
 * unconditional: `closeAllDocuments` may be absent, return nothing, or throw,
 * and `wait` runs only one of its two callbacks, so a failed close would
 * otherwise leave the worker running for the lifetime of the app.
 */
function destroyEngine(engine: PdfEngine<Blob> | null): void {
  if (!engine) return;

  let destroyed = false;
  const destroy = () => {
    if (destroyed) return;
    destroyed = true;
    try {
      engine.destroy?.();
    } catch {
      // Nothing left to release once destroy itself fails.
    }
  };

  try {
    const closing = engine.closeAllDocuments?.();
    if (closing?.wait) {
      closing.wait(destroy, destroy);
    } else {
      destroy();
    }
  } catch {
    destroy();
  }
}

export function useLocalPdfiumEngine({
  wasmUrl,
  logger,
  encoderPoolSize,
  fontFallback,
}: LocalPdfiumEngineOptions) {
  const [engine, setEngine] = useState<PdfEngine<Blob> | null>(null);
  const [isLoading, setIsLoading] = useState(true);
  const [error, setError] = useState<Error | null>(null);
  const engineRef = useRef<PdfEngine<Blob> | null>(null);

  useEffect(() => {
    let cancelled = false;
    // Reset per attempt so the render tree never exposes the engine this run is
    // about to destroy, nor a stale error left over from an earlier failure.
    setEngine(null);
    setError(null);
    setIsLoading(true);

    (async () => {
      try {
        startEagerWasmCompilation();
        const { createPdfiumEngine } =
          await import("@embedpdf/engines/pdfium-worker-engine");
        // The worker engine resolves pdfium from wasmUrl itself, so the
        // precompiled module (a main-thread optimization) must not gate
        // creation; awaiting it here would only delay the engine.
        const pdfEngine = createPdfiumEngine(wasmUrl, {
          logger,
          encoderPoolSize,
          fontFallback,
        });
        if (cancelled) {
          destroyEngine(pdfEngine);
          return;
        }
        engineRef.current = pdfEngine;
        setEngine(pdfEngine);
        setIsLoading(false);
      } catch (cause) {
        if (!cancelled) {
          setError(cause instanceof Error ? cause : new Error(String(cause)));
          setIsLoading(false);
        }
      }
    })();

    return () => {
      cancelled = true;
      const current = engineRef.current;
      engineRef.current = null;
      destroyEngine(current);
    };
  }, [wasmUrl, logger, encoderPoolSize, fontFallback]);

  return { engine, isLoading, error };
}