use of com.vaadin.flow.shared.Registration in project flow by vaadin.
the class ElementAttributeMap method registerResource.
private void registerResource(String attribute, AbstractStreamResource resource) {
ensureResourceRegistrations();
ensurePendingRegistrations();
assert !resourceRegistrations.containsKey(attribute);
StreamRegistration registration = getSession().getResourceRegistry().registerResource(resource);
resourceRegistrations.put(attribute, registration);
Registration handle = pendingRegistrations.remove(attribute);
if (handle != null) {
handle.remove();
}
pendingRegistrations.put(attribute, getNode().addDetachListener(() -> unsetResource(attribute)));
}
use of com.vaadin.flow.shared.Registration in project flow by vaadin.
the class ElementAttributeMap method unregisterResource.
private void unregisterResource(String attribute) {
ensureResourceRegistrations();
ensurePendingRegistrations();
StreamRegistration registration = resourceRegistrations.remove(attribute);
Registration handle = pendingRegistrations.remove(attribute);
if (handle != null) {
handle.remove();
}
if (registration != null) {
registration.unregister();
}
if (resourceRegistrations.isEmpty()) {
resourceRegistrations = null;
}
if (pendingRegistrations.isEmpty()) {
pendingRegistrations = null;
}
}
use of com.vaadin.flow.shared.Registration in project flow by vaadin.
the class ComponentEventBusTest method domEvent_removeListener.
@Test
public void domEvent_removeListener() {
TestComponent component = new TestComponent();
EventTracker<MappedToDomEvent> eventTracker = new EventTracker<>();
Registration remover = component.addListener(MappedToDomEvent.class, eventTracker);
remover.remove();
JsonObject eventData = createData("event.someData", 42, "event.moreData", 1);
fireDomEvent(component, "dom-event", eventData);
eventTracker.assertEventNotCalled();
assertNoListeners(component.getEventBus());
}
use of com.vaadin.flow.shared.Registration in project flow by vaadin.
the class ComponentTest method testAttachDetachListeners_parentAttachDetach_childListenersTriggered.
@Test
public void testAttachDetachListeners_parentAttachDetach_childListenersTriggered() {
UI ui = new UI();
TestComponentContainer parent = new TestComponentContainer();
TestComponentContainer child = new TestComponentContainer();
TestComponent grandChild = new TestComponent();
child.track();
Registration attachRegistrationHandle = grandChild.addAttachListener(event -> grandChild.getAttachEvents().incrementAndGet());
Registration detachRegistrationHandle = grandChild.addDetachListener(event -> grandChild.getDetachEvents().incrementAndGet());
parent.add(child);
child.add(grandChild);
child.assertAttachEvents(0);
grandChild.assertAttachEvents(0);
ui.add(parent);
child.assertAttachEvents(1);
grandChild.assertAttachEvents(1);
child.assertDetachEvents(0);
grandChild.assertDetachEvents(0);
ui.remove(parent);
parent.remove(child);
child.assertAttachEvents(1);
grandChild.assertAttachEvents(1);
child.assertDetachEvents(1);
grandChild.assertDetachEvents(1);
ui.add(parent);
parent.add(child);
child.assertAttachEvents(2);
grandChild.assertAttachEvents(2);
child.assertDetachEvents(1);
grandChild.assertDetachEvents(1);
ui.remove(parent);
child.assertAttachEvents(2);
grandChild.assertAttachEvents(2);
child.assertDetachEvents(2);
grandChild.assertDetachEvents(2);
attachRegistrationHandle.remove();
detachRegistrationHandle.remove();
ui.add(child);
child.assertAttachEvents(3);
grandChild.assertAttachEvents(2);
ui.remove(child);
child.assertDetachEvents(3);
grandChild.assertDetachEvents(2);
}
use of com.vaadin.flow.shared.Registration in project flow by vaadin.
the class ComponentEventBus method addDomTrigger.
/**
* Adds a DOM listener of the given type for the given component event.
* <p>
* Assumes that no listener exists.
*
* @param eventType
* the component event type
* @param domEventType
* the DOM event type
*/
private void addDomTrigger(Class<? extends ComponentEvent<?>> eventType, String domEventType) {
assert eventType != null;
assert !componentEventData.containsKey(eventType) || componentEventData.get(eventType).domEventRemover == null;
if (domEventType == null || domEventType.isEmpty()) {
throw new IllegalArgumentException("The DOM event type cannot be null or empty");
}
Element element = component.getElement();
// Register DOM event handler
LinkedHashMap<String, Class<?>> eventDataExpressions = ComponentEventBusUtil.getEventDataExpressions(eventType);
String[] eventData = new String[eventDataExpressions.size()];
eventDataExpressions.keySet().toArray(eventData);
// This needs to be an anonymous class and not a lambda because of
// https://github.com/vaadin/flow/issues/575
Registration remover = element.addEventListener(domEventType, event -> handleDomEvent(eventType, event), eventData);
componentEventData.computeIfAbsent(eventType, t -> new ComponentEventData()).domEventRemover = remover;
}
Aggregations