Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: trigger refresh from client on hotswap with PUSH #20848

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions flow-server/src/main/java/com/vaadin/flow/hotswap/Hotswapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,11 @@ private void triggerClientUpdate(
LOGGER.debug(
"Triggering re-navigation to current route for UIs affected by classes changes.");
for (UIRefreshStrategy action : uisToRefresh.keySet()) {
uisToRefresh.get(action)
.forEach(ui -> ui.access(() -> ui.refreshCurrentRoute(
action == UIRefreshStrategy.PUSH_REFRESH_CHAIN)));
String triggerEventJS = String.format(
"window.dispatchEvent(new CustomEvent(\"vaadin-refresh-ui\", { detail: { fullRefresh: %s }}));",
action == UIRefreshStrategy.PUSH_REFRESH_CHAIN);
uisToRefresh.get(action).forEach(ui -> ui
.access(() -> ui.getPage().executeJs(triggerEventJS)));
}
}
}
Expand Down Expand Up @@ -507,7 +509,19 @@ public void serviceDestroy(ServiceDestroyEvent event) {

@Override
public void uiInit(UIInitEvent event) {
sessions.add(event.getUI().getSession());
UI ui = event.getUI();
sessions.add(ui.getSession());
ui.getPage().executeJs(
"""
const $wnd = window;
window.addEventListener('vaadin-ui-refresh', (ev) => {
const senderFn = $wnd.Vaadin?.Flow?.clients[$0]?.sendEventMessage;
if (senderFn) {
senderFn(1, "ui-refresh", ev.detail);
}
});
""",
ui.getInternals().getAppId());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Assert;
import org.junit.Before;
Expand All @@ -35,6 +37,7 @@
import com.vaadin.flow.component.HasComponents;
import com.vaadin.flow.component.HasElement;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.page.Page;
import com.vaadin.flow.di.Lookup;
import com.vaadin.flow.internal.BrowserLiveReload;
import com.vaadin.flow.internal.BrowserLiveReloadAccessor;
Expand Down Expand Up @@ -1153,20 +1156,30 @@ private RefreshTestingUI initUIAndNavigateTo(VaadinSession session,

private static class RefreshTestingUI extends MockUI {

private static final Pattern UI_REFRESH_EVENT = Pattern.compile(
".*new CustomEvent\\(\"vaadin-refresh-ui\",\\s*\\{\\s*detail:\\s*\\{\\s*fullRefresh:\\s*(true|false)\\s*}\\s*}\\).*");
private Boolean refreshRouteChainRequested;

private final Page pageSpy;

public RefreshTestingUI(VaadinSession session) {
super(session);
pageSpy = Mockito.spy(super.getPage());
// Intercept javascript executions to check if the custom ui refresh
// event dispatch has been registered.
Mockito.doAnswer(i -> {
Matcher matcher = UI_REFRESH_EVENT.matcher(i.getArgument(0));
if (matcher.matches()) {
refreshRouteChainRequested = Boolean
.parseBoolean(matcher.group(1));
}
return null;
}).when(pageSpy).executeJs(Mockito.anyString());
}

@Override
public void refreshCurrentRoute(boolean refreshRouteChain) {
refreshRouteChainRequested = refreshRouteChain;
// No need to perform real navigation, tests only need to know if
// the method has been invoked.
// Navigation would fail anyway because of usage of method scoped
// classes. Blocking navigation prevents logs to be bloated by
// exception stack traces.
public Page getPage() {
return pageSpy;
}

void assertNotRefreshed() {
Expand Down
Loading