Skip to content

Commit

Permalink
Make CodeHost's codeUpsatesFlow a property
Browse files Browse the repository at this point in the history
  • Loading branch information
veyndan committed Dec 6, 2023
1 parent a914d68 commit ff85400
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,15 @@ import kotlinx.coroutines.launch
* * From `Starting` to `Running` when a `Zipline` finishes loading.
* * From `Running` to `Crashed` when a `Zipline` fails.
* * From `Running` to `Running` when the `Zipline` is replaced by a hot-reload.
*
* @param codeUpdatesFlow Returns a flow that emits a new [CodeSession] each time we should load fresh code.
*/
internal abstract class CodeHost<A : AppService>(
internal open class CodeHost<A : AppService>(
private val dispatchers: TreehouseDispatchers,
private val appScope: CoroutineScope,
private val frameClockFactory: FrameClock.Factory,
val stateStore: StateStore,
private val codeUpdatesFlow: Flow<CodeSession<A>>,
) {
/** Contents that this app is currently responsible for. */
private val listeners = mutableListOf<Listener<A>>()
Expand All @@ -76,9 +79,6 @@ internal abstract class CodeHost<A : AppService>(
val codeSession: CodeSession<A>?
get() = state.codeSession

/** Returns a flow that emits a new [CodeSession] each time we should load fresh code. */
abstract fun codeUpdatesFlow(): Flow<CodeSession<A>>

fun start() {
dispatchers.checkUi()

Expand Down Expand Up @@ -136,7 +136,7 @@ internal abstract class CodeHost<A : AppService>(

private fun CoroutineScope.collectCodeUpdates() {
launch(dispatchers.zipline) {
codeUpdatesFlow().collect {
codeUpdatesFlow.collect {
codeSessionLoaded(it)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class TreehouseApp<A : AppService> private constructor(
* Continuously polls for updated code, and emits a new [LoadResult] instance when new code is
* found.
*/
private fun ziplineFlow(): Flow<LoadResult> {
private val ziplineFlow: Flow<LoadResult> = run {
var loader = ZiplineLoader(
dispatcher = dispatchers.zipline,
manifestVerifier = factory.manifestVerifier,
Expand Down Expand Up @@ -76,7 +76,7 @@ public class TreehouseApp<A : AppService> private constructor(
}
}

return loader.load(
loader.load(
applicationName = spec.name,
manifestUrlFlow = spec.manifestUrl,
serializersModule = spec.serializersModule,
Expand All @@ -85,25 +85,22 @@ public class TreehouseApp<A : AppService> private constructor(
}
}

private val codeHost = object : CodeHost<A>(
private val codeHost = CodeHost(
dispatchers = dispatchers,
appScope = appScope,
frameClockFactory = factory.frameClockFactory,
stateStore = factory.stateStore,
) {
override fun codeUpdatesFlow(): Flow<CodeSession<A>> {
return ziplineFlow().mapNotNull { loadResult ->
when (loadResult) {
is LoadResult.Failure -> {
null // EventListener already notified.
}
is LoadResult.Success -> {
createCodeSession(loadResult.zipline)
}
codeUpdatesFlow = ziplineFlow.mapNotNull { loadResult ->
when (loadResult) {
is LoadResult.Failure -> {
null // EventListener already notified.
}
is LoadResult.Success -> {
createCodeSession(loadResult.zipline)
}
}
}
}
)

/**
* Returns the current zipline attached to this host, or null if Zipline hasn't loaded yet. The
Expand Down Expand Up @@ -167,7 +164,7 @@ public class TreehouseApp<A : AppService> private constructor(
private fun createCodeSession(zipline: Zipline): ZiplineCodeSession<A> {
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

Expand Down

0 comments on commit ff85400

Please sign in to comment.