Hey folks, I have a question <https://stackoverflo...
# coroutines
a
Hey folks, I have a question https://stackoverflow.com/q/54552056/1032286 would really appreciate if someone can help me with this
nevermind, answered it myself
the resulting coroutine can be used in
.produce{}
to create a channel
❤️ 1
a
huh. Creating a coroutine per each listener trigger?
Copy code
suspend fun View.globalLayout(): View =
    suspendCancellableCoroutine { cont ->
        val listener = ViewTreeObserver.OnGlobalLayoutListener {
            cont.resume(this)
        }

        cont.invokeOnCancellation {
            viewTreeObserver.removeOnGlobalLayoutListener(listener)
        }

        viewTreeObserver.addOnGlobalLayoutListener(listener)
    }

fun View.globalLayouts(context: CoroutineContext = Dispatchers.Unconfined): ReceiveChannel<View> =
    GlobalScope.produce(context) {
        while (true) {
            send(globalLayout())
        }
    }
?
*creating a new continuation per listener trigger