|
| 1 | +package dev.silenium.libs.flows.examples |
| 2 | + |
| 3 | +import dev.silenium.libs.flows.api.FlowItem |
| 4 | +import dev.silenium.libs.flows.api.Sink |
| 5 | +import dev.silenium.libs.flows.api.Transformer |
| 6 | +import dev.silenium.libs.flows.base.SourceBase |
| 7 | +import dev.silenium.libs.flows.impl.FlowGraph |
| 8 | +import kotlinx.coroutines.Dispatchers |
| 9 | +import kotlinx.coroutines.runBlocking |
| 10 | + |
| 11 | +data class MyData(val value: Int) |
| 12 | +data class MyMetadata(val negativeNumbers: Boolean) |
| 13 | + |
| 14 | +// A simple source that publishes numbers from 0 to 100 to two different output pads |
| 15 | +class MySource : SourceBase<MyData, MyMetadata>() { |
| 16 | + init { |
| 17 | + // Create two output pads, one for positive numbers and one for negative numbers |
| 18 | + metadata[0u] = MyMetadata(false) |
| 19 | + metadata[1u] = MyMetadata(true) |
| 20 | + } |
| 21 | + |
| 22 | + // Publish numbers from 0 to 100 |
| 23 | + // Publishing can be done from any thread/coroutine, as the flow is thread-safe |
| 24 | + suspend fun run() { |
| 25 | + for (i in 0..100) { |
| 26 | + if (i % 2 == 0) { |
| 27 | + publish(FlowItem(0u, metadata[0u]!!, MyData(i))) |
| 28 | + } else { |
| 29 | + publish(FlowItem(1u, metadata[1u]!!, MyData(i))) |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// A simple processor that doubles the value of the input data |
| 36 | +class MyTransformer : Transformer<MyData, MyMetadata, MyData, MyMetadata>, SourceBase<MyData, MyMetadata>() { |
| 37 | + private val inputMetadata_ = mutableMapOf<UInt, MyMetadata?>() |
| 38 | + override val inputMetadata: Map<UInt, MyMetadata?> get() = inputMetadata_ |
| 39 | + |
| 40 | + override fun configure(pad: UInt, metadata: MyMetadata): Result<Unit> { |
| 41 | + inputMetadata_[pad] = metadata |
| 42 | + this.metadata[pad] = metadata |
| 43 | + return Result.success(Unit) |
| 44 | + } |
| 45 | + |
| 46 | + override suspend fun receive(item: FlowItem<MyData, MyMetadata>): Result<Unit> = runCatching { |
| 47 | + publish(FlowItem(item.pad, item.metadata, MyData(item.value.value * 2))) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// A simple sink that prints the received data |
| 52 | +class MySink : Sink<MyData, MyMetadata> { |
| 53 | + private val inputMetadata_ = mutableMapOf<UInt, MyMetadata?>() |
| 54 | + override val inputMetadata: Map<UInt, MyMetadata?> get() = inputMetadata_ |
| 55 | + |
| 56 | + override suspend fun receive(item: FlowItem<MyData, MyMetadata>): Result<Unit> = runCatching { |
| 57 | + println("Received $item") |
| 58 | + } |
| 59 | + |
| 60 | + override fun close() { |
| 61 | + println("Sink closed") |
| 62 | + } |
| 63 | + |
| 64 | + override fun configure(pad: UInt, metadata: MyMetadata): Result<Unit> { |
| 65 | + inputMetadata_[pad] = metadata |
| 66 | + return Result.success(Unit) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +fun main() = runBlocking { |
| 71 | + val graph = FlowGraph(Dispatchers.Default) { |
| 72 | + val source = source(MySource(), "source") |
| 73 | + val processor = transformer(MyTransformer(), "transformer") |
| 74 | + val sink = sink(MySink(), "sink") |
| 75 | + |
| 76 | + source.connectTo(processor).getOrThrow() |
| 77 | + processor.connectTo(sink).getOrThrow() |
| 78 | + } |
| 79 | + graph.source<MySource>("source")!!.impl.run() |
| 80 | + graph.close() |
| 81 | +} |
0 commit comments