Skip to content

Commit 4c7a55b

Browse files
authored
IEP-1596 Handle Redundant File Watcher Events for eim_idf.json (#1267)
1 parent 50c1984 commit 4c7a55b

File tree

2 files changed

+41
-28
lines changed

2 files changed

+41
-28
lines changed

bundles/com.espressif.idf.core/src/com/espressif/idf/core/tools/watcher/EimJsonWatchService.java

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,17 @@
55
package com.espressif.idf.core.tools.watcher;
66

77
import java.io.IOException;
8-
import java.nio.file.*;
9-
import java.nio.file.attribute.FileAttribute;
8+
import java.nio.file.ClosedWatchServiceException;
9+
import java.nio.file.FileSystems;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.nio.file.Paths;
13+
import java.nio.file.StandardWatchEventKinds;
14+
import java.nio.file.WatchEvent;
15+
import java.nio.file.WatchKey;
16+
import java.nio.file.WatchService;
17+
import java.time.Instant;
18+
import java.time.temporal.ChronoUnit;
1019
import java.util.List;
1120
import java.util.concurrent.CopyOnWriteArrayList;
1221

@@ -28,8 +37,8 @@ public class EimJsonWatchService extends Thread
2837
private final List<EimJsonChangeListener> eimJsonChangeListeners = new CopyOnWriteArrayList<>();
2938
private volatile boolean running = true;
3039
private volatile boolean paused = false;
31-
32-
40+
private volatile Instant lastModifiedTime;
41+
3342
private EimJsonWatchService() throws IOException
3443
{
3544
String directoryPathString = Platform.getOS().equals(Platform.OS_WIN32) ? EimConstants.EIM_WIN_DIR
@@ -80,38 +89,38 @@ public void addEimJsonChangeListener(EimJsonChangeListener listener)
8089
eimJsonChangeListeners.add(listener);
8190
}
8291
}
83-
92+
8493
public void removeAllListeners()
8594
{
8695
eimJsonChangeListeners.clear();
8796
}
88-
97+
8998
public static void withPausedListeners(Runnable task)
9099
{
91100
EimJsonWatchService watchService = getInstance();
92101
boolean wasPaused = watchService.paused;
93102
watchService.pauseListeners();
94-
103+
95104
try
96105
{
97-
task.run();
106+
task.run();
98107
}
99108
catch (Exception e)
100109
{
101110
Logger.log(e);
102-
}
103-
finally {
111+
} finally
112+
{
104113
if (!wasPaused)
105114
watchService.unpauseListeners();
106115
}
107116
}
108-
117+
109118
public void pauseListeners()
110119
{
111120
Logger.log("Listeners are paused"); //$NON-NLS-1$
112121
paused = true;
113122
}
114-
123+
115124
public void unpauseListeners()
116125
{
117126
Logger.log("Listeners are resumed"); //$NON-NLS-1$
@@ -150,9 +159,26 @@ public void run()
150159
if (context instanceof Path path && path.toString().equals(EimConstants.EIM_JSON))
151160
{
152161
Path fullPath = watchDirectoryPath.resolve(path);
153-
for (EimJsonChangeListener listener : eimJsonChangeListeners)
162+
try
163+
{
164+
Instant currentModified = Files.getLastModifiedTime(fullPath).toInstant()
165+
.truncatedTo(ChronoUnit.SECONDS);
166+
167+
if (lastModifiedTime != null && currentModified.compareTo(lastModifiedTime) <= 0)
168+
{
169+
continue; // skip duplicate or same-second event
170+
}
171+
172+
lastModifiedTime = currentModified;
173+
174+
for (EimJsonChangeListener listener : eimJsonChangeListeners)
175+
{
176+
listener.onJsonFileChanged(fullPath, paused);
177+
}
178+
}
179+
catch (IOException e)
154180
{
155-
listener.onJsonFileChanged(fullPath, paused);
181+
Logger.log(e);
156182
}
157183
}
158184
}

bundles/com.espressif.idf.ui/src/com/espressif/idf/ui/tools/EspressifToolStartup.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import com.espressif.idf.core.tools.EimLoader;
4141
import com.espressif.idf.core.tools.ToolInitializer;
4242
import com.espressif.idf.core.tools.vo.EimJson;
43-
import com.espressif.idf.core.tools.watcher.EimJsonStateChecker;
4443
import com.espressif.idf.core.tools.watcher.EimJsonWatchService;
4544
import com.espressif.idf.core.util.IDFUtil;
4645
import com.espressif.idf.core.util.StringUtil;
@@ -79,9 +78,7 @@ public void earlyStartup()
7978
idfEnvironmentVariables = new IDFEnvironmentVariables();
8079
eimLoader = new EimLoader(new StartupClassDownloadEimDownloadListener(), standardConsoleStream,
8180
errorConsoleStream, Display.getDefault());
82-
EimJsonStateChecker stateChecker = new EimJsonStateChecker(preferences);
8381
eimJsonUiChangeHandler = new EimJsonUiChangeHandler(preferences);
84-
stateChecker.updateLastSeenTimestamp();
8582
EimJsonWatchService.getInstance().addEimJsonChangeListener(eimJsonUiChangeHandler);
8683

8784
if (!toolInitializer.isEimInstalled() && !toolInitializer.isEimIdfJsonPresent())
@@ -121,10 +118,6 @@ else if (toolInitializer.isEimIdfJsonPresent() && !toolInitializer.isEspIdfSet()
121118
toolInitializer.findAndSetEimPath();
122119
}
123120

124-
if (stateChecker.wasModifiedSinceLastRun())
125-
{
126-
showEimJsonStateChangeNotification();
127-
}
128121
}
129122

130123
private boolean checkIfEimPathMacOsIsInApplications()
@@ -217,7 +210,7 @@ private void displayInformationMessageBox(String messageTitle, String message)
217210
}, ignored -> {
218211
});
219212
}
220-
213+
221214
private void closeEspIdfManager()
222215
{
223216
Display.getDefault().asyncExec(() -> {
@@ -244,12 +237,6 @@ private void closeEspIdfManager()
244237
});
245238
}
246239

247-
248-
private void showEimJsonStateChangeNotification()
249-
{
250-
eimJsonUiChangeHandler.displayMessageToUser();
251-
}
252-
253240
private void notifyMissingTools()
254241
{
255242
GlobalModalLock.showModal(() -> MessageDialog.openQuestion(Display.getDefault().getActiveShell(),

0 commit comments

Comments
 (0)