io.provenance.eventstream.stream.consumers.EventStreamViewer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of es-core Show documentation
Show all versions of es-core Show documentation
A collection of libraries to connect and stream blocks from a node
The newest version!
package io.provenance.eventstream.stream.consumers
import io.provenance.eventstream.stream.BlockStreamFactory
import io.provenance.eventstream.stream.BlockStreamOptions
import io.provenance.eventstream.stream.EventStream
import io.provenance.eventstream.stream.models.StreamBlock
import io.provenance.eventstream.stream.models.StreamBlockImpl
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import mu.KotlinLogging
/**
* An event stream consumer that displays blocks from the provided event stream.
*
* @param eventStream The event stream which provides blocks to this consumer.
* @param options Options used to configure this consumer.
*/
@OptIn(FlowPreview::class)
@ExperimentalCoroutinesApi
class EventStreamViewer(
private val eventStream: EventStream,
private val options: BlockStreamOptions
) {
constructor(
eventStreamFactory: BlockStreamFactory,
options: BlockStreamOptions
) : this(eventStreamFactory.createSource(options) as EventStream, options)
private val log = KotlinLogging.logger { }
private fun onError(error: Throwable) {
log.error("$error")
}
suspend fun consume(error: (Throwable) -> Unit = ::onError, ok: (block: StreamBlock) -> Unit) {
consume(error) { b, _ -> ok(b) }
}
suspend fun consume(
error: (Throwable) -> Unit = ::onError,
ok: (block: StreamBlock, serialize: (StreamBlockImpl) -> String) -> Unit
) {
eventStream.streamBlocks()
.buffer()
.catch { error(it) }
.collect { ok(it, eventStream.serializer) }
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy