From a914d689028fd29c0b87dc2a455d02f5d63ed18e Mon Sep 17 00:00:00 2001 From: Veyndan Stuart Date: Wed, 6 Dec 2023 11:57:17 +0100 Subject: [PATCH 1/2] Move `ziplineFlow()` above `codeHost` --- .../cash/redwood/treehouse/TreehouseApp.kt | 84 +++++++++---------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/redwood-treehouse-host/src/commonMain/kotlin/app/cash/redwood/treehouse/TreehouseApp.kt b/redwood-treehouse-host/src/commonMain/kotlin/app/cash/redwood/treehouse/TreehouseApp.kt index 2768c5acd9..ca16e22a27 100644 --- a/redwood-treehouse-host/src/commonMain/kotlin/app/cash/redwood/treehouse/TreehouseApp.kt +++ b/redwood-treehouse-host/src/commonMain/kotlin/app/cash/redwood/treehouse/TreehouseApp.kt @@ -43,6 +43,48 @@ public class TreehouseApp private constructor( ) { public val dispatchers: TreehouseDispatchers = factory.dispatchers + /** + * Continuously polls for updated code, and emits a new [LoadResult] instance when new code is + * found. + */ + private fun ziplineFlow(): Flow { + var loader = ZiplineLoader( + dispatcher = dispatchers.zipline, + manifestVerifier = factory.manifestVerifier, + httpClient = factory.httpClient, + ) + + loader.concurrentDownloads = factory.concurrentDownloads + + // Adapt [EventListener.Factory] to a [ZiplineEventListener.Factory] + val ziplineEventListenerFactory = ZiplineEventListener.Factory { _, manifestUrl -> + val eventListener = factory.eventListenerFactory.create(this@TreehouseApp, manifestUrl) + RealEventPublisher(eventListener).ziplineEventListener + } + loader = loader.withEventListenerFactory(ziplineEventListenerFactory) + + if (!spec.loadCodeFromNetworkOnly) { + loader = loader.withCache( + cache = factory.cache, + ) + + if (factory.embeddedDir != null && factory.embeddedFileSystem != null) { + loader = loader.withEmbedded( + embeddedDir = factory.embeddedDir, + embeddedFileSystem = factory.embeddedFileSystem, + ) + } + } + + return loader.load( + applicationName = spec.name, + manifestUrlFlow = spec.manifestUrl, + serializersModule = spec.serializersModule, + ) { zipline -> + spec.bindServices(zipline) + } + } + private val codeHost = object : CodeHost( dispatchers = dispatchers, appScope = appScope, @@ -122,48 +164,6 @@ public class TreehouseApp private constructor( codeHost.restart() } - /** - * Continuously polls for updated code, and emits a new [LoadResult] instance when new code is - * found. - */ - private fun ziplineFlow(): Flow { - var loader = ZiplineLoader( - dispatcher = dispatchers.zipline, - manifestVerifier = factory.manifestVerifier, - httpClient = factory.httpClient, - ) - - loader.concurrentDownloads = factory.concurrentDownloads - - // Adapt [EventListener.Factory] to a [ZiplineEventListener.Factory] - val ziplineEventListenerFactory = ZiplineEventListener.Factory { _, manifestUrl -> - val eventListener = factory.eventListenerFactory.create(this@TreehouseApp, manifestUrl) - RealEventPublisher(eventListener).ziplineEventListener - } - loader = loader.withEventListenerFactory(ziplineEventListenerFactory) - - if (!spec.loadCodeFromNetworkOnly) { - loader = loader.withCache( - cache = factory.cache, - ) - - if (factory.embeddedDir != null && factory.embeddedFileSystem != null) { - loader = loader.withEmbedded( - embeddedDir = factory.embeddedDir, - embeddedFileSystem = factory.embeddedFileSystem, - ) - } - } - - return loader.load( - applicationName = spec.name, - manifestUrlFlow = spec.manifestUrl, - serializersModule = spec.serializersModule, - ) { zipline -> - spec.bindServices(zipline) - } - } - private fun createCodeSession(zipline: Zipline): ZiplineCodeSession { val appService = spec.create(zipline) From 3ea9f438d12d9c027d5df21909590c3f9bc53822 Mon Sep 17 00:00:00 2001 From: Veyndan Stuart Date: Wed, 6 Dec 2023 12:00:56 +0100 Subject: [PATCH 2/2] Make `TreehouseApp`'s `ziplineFlow` a property --- .../kotlin/app/cash/redwood/treehouse/TreehouseApp.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/redwood-treehouse-host/src/commonMain/kotlin/app/cash/redwood/treehouse/TreehouseApp.kt b/redwood-treehouse-host/src/commonMain/kotlin/app/cash/redwood/treehouse/TreehouseApp.kt index ca16e22a27..6001b223c1 100644 --- a/redwood-treehouse-host/src/commonMain/kotlin/app/cash/redwood/treehouse/TreehouseApp.kt +++ b/redwood-treehouse-host/src/commonMain/kotlin/app/cash/redwood/treehouse/TreehouseApp.kt @@ -47,7 +47,7 @@ public class TreehouseApp private constructor( * Continuously polls for updated code, and emits a new [LoadResult] instance when new code is * found. */ - private fun ziplineFlow(): Flow { + private val ziplineFlow: Flow = run { var loader = ZiplineLoader( dispatcher = dispatchers.zipline, manifestVerifier = factory.manifestVerifier, @@ -76,7 +76,7 @@ public class TreehouseApp private constructor( } } - return loader.load( + loader.load( applicationName = spec.name, manifestUrlFlow = spec.manifestUrl, serializersModule = spec.serializersModule, @@ -92,7 +92,7 @@ public class TreehouseApp private constructor( stateStore = factory.stateStore, ) { override fun codeUpdatesFlow(): Flow> { - return ziplineFlow().mapNotNull { loadResult -> + return ziplineFlow.mapNotNull { loadResult -> when (loadResult) { is LoadResult.Failure -> { null // EventListener already notified. @@ -167,7 +167,7 @@ public class TreehouseApp private constructor( private fun createCodeSession(zipline: Zipline): ZiplineCodeSession { val appService = spec.create(zipline) - // Extract the RealEventPublisher() created in ziplineFlow(). + // Extract the RealEventPublisher() created in ziplineFlow. val eventListener = zipline.eventListener as RealEventPublisher.ZiplineEventListener val eventPublisher = eventListener.eventPublisher