Skip to content

Commit f033fc6

Browse files
authored
Merge pull request #12 from Randomblock1/perf/qpdf-singleton-2438142539747209253
⚡ Optimize QPDF WASM initialization
2 parents 4b417b5 + 9bf90bd commit f033fc6

3 files changed

Lines changed: 35 additions & 38 deletions

File tree

src/lib/qpdf.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import createModule, { type QpdfInstance } from '@neslinesli93/qpdf-wasm';
2+
import wasmUrl from '@neslinesli93/qpdf-wasm/dist/qpdf.wasm?url';
3+
4+
let qpdfPromise: Promise<QpdfInstance> | null = null;
5+
6+
export function getQpdf(): Promise<QpdfInstance> {
7+
if (!qpdfPromise) {
8+
qpdfPromise = createModule({
9+
locateFile: () => wasmUrl,
10+
// @ts-expect-error missing from types
11+
noInitialRun: true,
12+
preRun: [
13+
(module: QpdfInstance) => {
14+
if (module.FS) {
15+
try {
16+
module.FS.mkdir('/input');
17+
module.FS.mkdir('/output');
18+
} catch (e) {
19+
console.warn('Error creating directories:', e);
20+
}
21+
}
22+
}
23+
]
24+
});
25+
}
26+
return qpdfPromise;
27+
}
28+
29+
export type { QpdfInstance };

src/routes/+page.svelte

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
faCircleExclamation,
66
faTriangleExclamation
77
} from '@fortawesome/free-solid-svg-icons';
8-
import createModule, { type QpdfInstance } from '@neslinesli93/qpdf-wasm';
9-
import wasmUrl from '@neslinesli93/qpdf-wasm/dist/qpdf.wasm?url';
8+
import { type QpdfInstance } from '@neslinesli93/qpdf-wasm';
9+
import { getQpdf } from '$lib/qpdf';
1010
1111
import { onMount } from 'svelte';
1212
import { fileListFromFileArray, convertFileHandlesToFileList, download } from '$lib/file-utils';
@@ -21,23 +21,7 @@
2121
onMount(async () => {
2222
// Initialize QPDF WASM module
2323
try {
24-
qpdf = await createModule({
25-
locateFile: () => wasmUrl,
26-
// @ts-expect-error missing from types
27-
noInitialRun: true,
28-
preRun: [
29-
(module: QpdfInstance) => {
30-
if (module.FS) {
31-
try {
32-
module.FS.mkdir('/input');
33-
module.FS.mkdir('/output');
34-
} catch (e) {
35-
console.warn('Error creating directories:', e);
36-
}
37-
}
38-
}
39-
]
40-
});
24+
qpdf = await getQpdf();
4125
console.log('QPDF WASM module initialized successfully');
4226
} catch (error) {
4327
console.error('Failed to initialize QPDF WASM module:', error);

src/routes/decrypt/+page.svelte

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<script lang="ts">
22
import { FontAwesomeIcon } from '@fortawesome/svelte-fontawesome';
33
import { faInfoCircle } from '@fortawesome/free-solid-svg-icons';
4-
import createModule, { type QpdfInstance } from '@neslinesli93/qpdf-wasm';
5-
import wasmUrl from '@neslinesli93/qpdf-wasm/dist/qpdf.wasm?url';
4+
import { type QpdfInstance } from '@neslinesli93/qpdf-wasm';
5+
import { getQpdf } from '$lib/qpdf';
66
77
import { onMount } from 'svelte';
88
import { fileListFromFileArray, convertFileHandlesToFileList, download } from '$lib/file-utils';
@@ -15,23 +15,7 @@
1515
onMount(async () => {
1616
// Initialize QPDF WASM module
1717
try {
18-
qpdf = await createModule({
19-
locateFile: () => wasmUrl,
20-
// @ts-expect-error missing from types
21-
noInitialRun: true,
22-
preRun: [
23-
(module: QpdfInstance) => {
24-
if (module.FS) {
25-
try {
26-
module.FS.mkdir('/input');
27-
module.FS.mkdir('/output');
28-
} catch (e) {
29-
console.warn('Error creating directories:', e);
30-
}
31-
}
32-
}
33-
]
34-
});
18+
qpdf = await getQpdf();
3519
console.log('QPDF WASM module initialized successfully');
3620
} catch (error) {
3721
console.error('Failed to initialize QPDF WASM module:', error);

0 commit comments

Comments
 (0)