From 9a5b1b54efe059f9cf4435b7d14df80265b20280 Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Wed, 20 Nov 2024 11:00:07 -0500 Subject: [PATCH] Refactor of common path to return a list Found in my stash list... --- .../protocol/guest/GuestProtocolAdapter.kt | 2 +- .../protocol/guest/ProtocolWidgetChildren.kt | 21 ++++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/redwood-protocol-guest/src/commonMain/kotlin/app/cash/redwood/protocol/guest/GuestProtocolAdapter.kt b/redwood-protocol-guest/src/commonMain/kotlin/app/cash/redwood/protocol/guest/GuestProtocolAdapter.kt index 45cccc98dd..50c7c44997 100644 --- a/redwood-protocol-guest/src/commonMain/kotlin/app/cash/redwood/protocol/guest/GuestProtocolAdapter.kt +++ b/redwood-protocol-guest/src/commonMain/kotlin/app/cash/redwood/protocol/guest/GuestProtocolAdapter.kt @@ -149,7 +149,7 @@ public abstract class GuestProtocolAdapter( public abstract fun removeWidget(id: Id) @RedwoodCodegenApi - public val childrenVisitor: ProtocolWidget.ChildrenVisitor = if (synthesizeSubtreeRemoval) { + public val childrenRemover: ProtocolWidget.ChildrenVisitor = if (synthesizeSubtreeRemoval) { object : ProtocolWidget.ChildrenVisitor { override fun visit( parent: ProtocolWidget, diff --git a/redwood-protocol-guest/src/commonMain/kotlin/app/cash/redwood/protocol/guest/ProtocolWidgetChildren.kt b/redwood-protocol-guest/src/commonMain/kotlin/app/cash/redwood/protocol/guest/ProtocolWidgetChildren.kt index 8c9432679e..1f4e53e99d 100644 --- a/redwood-protocol-guest/src/commonMain/kotlin/app/cash/redwood/protocol/guest/ProtocolWidgetChildren.kt +++ b/redwood-protocol-guest/src/commonMain/kotlin/app/cash/redwood/protocol/guest/ProtocolWidgetChildren.kt @@ -37,25 +37,26 @@ public class ProtocolWidgetChildren( } override fun remove(index: Int, count: Int) { - if (guestAdapter.synthesizeSubtreeRemoval) { + val removedIds = if (guestAdapter.synthesizeSubtreeRemoval) { // This boxes Id values. Don't bother optimizing since it only serves very old hosts. - val removedIds = ArrayList(count) - for (i in index until index + count) { - val widget = _widgets[i] - removedIds += widget.id - guestAdapter.removeWidget(widget.id) + buildList(count) { + for (i in index until index + count) { + val widget = _widgets[i] + this += widget.id + guestAdapter.removeWidget(widget.id) - widget.depthFirstWalk(guestAdapter.childrenVisitor) + widget.depthFirstWalk(guestAdapter.childrenRemover) + } } - guestAdapter.appendRemove(id, tag, index, count, removedIds) } else { for (i in index until index + count) { val widget = _widgets[i] guestAdapter.removeWidget(widget.id) - widget.depthFirstWalk(guestAdapter.childrenVisitor) + widget.depthFirstWalk(guestAdapter.childrenRemover) } - guestAdapter.appendRemove(id, tag, index, count, emptyList()) + emptyList() } + guestAdapter.appendRemove(id, tag, index, count, removedIds) _widgets.remove(index, count) }