Hi, I am trying to wrap callback api which fires m...
# coroutines
k
Hi, I am trying to wrap callback api which fires multiple times with coroutines Because it fires multiple times I have to use some kind of
channel
. For now I have something like that:
Copy code
@Test
    fun watcherShoulbBeInvokedTwice() = runBlocking {
        val watcher = createWatcher()
        triggerFirstResponse()
        val channel = watcher.await(coroutineContext)
        for (data in channel) {
            println(data)

        }
        triggerSecondResponse()
    }
And my await function looks like this:
Copy code
fun <T> Watcher<T>.await(coroutineContext: CoroutineContext) = produce<T>(capacity = Channel.UNLIMITED, context = coroutineContext) {
    val callback = object : Callback<T>() {
        override fun onResponse(response: Response<T>) {
            if (isActive) {
                offer(response.data()!!)
            }
        }

        override fun onFailure(e: ApolloException) {
            if (isActive) {
                throw e
            }
        }
    }
    enqueueAndWatch(callback)
}
But I cannot make my tests to print two responses