Skip to content

Commit

Permalink
Fix coroutine scoping of Redwood Treehouse UI
Browse files Browse the repository at this point in the history
Ensure that Zipline resources aren't closed until the UI's coroutine scope job completes
  • Loading branch information
dellisd committed Apr 3, 2024
1 parent 9ae18ef commit bfc07fc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Fixed:
- Fix `TreehouseUIView` to size itself according to the size of its subview.
- In `UIViewLazyList`, adding `beginUpdates`/`endUpdates` calls to `insertRows`/`deleteRows`, and wrapping changes in `UIView.performWithoutAnimation` blocks.
- Fix memory leak in 'protocol-guest' and 'protocol-host' where child nodes beneath a removed node were incorrectly retained in an internal map indefinitely.
- Ensure that Zipline services are not closed prematurely when disposing a Treehouse UI.


## [0.9.0] - 2024-02-28
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ import app.cash.redwood.ui.OnBackPressedDispatcher
import app.cash.redwood.ui.UiConfiguration
import app.cash.zipline.ZiplineScope
import app.cash.zipline.ZiplineScoped
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.job
import kotlinx.coroutines.plus

/**
Expand All @@ -55,6 +59,11 @@ private class RedwoodZiplineTreehouseUi(
*/
override val scope = (treehouseUi as? ZiplineScoped)?.scope ?: ZiplineScope()

private val coroutineScope = CoroutineScope(
appLifecycle.coroutineScope.coroutineContext +
Job(appLifecycle.coroutineScope.coroutineContext.job),
)

private lateinit var composition: RedwoodComposition

private lateinit var saveableStateRegistry: SaveableStateRegistry
Expand Down Expand Up @@ -102,7 +111,7 @@ private class RedwoodZiplineTreehouseUi(
)

val composition = ProtocolRedwoodComposition(
scope = appLifecycle.coroutineScope + appLifecycle.frameClock,
scope = coroutineScope + appLifecycle.frameClock,
bridge = bridge,
widgetVersion = appLifecycle.widgetVersion,
changesSink = host,
Expand All @@ -121,9 +130,12 @@ private class RedwoodZiplineTreehouseUi(
}

override fun close() {
coroutineScope.coroutineContext.job.invokeOnCompletion {
scope.close()
}
coroutineScope.cancel()
composition.cancel()
treehouseUi.close()
scope.close()
}
}

Expand Down

0 comments on commit bfc07fc

Please sign in to comment.