Adding Force Unsubscribe upon new Subscription
See original GitHub issueFeature Request
With pipable operator, RxJS strongly recommends to create new features via pipable function and publish it as user-land module. If this feature request is
Is your feature request related to a problem? Please describe. The problem this would solve is this not being able to subscribe to an observable and cancelling in-flight requests at the same time.
btnClick() {
this._subscriptions.add(this.obs$.subscribe()); //This request will fire every time this button is clicked
}
Describe the solution you’d like
btnClickFeatureRequest() {
this._subscription.addClean(this.obs$.subscribe());
//follows "Remove" fn() but also calls .unsubscribe() before splicing it.
//Allowing this method to be called X times and only ever returning the last result.
}
Ex: (in Subscription.ts)
addClean(subscription: Subscription) {
const subscriptions = this._subscriptions;
if (subscriptions) {
const subscriptionIndex = subscriptions.indexOf(subscription);
if (subscriptionIndex !== -1) {
subscriptions[subscriptionIndex].unsubscribe();//THIS!!!
subscriptions.splice(subscriptionIndex, 1);
}
}
}
this.add(subscription); //THIS!!!
Describe alternatives you’ve considered
DestinctUntil, TakeUntil -> Which both require setting up multiple subscribers and more variables making a component with 20+ observables that are being subscribed to very unclean.
(If this is new operator request) describe reason it should be core operator Cleaner code for the end coder, having to add multiple subscribers to cancel within the call is just unclean.
Additional context Will add if request is made.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)

Top Related StackOverflow Question
It’d work I guess, if not only few modification required.
I don’t think we’ll have in core, cause we intentionally deprecated from v5.
Cloaing for now as I believe answe itself has provided.
This sounds like serialsubscription (https://github.com/paulcbetts/rxjs-serial-subscription) which we have userland implementation as well. Also it’s quite easy to create your own subscription and let it parent subscription to achieve desired behavior.