From f265739578783ca08b7829ba030a38574b3ed4a7 Mon Sep 17 00:00:00 2001 From: Ahmed Ashour Date: Wed, 4 Oct 2017 09:35:19 +0200 Subject: Registration to remove state.registeredEventListeners (fixes #9634) (#10130) This changes SharedState.registeredEventListeners to be a Map that keeps track of how many listeners of each type have been added, and handles unregistering of such listeners correctly. --- .../vaadin/shared/communication/SharedState.java | 9 ++++--- .../com/vaadin/shared/ui/ComponentStateUtil.java | 29 +++++++++++++++------- 2 files changed, 26 insertions(+), 12 deletions(-) (limited to 'shared/src') diff --git a/shared/src/main/java/com/vaadin/shared/communication/SharedState.java b/shared/src/main/java/com/vaadin/shared/communication/SharedState.java index 6b9c1045c5..57ad67a0d7 100644 --- a/shared/src/main/java/com/vaadin/shared/communication/SharedState.java +++ b/shared/src/main/java/com/vaadin/shared/communication/SharedState.java @@ -19,7 +19,6 @@ package com.vaadin.shared.communication; import java.io.Serializable; import java.util.HashMap; import java.util.Map; -import java.util.Set; import com.vaadin.shared.Connector; import com.vaadin.shared.annotations.NoLayout; @@ -62,10 +61,14 @@ public class SharedState implements Serializable { public Map resources = new HashMap<>(); public boolean enabled = true; + /** - * A set of event identifiers with registered listeners. + * A Map of event identifiers with registered listeners, {@code key} is + * event identifier, {@code value} is the listeners count. + * + * @since */ @NoLayout - public Set registeredEventListeners = null; + public Map registeredEventListeners; } diff --git a/shared/src/main/java/com/vaadin/shared/ui/ComponentStateUtil.java b/shared/src/main/java/com/vaadin/shared/ui/ComponentStateUtil.java index 0ef247353b..a2f6618b9e 100644 --- a/shared/src/main/java/com/vaadin/shared/ui/ComponentStateUtil.java +++ b/shared/src/main/java/com/vaadin/shared/ui/ComponentStateUtil.java @@ -16,7 +16,7 @@ package com.vaadin.shared.ui; import java.io.Serializable; -import java.util.HashSet; +import java.util.HashMap; import com.vaadin.shared.AbstractComponentState; import com.vaadin.shared.Registration; @@ -67,12 +67,19 @@ public final class ComponentStateUtil implements Serializable { @Deprecated public static final void removeRegisteredEventListener(SharedState state, String eventIdentifier) { - if (state.registeredEventListeners == null) { - return; - } - state.registeredEventListeners.remove(eventIdentifier); - if (state.registeredEventListeners.size() == 0) { - state.registeredEventListeners = null; + if (state.registeredEventListeners != null) { + Integer count = state.registeredEventListeners.get(eventIdentifier); + if (count != null) { + if (count > 1) { + state.registeredEventListeners.put(eventIdentifier, + count - 1); + } else { + state.registeredEventListeners.remove(eventIdentifier); + if (state.registeredEventListeners.isEmpty()) { + state.registeredEventListeners = null; + } + } + } } } @@ -87,9 +94,13 @@ public final class ComponentStateUtil implements Serializable { public static final Registration addRegisteredEventListener( SharedState state, String eventListenerId) { if (state.registeredEventListeners == null) { - state.registeredEventListeners = new HashSet<>(); + state.registeredEventListeners = new HashMap<>(); + } + Integer count = state.registeredEventListeners.get(eventListenerId); + if (count == null) { + count = 0; } - state.registeredEventListeners.add(eventListenerId); + state.registeredEventListeners.put(eventListenerId, count + 1); return () -> removeRegisteredEventListener(state, eventListenerId); } } -- cgit v1.2.3