On 01. 09. 2020. 10:42, Jens wrote:
>
> But what I don't get is why you don't want to explicitly clean things up? If
> you rely on WeakRef or anything similar you depend on garbage collection,
> which isn't really great since you never know when GC will happen and
> behavior is different between browsers. So if you have an EventBus that uses
> some weak references it is likely designed as "if nobody has a strong
> reference to a registered event handler, then remove that event handler from
> the event bus". The problem is that you can not control GC and as long as GC
> does not run, these handlers will still get events, regardless if your app
> still uses these classes or not. So you have overhead until the browser
> decides to run GC and clean these weak references.
Exactly what I would like to have, for the same reason I have garbage
collector: opportunity to have less (fragile) code.
Explicit resource deallocation too often leads to resource leaks. Maybe I'm
(and the rest of the team) sloppy, I don't know, but it's simply my
experience. What you described is exactly what we are currently doing, but
in big enough project it becomes more and more difficult to trace lifetime
of objects.
Having weak subscriptions to event bus would require adopting some new
patterns (like just "invalidating" subscriber upon event and do actual
compute in onAttach or some other point in time when it's acknowledged that
results from caller are actually needed), but from my current experience I'm
confident enough it would result in more robust code base.
I'm not saying that I might not be wrong, but I would like to try.
> I would try to refactor code to make explicit event bus registration and
> deregistration easier and less error prone. For example take GWT Activities.
> The EventBus you get in its start() method is automatically cleaned up as
> soon as the activity stops. I am pretty sure there are options to build some
> classes around your existing ones to better deal with EventBus.
We tried that, too, didn't like that background activities are cut of from
event bus and also propagating ResettableEventBus instances to every single
one of widgets used in that activity results in lots of boilerplate.
-gkresic.