Browse Source

[resources] Use cached value synchronously on web. (#4893)

The change speeds resources web rendering up by the reading a cached
value instantly by request (it was being dispatched to the end of the UI
queue in `LaunchedEffect`)

## Release Notes
### Features - Resources
- Speed resources web rendering up by the reading a cached value
instantly
pull/4901/head
Konstantin 6 months ago committed by GitHub
parent
commit
2305ea77ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 37
      components/resources/library/src/webMain/kotlin/org/jetbrains/compose/resources/ResourceState.web.kt

37
components/resources/library/src/webMain/kotlin/org/jetbrains/compose/resources/ResourceState.web.kt

@ -1,10 +1,12 @@
package org.jetbrains.compose.resources package org.jetbrains.compose.resources
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.State import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.launch
@Composable @Composable
internal actual fun <T> rememberResourceState( internal actual fun <T> rememberResourceState(
@ -13,11 +15,14 @@ internal actual fun <T> rememberResourceState(
block: suspend (ResourceEnvironment) -> T block: suspend (ResourceEnvironment) -> T
): State<T> { ): State<T> {
val environment = LocalComposeEnvironment.current.rememberEnvironment() val environment = LocalComposeEnvironment.current.rememberEnvironment()
val state = remember(key1) { mutableStateOf(getDefault()) } val scope = rememberCoroutineScope()
LaunchedEffect(key1) { return remember(key1) {
state.value = block(environment) val mutableState = mutableStateOf(getDefault())
scope.launch(start = CoroutineStart.UNDISPATCHED) {
mutableState.value = block(environment)
}
mutableState
} }
return state
} }
@Composable @Composable
@ -28,11 +33,14 @@ internal actual fun <T> rememberResourceState(
block: suspend (ResourceEnvironment) -> T block: suspend (ResourceEnvironment) -> T
): State<T> { ): State<T> {
val environment = LocalComposeEnvironment.current.rememberEnvironment() val environment = LocalComposeEnvironment.current.rememberEnvironment()
val state = remember(key1, key2) { mutableStateOf(getDefault()) } val scope = rememberCoroutineScope()
LaunchedEffect(key1, key2) { return remember(key1, key2) {
state.value = block(environment) val mutableState = mutableStateOf(getDefault())
scope.launch(start = CoroutineStart.UNDISPATCHED) {
mutableState.value = block(environment)
}
mutableState
} }
return state
} }
@Composable @Composable
@ -44,9 +52,12 @@ internal actual fun <T> rememberResourceState(
block: suspend (ResourceEnvironment) -> T block: suspend (ResourceEnvironment) -> T
): State<T> { ): State<T> {
val environment = LocalComposeEnvironment.current.rememberEnvironment() val environment = LocalComposeEnvironment.current.rememberEnvironment()
val state = remember(key1, key2, key3) { mutableStateOf(getDefault()) } val scope = rememberCoroutineScope()
LaunchedEffect(key1, key2, key3) { return remember(key1, key2, key3) {
state.value = block(environment) val mutableState = mutableStateOf(getDefault())
scope.launch(start = CoroutineStart.UNDISPATCHED) {
mutableState.value = block(environment)
}
mutableState
} }
return state
} }
Loading…
Cancel
Save