comlink-loader icon indicating copy to clipboard operation
comlink-loader copied to clipboard

Use of onmessage

Open vicente-dl opened this issue 5 years ago • 1 comments

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!

vicente-dl avatar Jul 09 '20 20:07 vicente-dl

Maybe see Comlink callbacks?

niutech avatar Nov 12 '20 22:11 niutech