Skip to content

Instantly share code, notes, and snippets.

View SrGaabriel's full-sized avatar
🏠
Working from home

Gabriel SrGaabriel

🏠
Working from home
View GitHub Profile
@SrGaabriel
SrGaabriel / SendChannelHeartbeat.kt
Created February 6, 2022 22:29
A simple alternative to Ktor's default heartbeat method
@OptIn(ObsoleteCoroutinesApi::class)
public suspend fun <T> SendChannel<T>.startHeartbeat(
scope: CoroutineScope,
intervalMillis: Long,
delayMillis: Long,
content: T
): Job = scope.launch {
val channel = ticker(intervalMillis, delayMillis)
channel.consumeEach {
this@startHeartbeat.send(content)