Search in sources :

Example 6 with Registration

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)));
}
Also used : StreamRegistration(com.vaadin.flow.server.StreamRegistration) Registration(com.vaadin.flow.shared.Registration) StreamRegistration(com.vaadin.flow.server.StreamRegistration)

Example 7 with Registration

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;
    }
}
Also used : StreamRegistration(com.vaadin.flow.server.StreamRegistration) Registration(com.vaadin.flow.shared.Registration) StreamRegistration(com.vaadin.flow.server.StreamRegistration)

Example 8 with Registration

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());
}
Also used : TestComponent(com.vaadin.flow.component.ComponentTest.TestComponent) Registration(com.vaadin.flow.shared.Registration) JsonObject(elemental.json.JsonObject) Test(org.junit.Test)

Example 9 with Registration

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);
}
Also used : Registration(com.vaadin.flow.shared.Registration) Test(org.junit.Test)

Example 10 with Registration

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;
}
Also used : Registration(com.vaadin.flow.shared.Registration) Element(com.vaadin.flow.dom.Element)

Aggregations

Registration (com.vaadin.flow.shared.Registration)25 Test (org.junit.Test)20 UI (com.vaadin.flow.component.UI)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 TestComponent (com.vaadin.flow.component.ComponentTest.TestComponent)4 Element (com.vaadin.flow.dom.Element)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 StreamRegistration (com.vaadin.flow.server.StreamRegistration)3 JsonObject (elemental.json.JsonObject)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 BootstrapContext (com.vaadin.flow.server.BootstrapHandler.BootstrapContext)2 Set (java.util.Set)2 Assert (org.junit.Assert)2 Component (com.vaadin.flow.component.Component)1 Html (com.vaadin.flow.component.Html)1 Tag (com.vaadin.flow.component.Tag)1 Text (com.vaadin.flow.component.Text)1 HtmlImport (com.vaadin.flow.component.dependency.HtmlImport)1 JavaScript (com.vaadin.flow.component.dependency.JavaScript)1 StyleSheet (com.vaadin.flow.component.dependency.StyleSheet)1