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

Allow to clear all subscribers for a topic by changing uid to a number #60

Closed
pvdz opened this issue Jul 3, 2014 · 5 comments
Closed

Comments

@pvdz
Copy link

pvdz commented Jul 3, 2014

I wanted to do PubSub.unsubscribe('topic') but can't. Instead to unsubscribe an event I either have to maintain the returned ids or I have to maintain a reference to the handler function.

If the unique id returned by subscribe were to just be var token = ++lastUid, you could overload the unsubscribe method by checking for a number to do the same as strings do right now. Then you could use the string case to drop all handlers for a certain topic.

The only impact I see right now is to users who rely on the subscribe return value being a string. I'm not sure why anyone would though since for them it's just a token.

The fixes are trivial but I don't know whether you're open to such a change.

@mroderick
Copy link
Owner

If I understand correctly, you would like a way to remove all subscriptions for a topic.

A dedicated method would probably be better than further overloading the unsubscribe method. Perhaps it could look like: PubSub.clearSubscriptions('topic')

How would that affect hierarchical topics?

var func = function NOOP(){};

PubSub.subscribe('a', func);
PubSub.subscribe('a.b', func);
PubSub.subscribe('a.b.c', func);

PubSub.clearSubscriptions('a.b');

PubSub.publish('a.b.c', { hello : 'world' });

// Would func still be notified of `a.b.c` topic?

@pvdz
Copy link
Author

pvdz commented Jul 11, 2014

To be frank I had not considered hierarchical topics. But I suppose it would work the same way because, why not. If you unsubscribe a parent, like a.b in your example, it would make sense that any sub-topics would also be unsubscribed.

@mroderick
Copy link
Owner

I think that I am unlikely to use such a method myself, as I prefer to design parts of systems to not have too much knowledge about other parts of systems (so, in one part, I might not know if it would be safe to remove all subscriptions).

However, I think that with the right level of documentation, such a method could be useful in some scenarios.

@pvdz
Copy link
Author

pvdz commented Jul 11, 2014

The use case could be cleanup of an app, removing all global-level listeners when they are no longer needed because the app served it's purpose.

@mroderick
Copy link
Owner

This has been implemented with #62, and is included in 1.5.0

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

No branches or pull requests

2 participants