-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimize CodeEditorView and BigMonospaceText to reduce the number of …
…text layout, so that text layout in the first rendering is already stable, and no need to wait for 200ms to render the first layout
- Loading branch information
1 parent
666ad9c
commit 9b20a7c
Showing
6 changed files
with
79 additions
and
29 deletions.
There are no files selected for viewing
52 changes: 32 additions & 20 deletions
52
src/jvmMain/kotlin/com/sunnychung/application/multiplatform/hellohttp/util/ComposeStates.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,51 @@ | ||
package com.sunnychung.application.multiplatform.hellohttp.util | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.State | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.runtime.setValue | ||
import com.sunnychung.lib.multiplatform.kdatetime.KDuration | ||
import kotlinx.coroutines.FlowPreview | ||
import kotlinx.coroutines.channels.BufferOverflow | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.debounce | ||
import kotlinx.coroutines.flow.flowOn | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
|
||
/** | ||
* @return pair of debounced state and "is the state latest" | ||
*/ | ||
@OptIn(FlowPreview::class) | ||
@Composable | ||
fun <T> debouncedStateOf(interval: KDuration, stateProducer: () -> T): T { | ||
fun <T> debouncedStateOf(interval: KDuration, tolerateCount: Int = 0, vararg cacheKeys: Any?, stateProducer: () -> T): Pair<T, Boolean> { | ||
val currentState = stateProducer() | ||
val coroutineScope = rememberCoroutineScope() | ||
val flow = remember { MutableSharedFlow<T>( | ||
replay = 1, | ||
extraBufferCapacity = 1, | ||
onBufferOverflow = BufferOverflow.DROP_OLDEST | ||
) } | ||
var stateRecord by remember { mutableStateOf(currentState) } | ||
val flow = remember(*cacheKeys) { MutableSharedFlow<T>( | ||
replay = 1, | ||
extraBufferCapacity = 1, | ||
onBufferOverflow = BufferOverflow.DROP_OLDEST | ||
) } | ||
var stateRecord by remember(*cacheKeys) { mutableStateOf(currentState) } | ||
var stateCount by remember(*cacheKeys) { mutableStateOf(0) } | ||
if (stateRecord != currentState) { | ||
flow.tryEmit(currentState) | ||
} | ||
flow.debounce(interval.toMilliseconds()) | ||
.onEach { | ||
stateRecord = it | ||
++stateCount | ||
flow.tryEmit(currentState).also { | ||
log.v { "ds tryEmit($currentState) = $it. old = $stateRecord" } | ||
} | ||
if (stateCount <= tolerateCount) { | ||
stateRecord = currentState | ||
} | ||
.launchIn(scope = coroutineScope) | ||
return stateRecord | ||
} | ||
LaunchedEffect(flow) { | ||
log.v { "ds launch flow" } | ||
flow.distinctUntilChanged() | ||
.debounce(interval.toMilliseconds()) | ||
.collect { | ||
log.v { "ds collect state $it" } | ||
stateRecord = it | ||
} | ||
} | ||
return (stateRecord to (stateRecord == currentState)).also { | ||
log.v { "ds state = $it, count = $stateCount" } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters