Files
openide/platform/configuration-store-impl/src/SaveSessionProducerManager.kt
Vladimir Krivosheev 0904aef625 IJPL-148837 cleanup
GitOrigin-RevId: 5f67c4c65927d4924b93191377e9b092cb39e8ee
2024-05-17 10:06:45 +00:00

94 lines
3.0 KiB
Kotlin

// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.configurationStore
import com.intellij.openapi.application.writeAction
import com.intellij.openapi.components.StateStorage
import com.intellij.openapi.progress.ProcessCanceledException
import com.intellij.openapi.progress.blockingContext
import com.intellij.openapi.vfs.newvfs.RefreshQueue
import com.intellij.openapi.vfs.newvfs.events.VFileEvent
import kotlinx.coroutines.CancellationException
import java.util.*
internal open class SaveSessionProducerManager(private val isUseVfsForWrite: Boolean, private val collectVfsEvents: Boolean) {
private val producers = Collections.synchronizedMap(LinkedHashMap<StateStorage, SaveSessionProducer>())
fun getProducer(storage: StateStorage): SaveSessionProducer? {
var producer = producers[storage]
if (producer == null) {
producer = storage.createSaveSessionProducer() ?: return null
val prev = producers.put(storage, producer)
check(prev == null)
}
return producer
}
internal fun collectSaveSessions(result: MutableCollection<SaveSession>) {
for (session in producers.values) {
result.add(session.createSaveSession() ?: continue)
}
}
suspend fun save(saveResult: SaveResult) {
if (producers.isNotEmpty()) {
val saveSessions = ArrayList<SaveSession>()
collectSaveSessions(saveSessions)
if (saveSessions.isNotEmpty()) {
saveSessions(saveSessions, saveResult)
}
}
}
protected suspend fun saveSessions(saveSessions: Collection<SaveSession>, saveResult: SaveResult) {
if (isUseVfsForWrite) {
writeAction {
for (saveSession in saveSessions) {
try {
saveSession.saveBlocking()
}
catch (e: ReadOnlyModificationException) {
LOG.warn(e)
saveResult.addReadOnlyFile(SaveSessionAndFile(e.session ?: saveSession, e.file))
}
catch (e: ProcessCanceledException) {
throw e
}
catch (e: CancellationException) {
throw e
}
catch (e: Exception) {
saveResult.addError(e)
}
}
}
}
else {
val events = if (collectVfsEvents) ArrayList<VFileEvent>() else null
val syncList = if (events == null) null else Collections.synchronizedList(events)
for (saveSession in saveSessions) {
try {
saveSession.save(syncList)
}
catch (e: ReadOnlyModificationException) {
LOG.warn(e)
saveResult.addReadOnlyFile(SaveSessionAndFile(e.session ?: saveSession, e.file))
}
catch (e: ProcessCanceledException) {
throw e
}
catch (e: CancellationException) {
throw e
}
catch (e: Exception) {
saveResult.addError(e)
}
}
if (!events.isNullOrEmpty()) {
blockingContext {
RefreshQueue.getInstance().processEvents(false, events)
}
}
}
}
}