Skip to content

Commit e223ec8

Browse files
committed
fix: prevent load multiple pdf worker when use multiple pdf files
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 4a8e649 commit e223ec8

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

src/utils/asyncReader.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
// SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
22
// SPDX-License-Identifier: AGPL-3.0-or-later
33

4-
import { getDocument, GlobalWorkerOptions } from 'pdfjs-dist'
4+
import { getDocument, GlobalWorkerOptions, PDFWorker } from 'pdfjs-dist'
55
import pdfWorkerCode from 'pdfjs-dist/build/pdf.worker.min.mjs'
66

77
GlobalWorkerOptions.workerSrc = pdfWorkerCode
88

9+
let sharedWorker = null
10+
11+
function getSharedWorker() {
12+
if (!sharedWorker) {
13+
sharedWorker = new PDFWorker({ name: 'libresign-pdf-worker' })
14+
}
15+
return sharedWorker
16+
}
17+
918
export function setWorkerPath(path) {
1019
GlobalWorkerOptions.workerSrc = path
1120
}
@@ -20,5 +29,16 @@ export function readAsArrayBuffer(file) {
2029
}
2130

2231
export function readAsPDF(file) {
23-
return getDocument(file).promise
32+
const worker = getSharedWorker()
33+
const isArrayBuffer = file instanceof ArrayBuffer
34+
const isView = ArrayBuffer.isView(file)
35+
const isBlob = typeof Blob !== 'undefined' && file instanceof Blob
36+
37+
if (file && typeof file === 'object' && !isArrayBuffer && !isView && !isBlob) {
38+
return getDocument({ ...file, worker }).promise
39+
}
40+
if (typeof file === 'string') {
41+
return getDocument({ url: file, worker }).promise
42+
}
43+
return getDocument({ data: file, worker }).promise
2444
}

0 commit comments

Comments
 (0)