Skip to content

Commit

Permalink
do not use flatMapLatest for events (#834)
Browse files Browse the repository at this point in the history
  • Loading branch information
metin-kale authored Jan 2, 2024
1 parent d7c3d02 commit 66564c1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,19 @@ class SelectionMode<T>(override val job: Job) : WithJob {
fun selectItem(itemToSelect: Flow<T>, data: CollectionData<T>) {
if (single.isSet) {
single.handler?.let {
it(single.data.flatMapLatest { current ->
itemToSelect.map { item ->
if (data.isSame(current, item)) null else item
}
it(itemToSelect.map { item ->
val current = single.data.first()
if (data.isSame(current, item)) null else item
})
}
} else {
multi.handler?.let {
it(multi.data.flatMapLatest { current ->
itemToSelect.map { item ->
data.idProvider?.let { id ->
if (current.any { id(it) == id(item) }) current.filter { id(it) != id(item) }
else current + item
} ?: if (current.contains(item)) current - item else current + item
}
it(itemToSelect.map { item ->
val current = multi.data.first()
data.idProvider?.let { id ->
if (current.any { id(it) == id(item) }) current.filter { id(it) != id(item) }
else current + item
} ?: if (current.contains(item)) current - item else current + item
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,14 @@ class TabGroup<C : HTMLElement>(tag: Tag<C>, id: String?) : Tag<C> by tag {

private val isActive: Store<Int?> = storeOf(null)

private val state by lazy { selected.combine(disabledTabs.data, ::Pair) }

private fun <T> Flow<T>.handledBy(withJob: WithJob, handler: (Int, T, List<Boolean>) -> Int): Unit? =
value.handler?.invoke(
withJob,
state.flatMapLatest { (currentIndex, disabledTabs) ->
this.map { nextIndex ->
if (disabledTabs.all { it }) -1 else handler(currentIndex, nextIndex, disabledTabs)
}
})
this.map { nextIndex ->
val currentIndex = selected.first()
if (disabledTabs.current.all { it }) -1 else handler(currentIndex, nextIndex, disabledTabs.current)
}
)

private fun <T> withActiveUpdates(decorated: (Int, T, List<Boolean>) -> Int): (Int, T, List<Boolean>) -> Int =
{ currentIndex, payload, disabledTabs ->
Expand Down

0 comments on commit 66564c1

Please sign in to comment.