musescore-downloader/src/worker-helper.ts

31 lines
786 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-05-17 22:57:28 +00:00
const scriptUrlFromFunction = (fn: Function): string => {
const blob = new Blob(['(' + fn.toString() + ')()'], { type: 'application/javascript' })
return 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)
})
})
}
}