Add cold flow examples#230
Conversation
WalkthroughAdds cold coroutine flows for search suggestions and stock prices with implementations and tests; updates editor import settings, CI step (Codacy reporter commented), README badges, and .gitignore/local.properties handling. Changes
Sequence Diagram(s)sequenceDiagram
participant Input as Query Flow
participant SM as SearchManager
participant API as SearchApi
participant Output as Suggestions Flow
Input->>SM: emit query
SM->>SM: debounce (300ms)
SM->>SM: filter blank
SM->>SM: distinctUntilChanged
SM->>API: search(query)
API-->>SM: List<String>
SM->>Output: emit suggestions
Note over SM: on error → emit empty list
sequenceDiagram
participant Ticker as tickerFlow
participant SR as StockRepository
participant API as StockApi
participant Output as Price Flow
loop every refreshInterval
Ticker->>SR: emit Unit
SR->>API: fetchPrice(symbol)
API-->>SR: Double (price)
SR->>Output: emit price
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In
`@src/main/kotlin/dev/shtanko/concurrency/coroutines/flow/cold/SearchManager.kt`:
- Around line 22-25: The current placement of catch after flatMapLatest causes
per-query errors to potentially terminate the outer flow; move the error
handling inside the flatMapLatest lambda so each API call is wrapped with its
own try-recover: replace the inner mapping that uses flow {
emit(api.search(query)) } so that the flow produced per query handles exceptions
(catch or try/catch) and emits emptyList() on failure; modify the block around
flatMapLatest/flow { emit(api.search(query)) } to catch exceptions from
api.search(query) locally rather than relying on the outer .catch.
🧹 Nitpick comments (2)
src/main/kotlin/dev/shtanko/concurrency/coroutines/flow/cold/StockApi.kt (2)
12-17: Consider validating theintervalparameter.If
intervalis zero or negative, this could lead to unexpected behavior—either a tight loop with no delay or undefineddelay()behavior. Adding arequirecheck would make the function more robust.♻️ Suggested validation
-fun tickerFlow(interval: Long) = flow { +fun tickerFlow(interval: Long): Flow<Unit> { + require(interval > 0) { "Interval must be positive, was $interval" } + return flow { while (true) { emit(Unit) delay(interval) } } +}
19-25: Consider adding error handling for resilience.If
api.fetchPricethrows an exception, the flow will terminate. For a continuous price stream, you might want to catch and handle errors to keep the flow alive. Since this is an example file, this may be intentional for simplicity.♻️ Optional: Add retry/catch for resilience
+import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.retry + class StockRepository( private val api: StockApi, private val refreshInterval: Long = 5000L, ) { fun getStockPrice(symbol: String): Flow<Double> = - tickerFlow(refreshInterval).map { api.fetchPrice(symbol) } + tickerFlow(refreshInterval) + .map { api.fetchPrice(symbol) } + .retry(3) { e -> e is IOException }
…earchManager.kt Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
|


Summary by CodeRabbit
New Features
Documentation
Tests
Chores
Style
✏️ Tip: You can customize this high-level summary in your review settings.