svelte-pdf-view 0.1.5 → 0.1.7

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/README.md CHANGED
@@ -41,7 +41,7 @@ If you're using Vite (including SvelteKit), you may need to exclude `pdfjs-dist`
41
41
  export default defineConfig({
42
42
  // ... other config
43
43
  optimizeDeps: {
44
- exclude: ['pdfjs-dist']
44
+ exclude: ['svelte-pdf-view']
45
45
  }
46
46
  });
47
47
  ```
@@ -2,6 +2,7 @@
2
2
  import { BROWSER } from 'esm-env';
3
3
  import { onDestroy, onMount } from 'svelte';
4
4
  import { getPdfViewerContext, type PdfViewerActions } from './pdf-viewer/context.js';
5
+ import { getPdfJs } from './pdf-viewer/pdfjs-singleton.js';
5
6
  import { rendererStyles } from './pdf-viewer/renderer-styles.js';
6
7
 
7
8
  /** PDF source - can be a URL string, ArrayBuffer, Uint8Array, or Blob */
@@ -44,23 +45,6 @@
44
45
  // Core instances
45
46
  let viewer: import('./pdf-viewer/PDFViewerCore.js').PDFViewerCore | null = null;
46
47
  let findController: import('./pdf-viewer/FindController.js').FindController | null = null;
47
- let pdfjsLib: typeof import('pdfjs-dist/legacy/build/pdf.mjs') | null = null;
48
-
49
- async function initPdfJs() {
50
- if (!BROWSER) return null;
51
-
52
- pdfjsLib = await import('pdfjs-dist/legacy/build/pdf.mjs');
53
-
54
- // Create worker using import.meta.url for proper bundler resolution
55
- const worker = new pdfjsLib.PDFWorker({
56
- port: new Worker(new URL('pdfjs-dist/legacy/build/pdf.worker.mjs', import.meta.url), {
57
- type: 'module'
58
- }) as unknown as null
59
- });
60
- pdfjsLib.GlobalWorkerOptions.workerPort = worker.port;
61
-
62
- return pdfjsLib;
63
- }
64
48
 
65
49
  async function loadPdf(source: PdfSource) {
66
50
  if (!BROWSER || !scrollContainerEl) return;
@@ -69,7 +53,7 @@
69
53
  viewerState.error = null;
70
54
 
71
55
  try {
72
- const pdfjs = await initPdfJs();
56
+ const pdfjs = await getPdfJs();
73
57
  if (!pdfjs) return;
74
58
 
75
59
  const { PDFViewerCore } = await import('./pdf-viewer/PDFViewerCore.js');
@@ -228,6 +212,8 @@
228
212
  viewer = null;
229
213
  }
230
214
  findController = null;
215
+ // Note: Worker is a global singleton, not cleaned up per-component
216
+ // Use destroyPdfJs() from pdfjs-singleton.js if you need to fully cleanup
231
217
  });
232
218
  </script>
233
219
 
@@ -42,23 +42,6 @@
42
42
  // Core instances (loaded dynamically)
43
43
  let viewer: import('./pdf-viewer/PDFViewerCore.js').PDFViewerCore | null = null;
44
44
  let findController: import('./pdf-viewer/FindController.js').FindController | null = null;
45
- let pdfjsLib: typeof import('pdfjs-dist/legacy/build/pdf.mjs') | null = null;
46
-
47
- async function initPdfJs() {
48
- if (!BROWSER) return null;
49
-
50
- pdfjsLib = await import('pdfjs-dist/legacy/build/pdf.mjs');
51
-
52
- // Create worker using import.meta.url
53
- const worker = new pdfjsLib.PDFWorker({
54
- port: new Worker(new URL('pdfjs-dist/legacy/build/pdf.worker.mjs', import.meta.url), {
55
- type: 'module'
56
- }) as unknown as null
57
- });
58
- pdfjsLib.GlobalWorkerOptions.workerPort = worker.port;
59
-
60
- return pdfjsLib;
61
- }
62
45
 
63
46
  async function loadPdf(url: string) {
64
47
  if (!BROWSER || !scrollContainerEl) return;
@@ -67,8 +50,9 @@
67
50
  error = null;
68
51
 
69
52
  try {
70
- // Initialize PDF.js
71
- const pdfjs = await initPdfJs();
53
+ // Get PDF.js from global singleton
54
+ const { getPdfJs } = await import('./pdf-viewer/pdfjs-singleton.js');
55
+ const pdfjs = await getPdfJs();
72
56
  if (!pdfjs) return;
73
57
 
74
58
  // Initialize viewer
@@ -223,6 +207,8 @@
223
207
  viewer = null;
224
208
  }
225
209
  findController = null;
210
+ // Note: Worker is a global singleton, not cleaned up per-component
211
+ // Use destroyPdfJs() from pdfjs-singleton.js if you need to fully cleanup
226
212
  });
227
213
  </script>
228
214
 
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { default as PdfViewer, Toolbar as PdfToolbar, Renderer as PdfRenderer } from './PdfViewer.svelte';
2
2
  export type { PdfSource } from './PdfRenderer.svelte';
3
3
  export { getPdfViewerContext, type PdfViewerState, type PdfViewerActions, type PdfViewerContext } from './pdf-viewer/context.js';
4
+ export { destroyPdfJs } from './pdf-viewer/pdfjs-singleton.js';
package/dist/index.js CHANGED
@@ -2,3 +2,5 @@
2
2
  export { default as PdfViewer, Toolbar as PdfToolbar, Renderer as PdfRenderer } from './PdfViewer.svelte';
3
3
  // Export context for custom toolbars
4
4
  export { getPdfViewerContext } from './pdf-viewer/context.js';
5
+ // Export PDF.js singleton utilities
6
+ export { destroyPdfJs } from './pdf-viewer/pdfjs-singleton.js';
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Get the PDF.js library instance. Creates the worker on first call.
3
+ * Subsequent calls return the cached instance.
4
+ */
5
+ export declare function getPdfJs(): Promise<typeof import('pdfjs-dist/legacy/build/pdf.mjs') | null>;
6
+ /**
7
+ * Destroy the PDF.js worker and cleanup resources.
8
+ * Call this when you're completely done with PDF viewing in your app.
9
+ * Note: After calling this, the next getPdfJs() call will create a new worker.
10
+ */
11
+ export declare function destroyPdfJs(): void;
@@ -0,0 +1,51 @@
1
+ import { BROWSER } from 'esm-env';
2
+ // Global singleton state
3
+ let pdfjsLib = null;
4
+ let pdfWorker = null;
5
+ let rawWorker = null;
6
+ let initPromise = null;
7
+ /**
8
+ * Get the PDF.js library instance. Creates the worker on first call.
9
+ * Subsequent calls return the cached instance.
10
+ */
11
+ export async function getPdfJs() {
12
+ if (!BROWSER)
13
+ return null;
14
+ // Return cached instance
15
+ if (pdfjsLib && pdfWorker)
16
+ return pdfjsLib;
17
+ // If initialization is in progress, wait for it
18
+ if (initPromise)
19
+ return initPromise;
20
+ // Start initialization
21
+ initPromise = (async () => {
22
+ pdfjsLib = await import('pdfjs-dist/legacy/build/pdf.mjs');
23
+ // Create worker only once using import.meta.url for proper bundler resolution
24
+ rawWorker = new Worker(new URL('pdfjs-dist/legacy/build/pdf.worker.mjs', import.meta.url), {
25
+ type: 'module'
26
+ });
27
+ pdfWorker = new pdfjsLib.PDFWorker({
28
+ port: rawWorker
29
+ });
30
+ pdfjsLib.GlobalWorkerOptions.workerPort = pdfWorker.port;
31
+ return pdfjsLib;
32
+ })();
33
+ return initPromise;
34
+ }
35
+ /**
36
+ * Destroy the PDF.js worker and cleanup resources.
37
+ * Call this when you're completely done with PDF viewing in your app.
38
+ * Note: After calling this, the next getPdfJs() call will create a new worker.
39
+ */
40
+ export function destroyPdfJs() {
41
+ if (pdfWorker) {
42
+ pdfWorker.destroy();
43
+ pdfWorker = null;
44
+ }
45
+ if (rawWorker) {
46
+ rawWorker.terminate();
47
+ rawWorker = null;
48
+ }
49
+ pdfjsLib = null;
50
+ initPromise = null;
51
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-pdf-view",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "A modern, modular PDF viewer component for Svelte 5. Built on PDF.js with TypeScript support",
5
5
  "author": "Louis Li",
6
6
  "license": "Apache-2.0",