Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of onmessage #25

Open
vicente-dl opened this issue Jul 9, 2020 · 1 comment
Open

Use of onmessage #25

vicente-dl opened this issue Jul 9, 2020 · 1 comment

Comments

@vicente-dl
Copy link

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!

@niutech
Copy link

niutech commented Nov 12, 2020

Maybe see Comlink callbacks?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants