Skip to content

Commit

Permalink
feat: Open tab in dev mode less often
Browse files Browse the repository at this point in the history
As we can not know if a tab
for the application is open
we add a file that is checked
for modification time and after
default 30 min we open a new tab on
server restart.

If server is restarted again before
timeout file timestamp is updated.

Clearing build folder will also
cause new tab to open on next restart.

Closes #18393
  • Loading branch information
caalador committed Apr 26, 2024
1 parent b07ac8e commit dc500e0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
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,46 @@ 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) {
try {
Files.writeString(launchFile.toPath(),
"" + System.currentTimeMillis());
} catch (IOException e) {
LoggerFactory.getLogger(BrowserLauncher.class)
.debug("Failed to write browser launched file.", e);
}
return true;
}
return LAUNCHED_VALUE.equals(System.getProperty(LAUNCH_TRACKER));
}

private static void setLaunched() {
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.
try {
Files.writeString(getLaunchFile().toPath(),
"" + System.currentTimeMillis());
} catch (IOException e) {
LoggerFactory.getLogger(BrowserLauncher.class)
.debug("Failed to write browser launched file.", e);
}
System.setProperty(LAUNCH_TRACKER, LAUNCHED_VALUE);
}

Expand Down

0 comments on commit dc500e0

Please sign in to comment.