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

Notifications for xhr.upload.onprogress #9

Merged
merged 8 commits into from
Jul 23, 2015
Merged

Notifications for xhr.upload.onprogress #9

merged 8 commits into from
Jul 23, 2015

Conversation

demurgos
Copy link
Contributor

Hi,
Currently, q-xhr uses deferred.notify to track the progression of the download of the response. It would be nice to be able to track the upload when available.

Angular's $http being backed by $q, they don't have the deferred.notify API so it's not possible to copy their implementation. (it's the goal of your lib if I'm not mistaken ?)
Still, reacting to progress is a popular feature so here is a proposition for the implementation:

  • To avoid to break compatibility with applications expecting Q.xhr({...}).progress to only trigger on download progress, applications have to explicitly ask for upload progress notifications with the new config property watchUpload (boolean).

  • It's possible to set Q.xhr.defaults.watchUpload = true

  • Since there will be two sorts of progress calls, it should be possible to differentiate them. Both type of progress objects are instances of ProgressEvent. To differentiate them, it's possible to use:

    Q
      .xhr({url: '/foo', method: 'GET', watchUpload: true})
      .progress(function(progress) {
        if(window.XMLHttpRequestUpload && progress.target instanceof XMLHttpRequestUpload) {
          // upload progress
        }else{
          // download progress
        }
      })

    window.XMLHttpRequestUpload is a quick feature detection to avoid to throw on progress.target instanceof undefined when executed on older browsers.

    This test on the user side does not seem like a good idea (hard to maintain, boilerplate) so I add the flag upload when the progress event is received by the upload.onprogress callback. This way, the above example becomes simply:

    Q
      .xhr({url: '/foo', method: 'GET', watchUpload: true})
      .progress(function(progress) {
        if(progress.upload) {
          // upload progress
        }else{
          // download progress
        }
      })

What do you think about it ? (Maybe the names of the option and flag should change ?)

Triggers deferred.notify during upload progress if the config option `watchUpload` is set to true.
Adds the property `upload = true` to the upload progress event to easily detect the type of progress event (download or upload).
@nathanboktae
Copy link
Owner

Thanks for the pull request! I like it - a couple of things:

so it's not possible to copy their implementation. (it's the goal of your lib if I'm not mistaken ?)

No it's not. $http is a basis and a few points I did not explicitly implement are on in the readme.

I would prefer to remove the flag and take a major version bump. This is a nice feature and I don't see a reason to turn it off, definitely not by default.

Also documentation and a test (integration is good but unit only is fine) please. I see the build is failing on the saucelabs part - I'll check to see if my saucelabs key is still good but I am not worried about that.

@demurgos
Copy link
Contributor Author

Thank you for your quick answer, I'll complete the pull request tomorrow.

About $http, I wasn't sure if the goal was to stick with their API or not: thanks for clarification.

I'll drop watchUpload, but what about the easy way to differentiate between upload & download progress events ? Should I still augment the original event with progress.upload=true ?

@nathanboktae
Copy link
Owner

Should I still augment the original event with progress.upload=true ?

Yes, I like that approach.

You can bump to 1.0.0 btw - I was meaning to do that for some time. The project is stable.

@demurgos
Copy link
Contributor Author

I updated my changes but I'm not so familiar with mocha & coffee script so I screw up the tests. Could you take a look at it ?

@nathanboktae
Copy link
Owner

Yup I can update the tests. The changes are solid. thanks!

nathanboktae added a commit that referenced this pull request Jul 23, 2015
Notifications for xhr.upload.onprogress
@nathanboktae nathanboktae merged commit 25efee7 into nathanboktae:master Jul 23, 2015
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

Successfully merging this pull request may close these issues.

2 participants