-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from olafurpg/watch
Polish file watching
- Loading branch information
Showing
6 changed files
with
106 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
vork/src/main/scala/vork/internal/io/VorkFileListener.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package vork.internal.io | ||
|
||
import io.methvin.watcher.DirectoryChangeEvent | ||
import io.methvin.watcher.DirectoryChangeEvent.EventType | ||
import io.methvin.watcher.DirectoryChangeListener | ||
import io.methvin.watcher.DirectoryWatcher | ||
import java.io.InputStream | ||
import java.nio.file.Files | ||
import java.util.Scanner | ||
import java.util.concurrent.Executor | ||
import java.util.concurrent.ExecutorService | ||
import scala.meta.io.AbsolutePath | ||
import scala.collection.JavaConverters._ | ||
|
||
final class VorkFileListener( | ||
executor: ExecutorService, | ||
in: InputStream, | ||
runAction: DirectoryChangeEvent => Unit | ||
) extends DirectoryChangeListener { | ||
private var myIsWatching: Boolean = true | ||
private var watcher: DirectoryWatcher = _ | ||
private def blockUntilEnterKey(): Unit = { | ||
try { | ||
new Scanner(in).nextLine() | ||
} catch { | ||
case _: NoSuchElementException => | ||
} | ||
} | ||
def watchUntilInterrupted(): Unit = { | ||
watcher.watchAsync(executor) | ||
blockUntilEnterKey() | ||
executor.shutdown() | ||
myIsWatching = false | ||
watcher.close() | ||
} | ||
override def isWatching: Boolean = myIsWatching | ||
override def onEvent(event: DirectoryChangeEvent): Unit = { | ||
val targetFile = event.path() | ||
if (Files.isRegularFile(targetFile)) { | ||
event.eventType() match { | ||
case EventType.CREATE => runAction(event) | ||
case EventType.MODIFY => runAction(event) | ||
case EventType.OVERFLOW => runAction(event) | ||
case EventType.DELETE => () // We don't do anything when a file is deleted | ||
} | ||
} | ||
} | ||
} | ||
|
||
object VorkFileListener { | ||
def create(dir: AbsolutePath, executor: ExecutorService, in: InputStream)( | ||
runAction: DirectoryChangeEvent => Unit | ||
): VorkFileListener = { | ||
val listener = new VorkFileListener(executor, in, runAction) | ||
val watcher = DirectoryWatcher.create(List(dir.toNIO).asJava, listener, true) | ||
listener.watcher = watcher | ||
listener | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters