Skip to content

Commit

Permalink
tech: config read moved to background thread
Browse files Browse the repository at this point in the history
  • Loading branch information
popovanton0 committed May 18, 2024
1 parent 1bd9a8e commit f66e7f6
Showing 1 changed file with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package ru.ozon.ideplugin.kelp.pluginConfig
import com.intellij.notification.NotificationGroupManager
import com.intellij.notification.NotificationType
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.invokeLater
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.serviceOrNull
import com.intellij.openapi.project.Project
Expand All @@ -30,6 +30,7 @@ fun Project.kelpConfig(): KelpConfig? = serviceOrNull<KelpConfigService>()?.data
private class KelpConfigService(private val project: Project) : Disposable {
private val json = Json { ignoreUnknownKeys = true }
private val configFilePath = pluginConfigDirPath(project) / CONFIG_FILE_NAME
private val application = ApplicationManager.getApplication()

private var configText: String? = null
var data: KelpConfig? = null
Expand All @@ -47,14 +48,16 @@ private class KelpConfigService(private val project: Project) : Disposable {

private fun reloadConfig(isFirstRun: Boolean) {
runCatching {
runReadAction {
configText = VirtualFileManager.getInstance()
.findFileByNioPath(configFilePath)
?.readText()
application.executeOnPooledThread {
application.runReadAction {
configText = VirtualFileManager.getInstance()
.findFileByNioPath(configFilePath)
?.readText()

data = json.decodeFromString<KelpConfig>(configText ?: return@runReadAction)
AddLiveTemplates.execute(data!!, project.name)
if (!isFirstRun) reloadNotification()
data = json.decodeFromString<KelpConfig>(configText ?: return@runReadAction)
AddLiveTemplates.execute(data!!, project.name)
if (!isFirstRun) reloadNotification()
}
}
}.onFailure {
invalidConfigError(it)
Expand Down

0 comments on commit f66e7f6

Please sign in to comment.