Rxjs: Subscription dispose() or unsubscribe()

Created on 15 Jul 2015  路  24Comments  路  Source: ReactiveX/rxjs

Currently it's unsubscribe, but I feel it should be dispose. @zenparsing and @jhusain, what aligns with the ES7 proposal?

What are other people's feelings on this?

Most helpful comment

The issue with dispose, for me, is that "dispose" means something very specific and foundational in C#, whereas JS has no such notion. Using "dispose" would imply that it is some core architectural hook, which it clearly is not.

I think "cancel" would be a fine alternative though.

let subscription = observable.subscribe({ });
subscription.cancel();

In fact, "unsubscribe" is a bit of a finger-twister to type.

Interestingly, in my Mac's dictionary, "unsubscribe" is defined as:

cancel a subscription to an electronic mailing list or online service.

I like "cancel". : )

All 24 comments

This relates to #75

The issue with dispose, for me, is that "dispose" means something very specific and foundational in C#, whereas JS has no such notion. Using "dispose" would imply that it is some core architectural hook, which it clearly is not.

I think "cancel" would be a fine alternative though.

let subscription = observable.subscribe({ });
subscription.cancel();

In fact, "unsubscribe" is a bit of a finger-twister to type.

Interestingly, in my Mac's dictionary, "unsubscribe" is defined as:

cancel a subscription to an electronic mailing list or online service.

I like "cancel". : )

LOL.. You're not helping narrow it down! ... So one vote for cancel

That's not bad.

+1 for cancel. Currently, the subscription object has the "unsubscribed" getter. This should probably be renamed into isCanceled.

-10000000 for cancel because naturally Subscriptions will enter into the disposed phase after onCompleted or return has been called. To say cancel would imply that something bad happened or had to happen which is not the case for 99% of the cases.

dispose works because it implies a cleanup action is taking place which is ultimately true, for example with events. unsubscribe doesn't quite reveal that intent either which is unfortunate.

@mattpodwysocki

"Cancel" makes perfect sense as a method name on the subscription object.

subscription.cancel();  // Cancel the subscription, please

But if I understand your point, it doesn't make a whole lot of sense when returning something from the subscriber function:

new Observable(observer => {
    // Whatever
    return {
        cancel() { /* Do cleanup */ }
    };
});

Isn't allowing the subscriber function to return a subscription just a convenience for chaining though? You can always just write it out:

new Observable(observer => {
    let innerSubscription = something.subscribe(observer);
    return _=> { innerSubscription.cancel() };
});

Written out like this, "cancel" makes sense to me.

@zenparsing cancel to me is an overloaded word. It could mean I really want to cancel the transaction, or I really really don't care about the value that comes through. That's one reason I don't like it.

In WinJS parlance, this makes sense:

var promise = new WinJS.Promise(init, onCancel);
promise.cancel();

Because the intent of calling cancel on a Promise is indeed to cancel the action and send it into the error state.

The primary usage of the Subscription is to control lifetimes, and yes, the Subscriptions are chained together so that they can clean up at the end. That's why cancel seems wrong because when return is called, cancel is automatically called, thus confusing people what the word cancel actually means.

unsubscribe hardly ever cancels something.

@headinthebox @zenparsing correct, unsubscribe is not really for cancel more than it is to say I'm done listening and here is some cleanup that you should do as well, in the case of events, detaching the event handlers.

Exactly. If you have several subscriptions to say mouseMoves unsubscribing one of them does not cancel anything.

It's a matter of how to read the word "cancel".

Unsubscribe to mouseMoves doesn't cancel anything, but subscription.cancel() would precisely _cancel_ that subscription to mouseMoves. This is pretty much the same case as unsubscribing from a newsletter.

I like both cancel and unsubscribe. The latter is very common in mailing lists.

How about a MadLib?

  1. SUCCESS: I'm a producer, and I've succesfully finished producing values and called the complete method, which calls 1a)__verb__ internally, and now it's in a 1b)__past_tense_verb__ state.
  2. FAILURE: I'm a producer, and I could not finish producing values, so I called the error method, which calls 2a)__verb__ internally, and now it's in a 2b)__past_tense_verb__ state.
  3. QUIT: I'm a consumer, and I don't care about the values I'm getting any more so I called 3a)__verb__ and now it's in a 3b)__past_tense_verb__ state.

IMHO there is no ambiguity in the pair subscribe and unsubscribe, the second undoes the first. When I see subscribe and cancel I am not sure how far reaching cancel is. It could very well cancel _all_ outstanding subscriptions, or cancel the underlying work or whatever.

@headinthebox +1000 on that one, cancel is just a bad choice here because of its ambiguous meaning.

I'm actually shocked cancel appeared and has more legs than dispose. I figured dispose was going to be more of a favorite because of it's backwards compatibility with older Rx.

I agree with @mattpodwysocki that cancel/cancelled has too much weight when you think about success. If something completes, it's then not "cancelled", it _is_ unsubscribed, though.

... but at the same time, one could "cancel" a subscription, and have it "unsubscribed" internally.

MHO there is no ambiguity in the pair subscribe and unsubscribe

unsubscribe = :heart:

Why are we even discussing cancel as an option?

Why are we even discussing cancel as an option?

@zenparsing :P haha.

Is that what the es-observable spec is looking to do, @zenparsing?

@blesh Not really, just throwing out a personal opinion.

:+1: for unsubscribe. Its meaning is immediately grokked.

I feel like we've reached a consensus with unsubscribe.

Woot! I am the straw that broke the camels back!

I think it comes from Pub Sub ?
Publish Subscribe(Unsubscribe)
When you listening radio and the radio is subscribed to radio station which publishes waves how can even cancel is an option :D
It is a state of mind to think like an observable, particle, wave (people may discuss a lot about particle or wave :D)

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings