Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TreehouseApp's ziplineFlow a property #1728

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want a better diff here, you can step through the last two commits.

Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,56 @@ public class TreehouseApp<A : AppService> 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 val ziplineFlow: Flow<LoadResult> = run {
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,
)
}
}

loader.load(
applicationName = spec.name,
manifestUrlFlow = spec.manifestUrl,
serializersModule = spec.serializersModule,
) { zipline ->
spec.bindServices(zipline)
}
}

private val codeHost = object : CodeHost<A>(
dispatchers = dispatchers,
appScope = appScope,
frameClockFactory = factory.frameClockFactory,
stateStore = factory.stateStore,
) {
override fun codeUpdatesFlow(): Flow<CodeSession<A>> {
return ziplineFlow().mapNotNull { loadResult ->
return ziplineFlow.mapNotNull { loadResult ->
when (loadResult) {
is LoadResult.Failure -> {
null // EventListener already notified.
Expand Down Expand Up @@ -122,52 +164,10 @@ public class TreehouseApp<A : AppService> private constructor(
codeHost.restart()
}

/**
* Continuously polls for updated code, and emits a new [LoadResult] instance when new code is
* found.
*/
private fun ziplineFlow(): Flow<LoadResult> {
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<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
Loading