Hi again. I have another K\N related question. I h...
# kotlin-native
a
Hi again. I have another K\N related question. I have a listener in my shared module. Anyone who use my library is expected to subscribe to events using this interface/protocol. I wonder if it makes sense to wrap this interface with some sort of expect/actual mechanism to avoid retain cycle on iOS or are my concerns are irrelevant? Consider above:
Copy code
// Shared:
interface EventListener {
    fun onEvent(event: Event)
}

internal expect class EventDispatcher {
    fun dispatch(event: Event)
}

// iOS:
internal actual class EventDispatcher(listener: EventListener) {
    private val listenerWeakRef = WeakReference(listener)

    actual fun dispatch(event: Event) {
        listenerWeakRef.value?.onEvent(event)
    }
}

// Android
internal actual class EventDispatcher(private val listener: EventListener) {
    actual fun dispatch(event: Event) {
        listener.onEvent(event)
    }
}
and when user subscribe to events I wrap EventListener he/she provides with EventDispatcher (platform specific implementation)
Copy code
//Shared code
fun subscribe(listener: EventListener) {
    someInternalClass.subscribe(EventDispatcher(listener)) // From here on I am dispatching using wrapper.
}
Does that makes sense to you?