comlink-loader
comlink-loader copied to clipboard
Use of onmessage
Hi, great library, I am so glad I found this, it works almost great with my vuejs app.
the only issue I am having trouble is trying to send a message to the main thread which I cant do myself nor find in the docs.
The thing is that I am sending an array of files to my worker which uploads one by one (that works perfectly) but I am trying to send a "uploadCompleted" or "error" variable back to the client and I am not able to do it with a return since it takes too long.
here is a piece of my code:
//Component.vue
files = [...bunchOfFiles]
await WorkerUploader(files)
//worker.js
export const WorkerUploader = async files => {
const { relativePath, file, putAction, headers } = fileObj
files.map(file => {
let xhr = new XMLHttpRequest()
// handle error
xhr.upload.onerror = function() {
console.log(`Error during the upload: ${xhr.status}.`)
return { status: xhr.status, relativePath }
}
// upload completed successfully
xhr.onload = function() {
console.log('Upload completed successfully.')
return { status: xhr.status, relativePath }
}
// add custom url
xhr.open('PUT', putAction)
//add header
Object.keys(headers).map(key => {
xhr.setRequestHeader(key, headers[key])
})
})
}
Thank you!
Maybe see Comlink callbacks?