diff --git a/components/resources/library/src/blockingMain/kotlin/org/jetbrains/compose/resources/Resource.blocking.kt b/components/resources/library/src/blockingMain/kotlin/org/jetbrains/compose/resources/Resource.blocking.kt index e67caeed50..e69de29bb2 100644 --- a/components/resources/library/src/blockingMain/kotlin/org/jetbrains/compose/resources/Resource.blocking.kt +++ b/components/resources/library/src/blockingMain/kotlin/org/jetbrains/compose/resources/Resource.blocking.kt @@ -1,9 +0,0 @@ -package org.jetbrains.compose.resources - -import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.DelicateCoroutinesApi -import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.newSingleThreadContext - -@OptIn(ExperimentalCoroutinesApi::class, DelicateCoroutinesApi::class) -internal actual val cacheDispatcher: CoroutineDispatcher = newSingleThreadContext("resources_cache_ctx") \ No newline at end of file diff --git a/components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/ImageResources.kt b/components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/ImageResources.kt index 5fb4827bc1..c2989babea 100644 --- a/components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/ImageResources.kt +++ b/components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/ImageResources.kt @@ -9,6 +9,8 @@ import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.graphics.vector.rememberVectorPainter import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.unit.dp +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.withContext import org.jetbrains.compose.resources.vector.toImageVector import org.jetbrains.compose.resources.vector.xmldom.Element @@ -88,6 +90,8 @@ private sealed interface ImageCache { class Vector(val vector: ImageVector) : ImageCache } +@OptIn(ExperimentalCoroutinesApi::class) +private val imageCacheDispatcher = Dispatchers.Default.limitedParallelism(1) private val imageCache = mutableMapOf() //@TestOnly @@ -99,6 +103,6 @@ private suspend fun loadImage( path: String, resourceReader: ResourceReader, decode: (ByteArray) -> ImageCache -): ImageCache = withContext(cacheDispatcher) { +): ImageCache = withContext(imageCacheDispatcher) { imageCache.getOrPut(path) { decode(resourceReader.read(path)) } } diff --git a/components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/Resource.kt b/components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/Resource.kt index ba5ac44486..3928a4b1dd 100644 --- a/components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/Resource.kt +++ b/components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/Resource.kt @@ -1,10 +1,6 @@ package org.jetbrains.compose.resources -import kotlinx.coroutines.CoroutineDispatcher - internal typealias ResourceId = String @RequiresOptIn("This API is experimental and is likely to change in the future.") -annotation class ExperimentalResourceApi - -internal expect val cacheDispatcher: CoroutineDispatcher \ No newline at end of file +annotation class ExperimentalResourceApi \ No newline at end of file diff --git a/components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/StringResources.kt b/components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/StringResources.kt index 3b513fb693..194db39a78 100644 --- a/components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/StringResources.kt +++ b/components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/StringResources.kt @@ -2,6 +2,8 @@ package org.jetbrains.compose.resources import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.withContext import org.jetbrains.compose.resources.vector.xmldom.Element import org.jetbrains.compose.resources.vector.xmldom.NodeList @@ -14,6 +16,8 @@ private sealed interface StringItem { data class Array(val items: List) : StringItem } +@OptIn(ExperimentalCoroutinesApi::class) +private val stringsCacheDispatcher = Dispatchers.Default.limitedParallelism(1) private val parsedStringsCache = mutableMapOf>() //@TestOnly @@ -22,7 +26,7 @@ internal fun dropStringsCache() { } private suspend fun getParsedStrings(path: String, resourceReader: ResourceReader): Map = - withContext(cacheDispatcher) { + withContext(stringsCacheDispatcher) { parsedStringsCache.getOrPut(path) { val nodes = resourceReader.read(path).toXmlElement().childNodes val strings = nodes.getElementsWithName("string").associate { element -> diff --git a/components/resources/library/src/jsMain/kotlin/org/jetbrains/compose/resources/Resource.js.kt b/components/resources/library/src/jsMain/kotlin/org/jetbrains/compose/resources/Resource.js.kt index c560bfc90b..ae96944465 100644 --- a/components/resources/library/src/jsMain/kotlin/org/jetbrains/compose/resources/Resource.js.kt +++ b/components/resources/library/src/jsMain/kotlin/org/jetbrains/compose/resources/Resource.js.kt @@ -1,10 +1,5 @@ package org.jetbrains.compose.resources -import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.Dispatchers - -internal actual val cacheDispatcher: CoroutineDispatcher = Dispatchers.Default - /** * Represents the configuration object for web resources. *