Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes some potential bug with mapping lenses #910

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion core/src/jsMain/kotlin/dev/fritz2/core/tags.kt
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,10 @@ open class HtmlTag<out E : Element>(
}

override fun className(value: Flow<String>, initial: String) {
classesStateFlow.value += value.stateIn(MainScope() + job, SharingStarted.Eagerly, initial)
classesStateFlow.value += value
.catch { printErrorIgnoreLensException(it) }
.stateIn(MainScope() + job, SharingStarted.Eagerly, initial)

// this ensures that the set state is applied *immediately* without `Flow`-"delay".
// in this case, the `initial` value gets applied as "promised".
attr("class", buildClasses())
Expand Down
45 changes: 45 additions & 0 deletions core/src/jsTest/kotlin/dev/fritz2/core/tags.kt
Original file line number Diff line number Diff line change
Expand Up @@ -543,4 +543,49 @@ class TagTests {
assertEquals("fourth", getAttribute())
}

@Test
fun testClassNameWithFlowInsideReactiveScopeBasedUponListMapping_WontThrowUnhandledCollectionLensGetException() =
runTest {
val items = listOf("red", "green", "blue")

fun extractClasses() = items.mapNotNull { item -> document.getElementById(item) }
.joinToString(",") { it.className }

val storedItems = storeOf(items, Job())
val removeItem = storedItems.handle<String> { allItems, toRemove ->
allItems.filterNot { it == toRemove }
}

render {
storedItems.data.renderEach { item ->
val storedItem = storedItems.mapByElement(item) { it }
div(id = item) {
// access to mapped item will throw `CollectionLensGetException` after deletion
// -> must be catched internally like handlers do!
className(storedItem.data)

// we give some time to the `className`-function to try to access a mapped lens for an object
// that might not exist anymore.
beforeUnmount { _, _ -> delay(100) }
}
}
}

delay(100)
assertEquals("red,green,blue", extractClasses())

// this is the source of possible problem: By removing mapped lenses won't find their targeting object
// anymore. This will lead to `CollectionLensGetException`, that must be handled internally!
removeItem("green")

delay(200)
assertEquals("red,blue", extractClasses())

removeItem("blue")

// if not exception handling on the `classes`-Flow is done, rendering will freeze and the node for green
// will remain.
delay(200)
assertEquals("red", extractClasses())
}
}
Loading