Skip to content

Instantly share code, notes, and snippets.

@SrGaabriel
Created February 6, 2022 22:29
Show Gist options
  • Save SrGaabriel/dc136b80119f340ee7a5dfdf542c98bd to your computer and use it in GitHub Desktop.
Save SrGaabriel/dc136b80119f340ee7a5dfdf542c98bd to your computer and use it in GitHub Desktop.
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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment