Discussion:
Passing method references to HTMLElement.removeEventHandler
David Nouls
2018-10-12 12:05:13 UTC
Permalink
I think I found a little bug in the java 8 support in GWT. Or at least, it does not work as I expected it would.
I am using Elemental2 and I am trying to remove an EventListener from a HTMLElement.

The listener was previously installed like this:

element.addEventListener(“click”, this::handleClick)

when I remove the listener, I also use the same method reference on the same object.
element.removeEventListener(“click”, this::handleClick)

but in reality the click handler is not removed.

I was expecting that this method reference would always yield the same JS object, but it does not seem that way.
Is that intentional or a bug or am I missing something ?
--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+***@googlegroups.com.
To post to this group, send email to google-web-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.
Jens
2018-10-12 13:40:44 UTC
Permalink
Method references and lambdas are anonymous implementations. Each usage
will create a new instance.

Even in pure Java you have

Runnable r = this::onClick;
Runnable r2 = this::onClick;
System.out.println(Objects.equals(r, r2)); // false


So you should keep track of your click handler if you want to remove it
later.

-- J.
--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+***@googlegroups.com.
To post to this group, send email to google-web-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.
David Nouls
2018-10-12 14:14:54 UTC
Permalink
It makes sense somehow, but it took me by surprise. Thanks for the explanation.

Is it still good practice to remove listeners when you don’t need the html element anymore ? or do modern browser garbage collectors cope with circular references between DOM and JS objects ? It was one of the features of Widgets that they avoided the leaks automatically.
Method references and lambdas are anonymous implementations. Each usage will create a new instance.
Even in pure Java you have
Runnable r = this::onClick;
Runnable r2 = this::onClick;
System.out.println(Objects.equals(r, r2)); // false
So you should keep track of your click handler if you want to remove it later.
-- J.
--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+***@googlegroups.com.
To post to this group, send email to google-web-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.
Jens
2018-10-12 16:56:44 UTC
Permalink
Post by David Nouls
Is it still good practice to remove listeners when you don’t need the html
element anymore ? or do modern browser garbage collectors cope with
circular references between DOM and JS objects ? It was one of the features
of Widgets that they avoided the leaks automatically.
All recent browsers use garbage collection implementations that can deal
with cyclic references. So you don't have to remove event listeners when
you remove an element from DOM and do not reference it otherwise.

If you hold a strong reference to an element that has been removed from DOM
(e.g. you have an element cache, or some element lookup table somewhere)
then you might want to remove the handler to make sure all code reachable
from that handler can be garbage collected if you do not need/want to reuse
that explicit handler anymore.

Also keep in mind that DOM is a tree and that child nodes have a reference
to their parent. So if you hold a reference to a DOM node and then you
remove a parent of that DOM node, thus removing a whole sub tree containing
your referenced DOM node, then the whole subtree starting from the removed
parent will be kept in memory (and not just your referenced DOM node).
Typical example would be you hold a reference to a TD element and later
remove the whole corresponding TABLE element. Then the TABLE still exists
in memory as long as you hold a reference to the TD element and do not
append that TD element to a different element.

-- J.
--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+***@googlegroups.com.
To post to this group, send email to google-web-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.
Loading...