musescore-downloader/src/worker-helper.ts

31 lines
794 B
TypeScript
Raw Normal View History

2020-05-17 22:57:28 +00:00
import { PDFWorkerMessage } from './worker'
import { PDFWorker } from '../dist/cache/worker'
2020-09-27 15:45:51 +00:00
const scriptUrlFromFunction = (fn: () => any): string => {
2020-05-17 22:57:28 +00:00
const blob = new Blob(['(' + fn.toString() + ')()'], { type: 'application/javascript' })
2020-12-01 19:10:36 +00:00
return window.URL.createObjectURL(blob)
}
export class PDFWorkerHelper extends Worker {
2020-05-17 22:57:28 +00:00
constructor () {
const url = scriptUrlFromFunction(PDFWorker)
super(url)
}
2020-05-17 22:57:28 +00:00
generatePDF (imgURLs: string[], imgType: 'svg' | 'png', width: number, height: number): Promise<ArrayBuffer> {
const msg: PDFWorkerMessage = [
imgURLs,
imgType,
width,
height,
]
this.postMessage(msg)
return new Promise((resolve) => {
this.addEventListener('message', (e) => {
resolve(e.data)
})
})
}
}