Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Ability to react to progress events of $http XHR #1934

Closed
ryankshaw opened this issue Feb 1, 2013 · 153 comments · Fixed by #14367
Closed

Ability to react to progress events of $http XHR #1934

ryankshaw opened this issue Feb 1, 2013 · 153 comments · Fixed by #14367

Comments

@ryankshaw
Copy link

I would like to be able to bind to the progress event of the xhr upload.

for example (in plain javascript):

var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false)

(so I can show a progress meter for uploaded files)

I want to be able to do the same thing by doing $http.post...

for this and any other case edge case where you need to interact with the raw XHR object itself it might be nice to provide something that lets you just get at the original xhr object.

@michaeltaranto
Copy link

+100! Really need to be able to access the internal xhr object of $http service.

@bjconlan
Copy link

bjconlan commented Apr 1, 2013

Providing an 'xhr' config option (which I assume would be callback that gets the xhr instance passed in) is probably the simplest option to implement (and also means that we don't need to update angular every xhr spec change), but I think the correct way to do this is to extend the $q object to support a progress handler callback as per http://wiki.commonjs.org/wiki/Promises/A. This keeps with the nice abstraction angular provides to the $http object and since it is already based upon $q promises it could then provide something akin to what you are attempting to do (bind to the progress events)

Perhaps #2223 can help with this.

mbj referenced this issue Apr 4, 2013
this was never meant to be a public api used by apps. I refactored
the code to hide the functionality.

BREAKING CHANGE: $browser.addJs method was removed

apps that depended on this functionality should either use many of the
existing script loaders or create a simple helper method specific to the
app.
@aminariana
Copy link

+10

My issue is that I'd like to cancel the request if it wasn't completed upon new request.

I'm issuing many search queries async (depending on the user behavior) and there's severe race condition. The work-around I see right now is to fall back on jQuery.ajax method, simply because that's the only thing I'm familiar with that provides an XHR that I can cancel upon new request.

@trea
Copy link

trea commented May 30, 2013

+1 I'd also like to see something implemented for this purpose.

Thanks!

@magro
Copy link

magro commented Sep 12, 2013

+1

6 similar comments
@miki725
Copy link

miki725 commented Oct 7, 2013

+1

@gustavohenke
Copy link

+1

@danialfarid
Copy link

+1

@anormal81
Copy link

+1

@lu4
Copy link

lu4 commented Nov 7, 2013

+1

@cgwyllie
Copy link
Contributor

+1

@lovenigma
Copy link

+1

1 similar comment
@bhalperin28
Copy link

+1

@Guuz
Copy link

Guuz commented Dec 3, 2013

👍 I was surprised when I noticed this is not possible in AngularJS...

@miki725
Copy link

miki725 commented Dec 3, 2013

Any plans to solve this. Maybe in 1.3?

@IlanFrumer
Copy link

+1

@miki725
Copy link

miki725 commented Dec 14, 2013

+1

P.S.: I already +oned however posting again since not sure if my initial "+1" will count towards the 1.3 feature poll.

@magro
Copy link

magro commented Dec 14, 2013

+1

4 similar comments
@Guuz
Copy link

Guuz commented Dec 16, 2013

+1

@marcalj
Copy link

marcalj commented Dec 16, 2013

+1

@thvd
Copy link

thvd commented Dec 16, 2013

+1

@michalkvasnicak
Copy link

+1

@gustavohenke
Copy link

@miki725 seems like it's planned for the 1.3 milestone :)

@standuprey
Copy link
Contributor

+1

@atticoos
Copy link

It would be nice if we could just use the promise's progress callback

From kriskowal/q docs

promise.then(onFulfilled, onRejected, onProgress)

The then method from the Promises/A+ specification with an additional progress handler.

I see no reason why we can't use .then here and expect onProgress to be reported to

$http({
  method: 'POST',
  data: jsonData
}).then(function () {
  // on succes
}, function () {
  // on error
}, function () {
  // on progress
});

Or, if the .success and .error are preferred

$http.post(url, data).success(function () {
  // on success
}).error(function () {
  // on error
}).then(null, null, function () {
  // on progress
});

@benjamingr
Copy link
Contributor

@ajwhite I humbly disagree, it would be horrible to use the progress event, not only is it being removed from Kris Kowal's Q (check the v2 branch) and the Q authors are themselves against it - it will not be included in native promises (ever) and is a generally poor idea.

Please refer to the above discussion starting here:

#1934 (comment)

@atticoos
Copy link

@benjamine please refresh, I noticed the depreciation notice in the docs & redacted

@atticoos
Copy link

@benjamine also caught up on your conversation. With the points being made towards the deprecation of all progress events, even in .then(null, null, progress), it makes less sense to build support for it this way. With Bluebird & Q moving away from that support, a configuration property seems to make a bit more sense, and leave promises to their responsibility

@benjamingr
Copy link
Contributor

Glad we agree promise progress events are not the way forward :)

@caitp
Copy link
Contributor

caitp commented Jan 31, 2015

the progress api from promises is certainly not the way to do this (we've known this for some time, for instance, it would kind of suck to have to always register a progress listener for XHR, and we'd still not know whether we're talking about XHRUpload or just XHR, it doesn't map well to this api at all!)

Sooooo, basically, we need some other way to do this that plays nice with the current promise-y way of doing things, and don't really have that yet. :(

@benjamingr
Copy link
Contributor

@caitp what's wrong with allowing optional callbacks to the options object of the $http call?

@arcanis
Copy link

arcanis commented Jan 31, 2015

About this :

should progress notification handlers automatically trigger $digest cycle (if they don't than the whole feature is a bit useless and if they do we need to be careful about not causing perf problems - maybe we should debounce those events and call handlers + $digest only every x ms?)

I think the debounce is a good solution. We don't need them to be called often, we just need them to be called enough to give some kind of feedback to the user. Even something like once per second would be enough, by adding some css transitions here and there.

@chrisirhc
Copy link
Contributor

Since there are a number of progress events that can fire loadstart, progress, load, timeout, abort, error, … How about allowing the user to specify an object to attach the events (eventName: eventListener)?
For example the object could look like the following:

var eventCallbacks = {
  loadstart: function onLoadStart() {},
  progress: function onDownloadProgress() {},
  upload: {
    progress: function onUploadProgress() {}
  }
};

Then the $httpBackend, would call xhr.addEventListener(<eventName>, <function>); to attach the event listeners. Callbacks defined under the upload attribute would be bound to the xhr.upload.

This also allows for users to bind to readystatechange if they need to react to readyState == 3 loading events.

By stating that these are raw event listeners, you can also get around needing to invoke a digest as the user is expected to do so. It also gets around Angular needing to polyfill different browser's behavior.

I'll be happy to get a PR going.

@chrisirhc
Copy link
Contributor

Added a PR #11547 to illustrate what I meant above.

@benatkin
Copy link

Here's a directive that has a clever but terrible hack to get around it.

How it works: it replaces window.XMLHttpRequest.prototype.setRequestHeader with a function that calls the original, except when it finds __setXHR_ with a callback function, in which case it calls it.

@nikkwong
Copy link

nikkwong commented Jul 1, 2015

Is there any consensus on how best to achieve this for the time being?

@ryanki1
Copy link

ryanki1 commented Jul 20, 2015

This topic is already 2 years old - have we already made a start on implementing it?

@y-x-c
Copy link

y-x-c commented Jul 26, 2015

+1

3 similar comments
@nastakhov
Copy link

+1

@nicolas-alie
Copy link

+1

@weisk
Copy link

weisk commented Sep 7, 2015

+1

@petebacondarwin petebacondarwin modified the milestones: 1.5.x - migration-facilitation, 1.3.x - superluminal-nudge Sep 8, 2015
@petebacondarwin
Copy link
Member

Moved this to 1.5 - let's discuss the implementation at #11547

@angular angular locked and limited conversation to collaborators Sep 8, 2015
gkalpak pushed a commit to gkalpak/angular.js that referenced this issue Nov 2, 2018
gkalpak pushed a commit to gkalpak/angular.js that referenced this issue Nov 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.