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

feat: Open tab in dev mode less often #19270

Merged
merged 3 commits into from
Apr 29, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,12 @@ public class InitParameters implements Serializable {
* Configuration name for cleaning or leaving frontend files in build.
*/
public static final String CLEAN_BUILD_FRONTEND_FILES = "vaadin.clean.build.frontend.files";

/**
* Configuration name for how long since last browser open before we open a
* new tab for the application in development mode.
*
* Time is given in minutes.
*/
public static final String LAUNCH_BROWSER_DELAY = "launch-browser-delay";
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.vaadin.base.devserver;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vaadin.flow.server.VaadinContext;
import com.vaadin.flow.server.startup.ApplicationConfiguration;
import com.vaadin.open.Open;

import static com.vaadin.flow.server.InitParameters.LAUNCH_BROWSER_DELAY;

/**
* Util for launching a browser instance.
*/
Expand Down Expand Up @@ -52,11 +59,43 @@ private boolean isProductionMode() {
return applicationConfiguration.isProductionMode();
}

private static boolean isLaunched() {
private boolean isLaunched() {
File launchFile = getLaunchFile();
ApplicationConfiguration applicationConfiguration = ApplicationConfiguration
.get(context);
int lastModifiedDelay = Integer.parseInt(applicationConfiguration
.getStringProperty(LAUNCH_BROWSER_DELAY, "30"));
// If launch file exists and is younger than time update file content
// and modified stamp
if (launchFile.exists()
&& TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis()
- launchFile.lastModified()) < lastModifiedDelay) {
writeLaunchFile(launchFile);
return true;
}
return LAUNCHED_VALUE.equals(System.getProperty(LAUNCH_TRACKER));
}

private static void setLaunched() {
private void writeLaunchFile(File launchFile) {
try {
Files.writeString(launchFile.toPath(),
Long.toString(System.currentTimeMillis()));
} catch (IOException e) {
getLogger().debug("Failed to write browser launched file.", e);
}
}

private File getLaunchFile() {
ApplicationConfiguration applicationConfiguration = ApplicationConfiguration
.get(context);
File buildFolder = new File(applicationConfiguration.getProjectFolder(),
applicationConfiguration.getBuildFolder());
return new File(buildFolder, "tab.launch");
}

private void setLaunched() {
// write launch file and update modified timestamp.
writeLaunchFile(getLaunchFile());
System.setProperty(LAUNCH_TRACKER, LAUNCHED_VALUE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ public static List<String> getExcludedUrls(Environment environment) {
*/
private boolean launchBrowser = false;

/**
* Timeout until a new browser should be launched on startup when in
* development mode.
*/
private int launchBrowserDelay = 30;

/**
* URL patterns that should not be handled by the Vaadin servlet when mapped
* to the context root.
Expand Down Expand Up @@ -399,6 +405,30 @@ public void setLaunchBrowser(boolean launchBrowser) {
this.launchBrowser = launchBrowser;
}

/**
* Returns timout after which a browser should be launched again on startup
* when in development mode.
* <p>
*
* @return if a browser should be launched on startup when in development
* mode
*/
public int getLaunchBrowserDelay() {
return launchBrowserDelay;
}

/**
* Sets timout for launching a new browser on startup when in development
* mode.
*
* @param launchBrowser
* {@code true} to launch a browser on startup when in
* development mode, {@code false} otherwise
*/
public void setLaunchBrowserDelay(int launchBrowser) {
this.launchBrowserDelay = launchBrowser;
}

/**
* Returns whether class scan caching between reloads when using Spring Boot
* DevTools should be enabled.
Expand Down
Loading