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());
}
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());
}
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();
}
}
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();
};
}
Aggregations