Skip to content

Commit

Permalink
Fix dispose FileChangeDetection
Browse files Browse the repository at this point in the history
  • Loading branch information
sdercolin committed Jul 22, 2024
1 parent 46844f1 commit e197bd4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/jvmMain/kotlin/com/sdercolin/vlabeler/ui/AppState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ class AppState(

fun exit(fromError: Boolean = false) {
mainScope.launch {
terminalAutoReloadLabel()
terminateAutoSaveProject()
if (!fromError) discardAutoSavedProjects()
ipcState.close()
Expand Down
6 changes: 6 additions & 0 deletions src/jvmMain/kotlin/com/sdercolin/vlabeler/ui/ProjectStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ interface ProjectStore {
*/
fun reloadLabelFile(file: File?, skipConfirmation: Boolean)
fun autoReloadLabel(behavior: AppConf.AutoReload.Behavior, moduleName: String)
suspend fun terminalAutoReloadLabel()

suspend fun withExporting(onSuccess: suspend () -> File)
}
Expand Down Expand Up @@ -812,4 +813,9 @@ class ProjectStoreImpl(
cachedFileHashMap[file.absolutePath] = hash
isExporting = false
}

override suspend fun terminalAutoReloadLabel() {
fileChangeDetection?.awaitDispose()
fileChangeDetection = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import com.sdercolin.vlabeler.env.Log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.yield
import java.io.File
import java.nio.file.ClosedWatchServiceException
import java.nio.file.FileSystems
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.StandardWatchEventKinds.ENTRY_CREATE
import java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY
import java.nio.file.WatchService

class FileChangeDetection(
private val directory: File,
Expand All @@ -21,6 +24,7 @@ class FileChangeDetection(
) {
private var job: Job? = null
private var callbackJob: Job? = null
private var watchService: WatchService? = null

fun startIn(coroutineScope: CoroutineScope) {
if (job?.isActive == true) {
Expand All @@ -34,6 +38,8 @@ class FileChangeDetection(
directory.mkdirs()
}
val watchService = FileSystems.getDefault().newWatchService()
this@FileChangeDetection.watchService?.close()
this@FileChangeDetection.watchService = watchService
val directoryPath = Paths.get(directory.absolutePath)
directoryPath.register(watchService, ENTRY_CREATE, ENTRY_MODIFY)
while (isActive) {
Expand Down Expand Up @@ -67,18 +73,27 @@ class FileChangeDetection(
}
}
}.onFailure {
Log.error(it)
if (it !is ClosedWatchServiceException) {
Log.error(it)
}
}
}
}

fun dispose() {
this@FileChangeDetection.watchService?.close()
callbackJob?.cancel()
callbackJob = null
job?.cancel()
job = null
}

suspend fun awaitDispose() {
this@FileChangeDetection.watchService?.close()
callbackJob?.cancelAndJoin()
job?.cancelAndJoin()
}

fun interface Callback {
suspend fun onFileChanged(changed: List<File>, new: List<File>)
}
Expand Down

0 comments on commit e197bd4

Please sign in to comment.