thomaslevesque / WeakEvent

Generic weak event implementation
Apache License 2.0
182 stars 23 forks source link

How to clear all subscriptions? #47

Closed DaveInCaz closed 2 years ago

DaveInCaz commented 3 years ago

Thanks for creating this library, I'm finding it very useful.

With a standard C# event handler you could clear all subscribers by setting it equal to null from within its class. Using WeakEventSource, is there a way to do something equivalent?

Maybe just creating a new WeakEventSource<> object is the way to do that?

thomaslevesque commented 3 years ago

Hi @DaveInCaz,

Yes, you can just recreate a new instance. It might make sense to add a Clear method, though!

DaveInCaz commented 3 years ago

Thanks. The reason I wondered about clearing it is if that would have some advantage in freeing the subscribers sooner, like if it would allow an earlier garbage collection than if the weak event source were itself just dereferenced. If not (which is what it sounds like), then it probably isn't needed.

Admittedly this is like the opposite problem that weak events were intended to solve :)

thomaslevesque commented 3 years ago

The reason I wondered about clearing it is if that would have some advantage in freeing the subscribers sooner

Why would you want to free the subscribers sooner, though? They decided to subscribe to the event, so if they're still "alive" and don't explicitly unsubscribe themselves, why would you forcibly unsubscribe them? I think it's a pretty rare situation where you know better than the subscriber when it should be unsubscribed...

like if it would allow an earlier garbage collection than if the weak event source were itself just dereferenced

Being subscribed to the WeakEventSource doesn't prevent them from being garbage collected (that's the whole point, after all :wink:)

I'm not saying there can't be any reason to clear all subscribers, but I don't think freeing them sooner is one of them.

DaveInCaz commented 3 years ago

I did have a situation where an event publisher was being removed (replaced, actually) but the subscribers were still alive, so a manual cleanup of those subscriptions seemed necessary. But I agree, this might not be too common of a scenario.

Sorry, my second comment about GC wasn't explained sufficiently - what I meant was whether clearing the subscribers would allow the weak event source itself to be GC'ed more efficiently than if you just dereference it. Sounds like it should not make any difference, but I'm not familiar enough with how the GC deals with the collection of weak references (handles?) that the source would contain to know for sure.

thomaslevesque commented 3 years ago

I did have a situation where an event publisher was being removed (replaced, actually) but the subscribers were still alive, so a manual cleanup of those subscriptions seemed necessary.

But in that case, the subscribers will need to be notified anyway, so that they can subscribe to the new publisher. So they can also unsubscribe themselves from the old publisher. That being said, it's true that it would be a good use case for a WeakEventSource.ClearSubscriptions() method.

what I meant was whether clearing the subscribers would allow the weak event source itself to be GC'ed more efficiently than if you just dereference it

That won't make a difference. The publisher (WeakEventSource, or the object that holds it) references the subscribers, not the other way around. And even if the subscribers happen to reference the publisher, if they only reference each other, they're not reachable, so they can be GC'ed.

thomaslevesque commented 2 years ago

Closing due to inactivity