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

Question: Best practice for Ajax Progress #2424

Closed
Brooooooklyn opened this issue Feb 28, 2017 · 1 comment
Closed

Question: Best practice for Ajax Progress #2424

Brooooooklyn opened this issue Feb 28, 2017 · 1 comment

Comments

@Brooooooklyn
Copy link
Contributor

I was tried to implement a progress subscriber here:
https://github.com/Brooooooklyn/learning-rxjs/blob/article3/src/FileUploader.ts#L171

but it seems not so elegant, and I've tried such things:

const host = `${apiHost}/upload/chunk/${meta.fileKey}?chunk=${index + 1}&chunks=${meta.chunks}`
const subscriber = new Subscriber()
return Observable.ajax({
  url: host,
  body: blob,
  method: 'post',
  crossDomain: true,
  headers: { 'Content-Type': 'application/octet-stream' },
  progressSubscriber: subscriber
})
  .takeUntil(this.pause$)
  .repeatWhen(() => this.resume$)

But can not repeat subscriber when Error occurred.

@trxcllnt
Copy link
Member

@Brooooooklyn You should be able to retry by multicasting progress events through a Subject. This is clever, but should do the job:

Observable.never().multicast(
    () => new Subject(),
    (subject) => subject
        .map((data) => ({ type: 'xhr-progress', data }))
        .merge(Observable.ajax({
            /* etc. */
            progressSubscriber: subject
        })
        .map((data) => ({ type: 'xhr-response', data })))
  )
  .takeUntil(this.pause$)
  .repeatWhen(() => this.resume$)

@benlesh benlesh closed this as completed Jun 7, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jul 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants