moshkit icon

Untitled

moshkit | PRO | 12/28/20 08:47:05 AM UTC | 0 ⭐ | 13496 👁️ | Never ⏰ | []
Kotlin |

1.1 KB

|

None

|

0 👍

/

0 👎

fun <T> ApolloSubscriptionCall<T>.toFlowCacheSafely(): Flow<Response<T>> = callbackFlow {
    val clone = clone()
    var pendingException: ApolloException? = null
    var hasResponse: Boolean = false
    clone.execute(
        object : ApolloSubscriptionCall.Callback<T> {
            override fun onConnected() {
            }
 
            override fun onResponse(response: Response<T>) {
                runCatching {
                    channel.offer(response)
                    hasResponse = true
                    if (pendingException != null) {
                        channel.close(pendingException)
                    }
                }
            }
 
            override fun onFailure(e: ApolloException) {
                pendingException = e
                if (hasResponse) {
                    channel.close(e)
                }
            }
 
            override fun onCompleted() {
                channel.close()
            }
 
            override fun onTerminated() {
                channel.close()
            }
        }
    )
    awaitClose { clone.cancel() }
}
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎