hey, apologies if this has been answered a thousan...
# announcements
m
hey, apologies if this has been answered a thousand times, but is there a preferred solution to handling multiple listeners that can be added to one class?
Copy code
private class MyAdListener(private val listeners: List<AdListener>) : AdListener {
    override fun onAdStarted(adInfo: AdInfo) = listeners.forEach { it.onAdStarted(adInfo) }
    override fun onAdCompleted(adInfo: AdInfo) = listeners.forEach { it.onAdCompleted(adInfo) }
    override fun onAdBreakStarted(adInfo: AdInfo) = listeners.forEach { it.onAdBreakStarted(adInfo) }
    override fun onAdBreakCompleted(adInfo: AdInfo) = listeners.forEach { it.onAdBreakCompleted(adInfo) }
    // etc
is there a method for delegation that works? something like...
Copy code
private class MyAdListener(private val listeners: List<AdListener>) : AdListener by listeners.each()
z
Nope, but I would probably expose a Flow instead of a manual listener api.
m
thanks, looking at ways to restructure this
j
You can implement this with a dynamic proxy, to implement all methods of an interface in handle(), that just delegates the invoked method and its arguments to each listener in turn. It will be perfectly performant for almost all use cases.