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

Please add a Default EventListener #2129

Open
handstandsam opened this issue Jun 20, 2024 · 2 comments
Open

Please add a Default EventListener #2129

handstandsam opened this issue Jun 20, 2024 · 2 comments

Comments

@handstandsam
Copy link

handstandsam commented Jun 20, 2024

When doing an initial setup/integration it's not easy to know that you need to create one of these and override the correct methods.

Finally figured out an issue after creating one of these and implementing some of the error and lifecycle (loaded, etc) methods.

EventListener: https://github.com/cashapp/redwood/blob/d3b544d528b07edef5afdb5300d33d301c2950ff/redwood-treehouse-host/src/commonMain/kotlin/app/cash/redwood/treehouse/EventListener.kt

@JakeWharton
Copy link
Collaborator

There is a default. Are you saying the behavior of the default should do something like log?

@handstandsam
Copy link
Author

handstandsam commented Jul 3, 2024

While hacking with @swankjesse to set up redwood on a new project, we had to provide our own EventListener in order to surface silent failures.

The event listener that we had to implement:

import app.cash.redwood.treehouse.EventListener
import app.cash.redwood.treehouse.TreehouseApp
import app.cash.zipline.Zipline
import app.cash.zipline.ZiplineManifest

class MyEventListener : EventListener() {

    override fun codeLoadStart(): Any? {
        println("codeLoadStart()")
        return null
    }

    override fun codeLoadSuccess(manifest: ZiplineManifest, zipline: Zipline, startValue: Any?) {
        println("codeLoadSuccess($manifest $zipline)")
    }

    override fun codeLoadSkipped(startValue: Any?) {
        println("codeLoadSkipped()")
    }

    override fun codeLoadSkippedNotFresh(startValue: Any?) {
        println("codeLoadSkippedNotFresh()")
    }

    override fun codeLoadFailed(exception: Exception, startValue: Any?) {
        println("codeLoadFailed($exception)")
    }

    override fun uncaughtException(exception: Throwable) {
        exception.printStackTrace()
    }

    class Factory : EventListener.Factory {
        override fun create(app: TreehouseApp<*>, manifestUrl: String?): EventListener {
            return MyEventListener()
        }
    }
}

Which we added here during treehouseAppFactory.create:

    private fun createTreehouseApp(httpClient: OkHttpClient): TreehouseApp<PathRoutableAppService> {
        val treehouseAppFactory = TreehouseAppFactory(
            context = applicationContext,
            httpClient = httpClient,
            manifestVerifier = ManifestVerifier.Companion.NO_SIGNATURE_CHECKS,
        )

        val manifestUrlFlow =
            flowOf("http://10.0.2.2:23368/manifest.zipline.json").withDevelopmentServerPush(
                    httpClient.asZiplineHttpClient()
                )

        val treehouseApp = treehouseAppFactory.create(
            appScope = scope,
            spec = object : TreehouseApp.Spec<PathRoutableAppService>() {
                override val name: String = "my"
                override val manifestUrl: Flow<String> = manifestUrlFlow
                override fun bindServices(zipline: Zipline) {
                    zipline.bind("RawTreehousePlatform", platform)
                }

                override fun create(zipline: Zipline): PathRoutableAppService {
                    return zipline.take("PathRoutableAppService")
                }
            },
            eventListenerFactory = MyEventListener.Factory()
        )

        treehouseApp.start()

        return treehouseApp
    }

Without that, all exceptions were silent and we were in the dark. It would have been nice to see the errors as it wasn't obvious to provide an event listener.


I'll DM you a link to the commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants