Skip to content

Commit

Permalink
improve project watcher...
Browse files Browse the repository at this point in the history
  • Loading branch information
astinz committed Jul 17, 2023
1 parent 3cbf76f commit f8e64cb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package xyz.mcxross.cohesive.daemon

import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import xyz.mcxross.cohesive.utils.Log
import java.nio.file.*
import java.nio.file.FileSystems
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.StandardWatchEventKinds
import java.nio.file.WatchKey

class DirectoryListener(private val directoryPath: String) {
private var running: Boolean = false
Expand Down Expand Up @@ -30,44 +40,62 @@ class DirectoryListener(private val directoryPath: String) {
throw IllegalArgumentException("Invalid directory path: $directoryPath")
}

running = true

Log.d { "Directory listener started. Listening for new files in $directoryPath and child dir(s)" }

watchDirectory(directory)

}

@OptIn(DelicateCoroutinesApi::class)
private fun watchDirectory(directory: Path) {
val watchService = FileSystems.getDefault().newWatchService()
directory.register(
watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY,
StandardWatchEventKinds.ENTRY_DELETE,
)

running = true

Log.d { "Directory listener started. Listening for new files in $directoryPath" }

while (running) {
val key: WatchKey = watchService.take()
val subDirs = Files.newDirectoryStream(directory) {
Files.isDirectory(it)
}

for (event in key.pollEvents()) {
val kind = event.kind()
val fileName = event.context() as Path
subDirs.forEach {
GlobalScope.launch {
watchDirectory(it)

when (kind) {
StandardWatchEventKinds.ENTRY_CREATE -> {
val filePath = directory.resolve(fileName)
onCreateCallback?.invoke(filePath.toString())
while (running) {
val key: WatchKey = withContext(Dispatchers.IO) {
watchService.take()
}

StandardWatchEventKinds.ENTRY_DELETE -> {
val filePath = directory.resolve(fileName)
onDeleteCallback?.invoke(filePath.toString())
for (event in key.pollEvents()) {
val kind = event.kind()
val fileName = event.context() as Path

when (kind) {
StandardWatchEventKinds.ENTRY_CREATE -> {
val filePath = directory.resolve(fileName)
onCreateCallback?.invoke(filePath.toString())
}

StandardWatchEventKinds.ENTRY_DELETE -> {
val filePath = directory.resolve(fileName)
onDeleteCallback?.invoke(filePath.toString())
}

StandardWatchEventKinds.ENTRY_MODIFY -> {
val filePath = directory.resolve(fileName)
onModifyCallback?.invoke(filePath.toString())
}
}
}

StandardWatchEventKinds.ENTRY_MODIFY -> {
val filePath = directory.resolve(fileName)
onModifyCallback?.invoke(filePath.toString())
}
key.reset()
}
}

key.reset()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import xyz.mcxross.cohesive.daemon.createCohesiveProjectListener
import xyz.mcxross.cohesive.designsystem.mellow.File
import xyz.mcxross.cohesive.utils.Log

@OptIn(DelicateCoroutinesApi::class)
class Project(
Expand All @@ -29,7 +30,7 @@ class Project(

createCohesiveProjectListener(parentDir.absolutePath) {
onCreate {
//TODO handle new file created
//TODO handle modify file
}
onModify {
//TODO handle modify file
Expand Down

0 comments on commit f8e64cb

Please sign in to comment.