Skip to content

Commit

Permalink
fix infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny-chung committed Nov 3, 2024
1 parent fd484cc commit 635b6af
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.sunnychung.application.multiplatform.hellohttp.util

class ObjectRef<T>(val ref: T) {
override fun equals(other: Any?): Boolean {
if (other !is ObjectRef<*>) return false
return ref === other.ref
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import com.sunnychung.application.multiplatform.hellohttp.extension.insert
import com.sunnychung.application.multiplatform.hellohttp.extension.intersect
import com.sunnychung.application.multiplatform.hellohttp.extension.length
import com.sunnychung.application.multiplatform.hellohttp.model.SyntaxHighlight
import com.sunnychung.application.multiplatform.hellohttp.util.ObjectRef
import com.sunnychung.application.multiplatform.hellohttp.util.TreeRangeMaps
import com.sunnychung.application.multiplatform.hellohttp.util.chunkedLatest
import com.sunnychung.application.multiplatform.hellohttp.util.log
Expand Down Expand Up @@ -491,7 +492,7 @@ fun CodeEditorView(
val string = it.bigText.buildCharSequence() as AnnotatedString
log.d { "${bigTextFieldState.text} : ${it.bigText} onTextChange(${string.text.abbr()})" }
onTextChange(string.text)
secondCacheKey.value = string.text
secondCacheKey.value = ObjectRef(string.text)
}
bigTextValueId = it.changeId
searchTrigger.trySend(Unit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.text.AnnotatedString
import com.sunnychung.application.multiplatform.hellohttp.util.ObjectRef
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
Expand Down Expand Up @@ -40,16 +41,22 @@ fun rememberAnnotatedBigTextFieldState(initialValue: AnnotatedString = Annotated
}

@Composable
fun rememberAnnotatedBigTextFieldState(initialValue: String = ""): Pair<MutableState<String>, MutableState<BigTextFieldState>> {
val secondCacheKey = rememberSaveable { mutableStateOf(initialValue) }
fun rememberAnnotatedBigTextFieldState(initialValue: String = ""): Pair<MutableState<ObjectRef<String>>, MutableState<BigTextFieldState>> {
val secondCacheKey = rememberSaveable { mutableStateOf(ObjectRef(initialValue)) }
val state = rememberSaveable {
log.d { "cache miss 1" }
log.i { "cache miss 1" }
mutableStateOf(BigTextFieldState(BigText.createFromLargeAnnotatedString(AnnotatedString(initialValue)), BigTextViewState()))
}
if (initialValue !== secondCacheKey.value) {
log.d { "cache miss. old key2 = ${secondCacheKey.value.abbr()}; new key2 = ${initialValue.abbr()}" }
secondCacheKey.value = initialValue
if (ObjectRef(initialValue) != secondCacheKey.value) {
log.i { "cache miss. old key2 = ${secondCacheKey.value.ref.abbr()}; new key2 = ${initialValue.abbr()}" }

// // set a value different from initialValue, otherwise the value 'equals' to the old value and would not be set to secondCacheKey.value
// secondCacheKey.value = if (initialValue.isNotEmpty()) "" else " "
// secondCacheKey.value = initialValue

secondCacheKey.value = ObjectRef(initialValue)
state.value = BigTextFieldState(BigText.createFromLargeAnnotatedString(AnnotatedString(initialValue)), BigTextViewState())
// log.i { "new view state = ${state.value.viewState}" }
}
return secondCacheKey to state
}
Expand Down

0 comments on commit 635b6af

Please sign in to comment.