-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Cancellation Documentation #817
Comments
I would be also interested to know about this. Also, in this example: https://github.com/Reactive-Extensions/RxJS/blob/master/examples/autocomplete/autocomplete.js , Is there a simple way we could refactor this example to fix this in a RxJS style? Thanks |
Cancellation comes in two flavors: explicit and implicit. Explicit This refers to actively disposing of a subscription when you are done using it. var subscription = Rx.Observable
.interval(2000).scan(0, (acc, x) => acc + x)
.subscribe(total => console.log(total));
//Some time in the future
subscription.dispose(); In the above, I am cancelling the subscription to the observable explicitly which immediately cancels the future scheduled actions. Implicit This is more toward @gre 's comment. Probably the more common scenario, here the To take the example of the autocomplete var searcher = keyup.flatMapLatest(searchWikipedia); First thing I would recommend is that you go look at the documentation for Now what that cancellation means really depends on the sequence and what the underlying source supports. RxJS always provides best effort cancellation for in-flight requests. However this won't always translate to cancelling all the in motion actions, the only guarantee you have is that your down stream If you take the example of the autocomplete, since the ajax request is a promise (which cannot be cancelled) cancelling it will only stop the event from being emitted not the ajax request. Something like the |
Actually an XMLHttpRequest does have an .abort() method. So I think we need
|
I can't speak for the original intent of the example, it may be trying to highlight how to strangle out old code, or how RxJS plays nice with other libraries. That being said, it might make sense to use RxJS-DOM which does have abortable Ajax. But it would need to make that inclusion clear. |
I guess that is already possible no? https://github.com/Reactive-Extensions/RxJS-DOM/blob/master/src/ajax.js#L186 I don't know the syntax however. |
@gre yes, it is possible to use |
@aldendaniels I think @paulpdaniels answered this correctly as it is a best try cancellation which means if the XHR/Promise/Whatever has not completed, then it will call abort if the |
@mattpodwysocki : thanks for the feedback, great to know we can do it, I was just very curious how easy it was to make it :) BTW in RxJS-DOM, the autocomplete example uses jsonp, that is cancellable, so great :) Cheers |
@mattpodwysocki, @paulpdaniels - Thanks! I'm new to RX, but I've used Promises extensively. The promise spec doesn't (yet) provide cancellation semantics, but some promise libraries are inventing there own. Since a Promise only returns a single value, it has a "pending" state while the promise is awaiting resolution. Since the promise has a pending state, it makes sense that you can cancel a promise when (and only when) it's pending. Cancellation implies two things:
Check out petkaantonov/bluebird#415 (comment) for more on this. Example: let dataPromise = fetch(ur)
.then(transformData);
window.setTimeout(() => {
dataPromise.cancel(); // Calls some onCancel() method that fetch() setup.
}, 100); This is great for Promises, but doesn't map easily to observables. With observables, we can shut down the pipe (unsubscribe), but not cancel the in-flight operation. From what I can tell, Rx observable subscribers don't know when an observable is "pending". If you had a way to tell an observable chain to expect a currently pending value, then it might be possible to cancel the currently pending action. You could probably accomplish this today via an observable stream of promises. If you're mapping events to API requests for example, this would become a sync operation that maps events to promises. Then those promises could be cancelled using something like Bluebirds upcoming cancellation API. Am I thinking correctly? |
@aldendaniels As was mentioned above, Rx uses best effort cancellation, meaning that it will do whatever it reasonably can to clean up the resources it created. When dealing with It is important to note as well that explicit cancellation is not part of the This kind of cancellation covers not just things like removing event handlers, but in the case of certain operators like Does that make it any clearer? It also might be something to bring up on the gitter to feedback faster. |
@paulpdaniels - Was clearing out old emails and realized that I never responded to your post. My apologies! This is very helpful and a great starting point for getting my mind around cancellation with RxJS. Thanks for taking the time to help explain. |
The RxJS docs state that:
I'm struggling to find documentation regarding cancellation. Specifically, I'm looking for a way to propagate a cancelation event backwards through the observable chain to the observable source.
For example, imagine that I'm aggregating the result of three XHR requests. If one of the requests responds with an error code, I want to cancel the other two outgoing requests.
Does RxJS provide an idiom for this kind of cancellation? If not, it would help to clarify what is meant by "cancellation" in the docs.
Thanks!
The text was updated successfully, but these errors were encountered: