Skip to content
Draft
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
7 changes: 5 additions & 2 deletions src/main/java/play/dev/filewatch/DefaultFileWatchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

Expand Down Expand Up @@ -37,7 +40,7 @@ public DefaultFileWatchService(LoggerProxy logger, boolean isMac) {
}

@Override
public FileWatcher watch(Iterable<File> filesToWatch, Runnable onChange) {
public FileWatcher watch(Iterable<File> filesToWatch, Consumer<Optional<Path>> onChange) {
var dirsToWatch =
StreamSupport.stream(filesToWatch.spliterator(), false)
.filter(
Expand Down Expand Up @@ -65,7 +68,7 @@ public FileWatcher watch(Iterable<File> filesToWatch, Runnable onChange) {
var directoryWatcher =
DirectoryWatcher.builder()
.paths(dirsToWatch.map(File::toPath).collect(Collectors.toList()))
.listener(__ -> onChange.run())
.listener(dirChangeEvent -> onChange.accept(Optional.of(dirChangeEvent.path())))
.fileHashing(!disableFileHashCheck)
.watchService(watchService)
.build();
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/play/dev/filewatch/FileWatchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
package play.dev.filewatch;

import java.io.File;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.function.Consumer;

/** A service that can watch files */
public interface FileWatchService {
Expand All @@ -18,16 +21,16 @@ public interface FileWatchService {
* @param onChange A callback that is executed whenever something changes.
* @return A watcher
*/
FileWatcher watch(Iterable<File> filesToWatch, Runnable onChange);
FileWatcher watch(Iterable<File> filesToWatch, Consumer<Optional<Path>> onChange);

/**
* @deprecated Use {@link #watch(Iterable, Runnable)} instead
* @deprecated Use {@link #watch(Iterable, Consumer)} instead
*/
@Deprecated(since = "3.0.0", forRemoval = true)
default FileWatcher watch(List<File> filesToWatch, Callable<Void> onChange) {
return watch(
filesToWatch,
() -> {
dirChangeEvent -> {
try {
onChange.call();
} catch (RuntimeException e) {
Expand Down Expand Up @@ -112,7 +115,7 @@ static FileWatchService detect(
return new FileWatchService() {

@Override
public FileWatcher watch(Iterable<File> filesToWatch, Runnable onChange) {
public FileWatcher watch(Iterable<File> filesToWatch, Consumer<Optional<Path>> onChange) {
return delegate.watch(filesToWatch, onChange);
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/play/dev/filewatch/PollingFileWatchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

Expand Down Expand Up @@ -36,7 +38,7 @@ private static Iterable<File> listRecursively(Iterable<File> files) {
}

@Override
public FileWatcher watch(Iterable<File> filesToWatch, Runnable onChange) {
public FileWatcher watch(Iterable<File> filesToWatch, Consumer<Optional<Path>> onChange) {
stopped = false;

var thread =
Expand All @@ -51,7 +53,7 @@ public FileWatcher watch(Iterable<File> filesToWatch, Runnable onChange) {
state,
() -> stopped);
if (result.isTriggered()) {
onChange.run();
onChange.accept(Optional.empty());
}
state = result.getState();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ abstract class FileWatchServiceSpec extends Specification {
}

private def watchFiles[T](files: File*)(block: => T): T = {
val watcher = watchService.watch(files.map(_.toJava).asJava, () => reportChange())
val watcher = watchService.watch(files.map(_.toJava).asJava, maybePath => reportChange())
reset()
try {
block
Expand Down
Loading