Search in sources :

Example 1 with RoutesChangedEvent

use of com.vaadin.flow.router.RoutesChangedEvent in project flow by vaadin.

the class AbstractRouteRegistryTest method removeChangeListener_noEventsAreFired.

@Test
public void removeChangeListener_noEventsAreFired() {
    List<RoutesChangedEvent> events = new ArrayList<>();
    Registration registration = registry.addRoutesChangeListener(events::add);
    registry.setRoute("home", MyRoute.class, Collections.emptyList());
    Assert.assertEquals("Event should have been fired for listener", 1, events.size());
    registration.remove();
    registry.setRoute("away", MyRoute.class, Collections.emptyList());
    Assert.assertEquals("No new event should have fired", 1, events.size());
}
Also used : Registration(com.vaadin.flow.shared.Registration) ArrayList(java.util.ArrayList) RoutesChangedEvent(com.vaadin.flow.router.RoutesChangedEvent) Test(org.junit.Test)

Example 2 with RoutesChangedEvent

use of com.vaadin.flow.router.RoutesChangedEvent in project flow by vaadin.

the class SessionRouteRegistryTest method removeListener_noEventsAreGottenForAnyRegistry.

@Test
public void removeListener_noEventsAreGottenForAnyRegistry() {
    SessionRouteRegistry sessionRegistry = getRegistry(session);
    List<RoutesChangedEvent> events = new ArrayList<>();
    Registration registration = sessionRegistry.addRoutesChangeListener(events::add);
    registry.setRoute("main", MyRoute.class, Collections.emptyList());
    sessionRegistry.update(() -> {
        sessionRegistry.setRoute("main", Secondary.class, Collections.emptyList());
        sessionRegistry.setRoute("Alias1", Secondary.class, Collections.emptyList());
        sessionRegistry.setRoute("Alias2", Secondary.class, Collections.emptyList());
    });
    Assert.assertEquals("One event for both registries should have been fired.", 2, events.size());
    registration.remove();
    sessionRegistry.removeRoute("main");
    Assert.assertEquals("No new event should have been received for session scope", 2, events.size());
    registry.removeRoute("main");
    Assert.assertEquals("No new event should have been received for application scope", 2, events.size());
}
Also used : Registration(com.vaadin.flow.shared.Registration) ArrayList(java.util.ArrayList) RoutesChangedEvent(com.vaadin.flow.router.RoutesChangedEvent) Test(org.junit.Test)

Example 3 with RoutesChangedEvent

use of com.vaadin.flow.router.RoutesChangedEvent in project flow by vaadin.

the class AbstractRouteRegistry method unlock.

private void unlock() {
    if (configurationLock.getHoldCount() == 1 && editing != null) {
        try {
            ConfiguredRoutes oldConfiguration = configuredRoutes;
            configuredRoutes = new ConfiguredRoutes(editing);
            if (!routesChangedListeners.isEmpty()) {
                List<RouteBaseData<?>> oldRoutes = flattenRoutes(getRegisteredRoutes(oldConfiguration));
                List<RouteBaseData<?>> newRoutes = flattenRoutes(getRegisteredRoutes(configuredRoutes));
                List<RouteBaseData<?>> added = new ArrayList<>();
                List<RouteBaseData<?>> removed = new ArrayList<>();
                oldRoutes.stream().filter(route -> !newRoutes.contains(route)).forEach(removed::add);
                newRoutes.stream().filter(route -> !oldRoutes.contains(route)).forEach(added::add);
                fireEvent(new RoutesChangedEvent(this, added, removed));
            }
        } finally {
            editing = null;
            configurationLock.unlock();
        }
    } else {
        configurationLock.unlock();
    }
}
Also used : RouteAliasData(com.vaadin.flow.router.RouteAliasData) Component(com.vaadin.flow.component.Component) SerializableBiConsumer(com.vaadin.flow.function.SerializableBiConsumer) Registration(com.vaadin.flow.shared.Registration) ArrayList(java.util.ArrayList) Map(java.util.Map) Command(com.vaadin.flow.server.Command) HasErrorParameter(com.vaadin.flow.router.HasErrorParameter) NotFoundException(com.vaadin.flow.router.NotFoundException) RouterLayout(com.vaadin.flow.router.RouterLayout) ReentrantLock(java.util.concurrent.locks.ReentrantLock) RouteData(com.vaadin.flow.router.RouteData) RouteRegistry(com.vaadin.flow.server.RouteRegistry) ReflectTools(com.vaadin.flow.internal.ReflectTools) RoutesChangedListener(com.vaadin.flow.router.RoutesChangedListener) Serializable(java.io.Serializable) InvalidRouteConfigurationException(com.vaadin.flow.server.InvalidRouteConfigurationException) Objects(java.util.Objects) List(java.util.List) Optional(java.util.Optional) RoutesChangedEvent(com.vaadin.flow.router.RoutesChangedEvent) RouteBaseData(com.vaadin.flow.router.RouteBaseData) Collections(java.util.Collections) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RouteParameters(com.vaadin.flow.router.RouteParameters) RouteBaseData(com.vaadin.flow.router.RouteBaseData) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RoutesChangedEvent(com.vaadin.flow.router.RoutesChangedEvent)

Example 4 with RoutesChangedEvent

use of com.vaadin.flow.router.RoutesChangedEvent in project flow by vaadin.

the class SessionRouteRegistry method addRoutesChangeListener.

/**
 * Adds the given route change listener to the registry.
 * <p>
 * For the session scoped registry also changes to the application scoped
 * registry will be delegated to the listener if the added or removed route
 * was not masked by a registration in the session scope.
 *
 * @param listener
 *            listener to add
 * @return registration to remove the listener
 */
@Override
public Registration addRoutesChangeListener(RoutesChangedListener listener) {
    final Registration parentRegistration = getParentRegistry().addRoutesChangeListener(event -> {
        ConfiguredRoutes configuration = getConfiguration();
        List<RouteBaseData<?>> addedVisible = event.getAddedRoutes().stream().filter(routeData -> !configuration.hasTemplate(routeData.getTemplate())).collect(Collectors.toList());
        List<RouteBaseData<?>> removedVisible = event.getRemovedRoutes().stream().filter(routeData -> !configuration.hasTemplate(routeData.getTemplate())).collect(Collectors.toList());
        // Only fire an event if we have visible changes.
        if (!(addedVisible.isEmpty() && removedVisible.isEmpty())) {
            fireEvent(new RoutesChangedEvent(event.getSource(), addedVisible, removedVisible));
        }
    });
    final Registration registration = super.addRoutesChangeListener(listener);
    return () -> {
        registration.remove();
        parentRegistration.remove();
    };
}
Also used : RouterLayout(com.vaadin.flow.router.RouterLayout) Component(com.vaadin.flow.component.Component) RouteData(com.vaadin.flow.router.RouteData) AbstractRouteRegistry(com.vaadin.flow.router.internal.AbstractRouteRegistry) Registration(com.vaadin.flow.shared.Registration) Set(java.util.Set) RoutesChangedListener(com.vaadin.flow.router.RoutesChangedListener) Collectors(java.util.stream.Collectors) ConfiguredRoutes(com.vaadin.flow.router.internal.ConfiguredRoutes) ArrayList(java.util.ArrayList) Objects(java.util.Objects) List(java.util.List) NavigationRouteTarget(com.vaadin.flow.router.internal.NavigationRouteTarget) Optional(java.util.Optional) RoutesChangedEvent(com.vaadin.flow.router.RoutesChangedEvent) PathUtil(com.vaadin.flow.router.internal.PathUtil) RouteBaseData(com.vaadin.flow.router.RouteBaseData) RouteTarget(com.vaadin.flow.router.internal.RouteTarget) RouteParameters(com.vaadin.flow.router.RouteParameters) Registration(com.vaadin.flow.shared.Registration) RouteBaseData(com.vaadin.flow.router.RouteBaseData) ConfiguredRoutes(com.vaadin.flow.router.internal.ConfiguredRoutes) RoutesChangedEvent(com.vaadin.flow.router.RoutesChangedEvent)

Aggregations

RoutesChangedEvent (com.vaadin.flow.router.RoutesChangedEvent)4 Registration (com.vaadin.flow.shared.Registration)4 ArrayList (java.util.ArrayList)4 Component (com.vaadin.flow.component.Component)2 RouteBaseData (com.vaadin.flow.router.RouteBaseData)2 RouteData (com.vaadin.flow.router.RouteData)2 RouteParameters (com.vaadin.flow.router.RouteParameters)2 RouterLayout (com.vaadin.flow.router.RouterLayout)2 RoutesChangedListener (com.vaadin.flow.router.RoutesChangedListener)2 List (java.util.List)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 Test (org.junit.Test)2 SerializableBiConsumer (com.vaadin.flow.function.SerializableBiConsumer)1 ReflectTools (com.vaadin.flow.internal.ReflectTools)1 HasErrorParameter (com.vaadin.flow.router.HasErrorParameter)1 NotFoundException (com.vaadin.flow.router.NotFoundException)1 RouteAliasData (com.vaadin.flow.router.RouteAliasData)1 AbstractRouteRegistry (com.vaadin.flow.router.internal.AbstractRouteRegistry)1 ConfiguredRoutes (com.vaadin.flow.router.internal.ConfiguredRoutes)1