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

Add component-level API for customizing DOM event listeners #4445

Merged
merged 3 commits into from
Aug 2, 2018

Conversation

pekam
Copy link
Contributor

@pekam pekam commented Jul 27, 2018

Each component-level event-listener is now mapped to its corresponding
DOM event listener registration.

An overload of ComponentEventBus::addListener is added which takes a
consumer for customizing the DOM event listener. The method still
returns a component-level registration.

This allows creating Component-level API for eg. overriding the debounce
or filter settings defined in the event's @DomEvent annotation.


This change is Reviewable

Each component-level event-listener is now mapped to its corresponding
DOM event listener registration.

An overload of ComponentEventBus::addListener is added which takes a
consumer for customizing the DOM event listener. The method still
returns a component-level registration.

This allows creating Component-level API for eg. overriding the debounce
or filter settings defined in the event's @DomEvent annotation.
@pekam pekam self-assigned this Jul 27, 2018
Copy link
Contributor

@gilberto-torrezan gilberto-torrezan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 5 of 5 files at r1.
Reviewable status: 4 unresolved discussions, 0 of 1 LGTMs obtained


flow-server/src/main/java/com/vaadin/flow/component/ComponentEventBus.java, line 148 at r1 (raw file):

            Class<? extends ComponentEvent<?>> eventType) {
        boolean alreadyRegistered = hasListener(eventType);
        if (alreadyRegistered) {

What happens now if the same eventType with the same listener is registered more than once?
(Answer that with a comment in the javadocs)


flow-server/src/main/java/com/vaadin/flow/component/ComponentEventBus.java, line 335 at r1 (raw file):

        }

        if (!eventData.containsKey(listener)) {

Is it possible to have the listener registered in the eventData (so the eventData.containsKey(listener) would return true) but the domEventRemover (from eventData.get(listener)) is null ?

If not, then there's one optimization that could be done here:

Registration domEventRemover = eventData.remove(listener);
if (domEventRemover == null) {
    throw new IllegalArgumentException("The given listener is not registered");
}
domEventRemover.remove();

Copy link
Contributor Author

@pekam pekam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 4 unresolved discussions, 0 of 1 LGTMs obtained


flow-server/src/main/java/com/vaadin/flow/component/ComponentEventBus.java, line 148 at r1 (raw file):

Previously, gilberto-torrezan (Gilberto Torrezan) wrote…

What happens now if the same eventType with the same listener is registered more than once?
(Answer that with a comment in the javadocs)

Currently adding the same listener twice causes overriding the DomListenerRegistration in the ComponentEventData map, but the first dom-listener never gets removed. So even after component-level Registration::remove, it only removes the latest dom-registration that is saved in the map. The old one is not in the map so it remains registered for ever.

To correctly support adding same listener multiple times with separate Registrations, I need to change the data-structure from a map to some kind of a list of pairs, because a map can't have duplicate keys.

This was a good point. I think this PR kinda broke this use-case, as there was no test for it.

Copy link
Contributor Author

@pekam pekam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 4 unresolved discussions, 0 of 1 LGTMs obtained


flow-server/src/main/java/com/vaadin/flow/component/ComponentEventBus.java, line 148 at r1 (raw file):

Previously, pekam (Pekka Maanpää) wrote…

Currently adding the same listener twice causes overriding the DomListenerRegistration in the ComponentEventData map, but the first dom-listener never gets removed. So even after component-level Registration::remove, it only removes the latest dom-registration that is saved in the map. The old one is not in the map so it remains registered for ever.

To correctly support adding same listener multiple times with separate Registrations, I need to change the data-structure from a map to some kind of a list of pairs, because a map can't have duplicate keys.

This was a good point. I think this PR kinda broke this use-case, as there was no test for it.

Done.


flow-server/src/main/java/com/vaadin/flow/component/ComponentEventBus.java, line 335 at r1 (raw file):

Previously, gilberto-torrezan (Gilberto Torrezan) wrote…

Is it possible to have the listener registered in the eventData (so the eventData.containsKey(listener) would return true) but the domEventRemover (from eventData.get(listener)) is null ?

If not, then there's one optimization that could be done here:

Registration domEventRemover = eventData.remove(listener);
if (domEventRemover == null) {
    throw new IllegalArgumentException("The given listener is not registered");
}
domEventRemover.remove();

The data structure is a bit different but there was a similar optimization to be made.

private <T extends ComponentEvent<?>> void fireEventForListener(T event,
ListenerWrapper<T> wrapper) {
Class<T> eventType = (Class<T>) event.getClass();
event.setUnregisterListenerCommand(() -> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Remove useless curly braces around statement rule

@vaadin-bot
Copy link
Collaborator

SonarQube analysis reported 1 issue

  • MINOR 1 minor

Watch the comments in this conversation to review them.

Copy link
Contributor

@gilberto-torrezan gilberto-torrezan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 2 of 2 files at r2.
Reviewable status: 1 unresolved discussion, 0 of 1 LGTMs obtained, and 1 stale

Copy link
Contributor

@gilberto-torrezan gilberto-torrezan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dismissed @vaadin-bot from a discussion.
Reviewable status: :shipit: complete! all discussions resolved, 1 of 1 LGTMs obtained

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

Successfully merging this pull request may close these issues.

3 participants