diff --git a/CHANGELOG.md b/CHANGELOG.md index be4bc0d1..f9f2e57c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,12 +1,30 @@ # Change Log -## [Unreleased] - TBD +## [0.8.1] - TBD -### Fixed +### Changed + +- Update dependencies: + - [Kotlin `1.9.24`](https://github.com/JetBrains/kotlin/releases/tag/v1.9.24). + - [KotlinX Coroutines `1.8.1`](https://github.com/Kotlin/kotlinx.coroutines/releases/tag/1.8.1). + - `Gradle` to `8.7`. + +### Added + +- Add `Flow.catchAndReturn`, `Flow.catchAndResume` operators. +- Add `Flow.mapToResult`, `Flow.mapResultCatching`, `Flow.throwFailure` operators. + +### Changed + +- `Flow.chunked(bufferSize: Int)` / `Flow.bufferCount(bufferSize: Int)`: reduce unnecessary allocations. + +##### Changelog relative to version [0.8.1-Beta] - `Flow.mapResultCatching` now does not catch `CancellationException` thrown from the `transform` lambda. +- [Kotlin `1.9.24`](https://github.com/JetBrains/kotlin/releases/tag/v1.9.24). + ## [0.8.1-Beta] - Mar 23, 2024 ### Changed diff --git a/README.md b/README.md index 5de131c8..70239772 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,8 @@ [![Build](https://github.com/hoc081098/FlowExt/actions/workflows/build.yml/badge.svg)](https://github.com/hoc081098/FlowExt/actions/workflows/build.yml) [![Validate Gradle Wrapper](https://github.com/hoc081098/FlowExt/actions/workflows/gradle-wrapper-validation.yml/badge.svg)](https://github.com/hoc081098/FlowExt/actions/workflows/gradle-wrapper-validation.yml) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) -[![Kotlin version](https://img.shields.io/badge/Kotlin-1.9.22-blueviolet?logo=kotlin&logoColor=white)](http://kotlinlang.org) -[![KotlinX Coroutines version](https://img.shields.io/badge/Kotlinx_Coroutines-1.8.0-blueviolet?logo=kotlin&logoColor=white)](https://github.com/Kotlin/kotlinx.coroutines/releases/tag/1.8.0) +[![Kotlin version](https://img.shields.io/badge/Kotlin-1.9.24-blueviolet?logo=kotlin&logoColor=white)](http://kotlinlang.org) +[![KotlinX Coroutines version](https://img.shields.io/badge/Kotlinx_Coroutines-1.8.1-blueviolet?logo=kotlin&logoColor=white)](https://github.com/Kotlin/kotlinx.coroutines/releases/tag/1.8.1) ![badge][badge-android] ![badge][badge-wearos] ![badge][badge-android-native] diff --git a/src/commonMain/kotlin/com/hoc081098/flowext/bufferCount.kt b/src/commonMain/kotlin/com/hoc081098/flowext/bufferCount.kt index ed21a78a..1b9a9fcc 100644 --- a/src/commonMain/kotlin/com/hoc081098/flowext/bufferCount.kt +++ b/src/commonMain/kotlin/com/hoc081098/flowext/bufferCount.kt @@ -118,20 +118,19 @@ private fun Flow.bufferSkip(bufferSize: Int, skip: Int): Flow> { private fun Flow.bufferExact(bufferSize: Int): Flow> { return flow { - var buffer: MutableList = mutableListOf() + var buffer: MutableList? = null collect { element -> - buffer += element + val b = buffer ?: mutableListOf().also { buffer = it } + b += element - if (buffer.size >= bufferSize) { - emit(buffer) - buffer = mutableListOf() + if (b.size >= bufferSize) { + emit(b) + buffer = null } } // Emits remaining buffer - if (buffer.isNotEmpty()) { - emit(buffer) - } + buffer?.let { emit(it) } } } diff --git a/src/commonTest/kotlin/com/hoc081098/flowext/BufferCountTest.kt b/src/commonTest/kotlin/com/hoc081098/flowext/BufferCountTest.kt index 51327cb4..c6d6c0cd 100644 --- a/src/commonTest/kotlin/com/hoc081098/flowext/BufferCountTest.kt +++ b/src/commonTest/kotlin/com/hoc081098/flowext/BufferCountTest.kt @@ -56,6 +56,31 @@ class BufferCountTest : BaseTest() { Event.Complete, ), ) + + range(0, 10) + .bufferCount(1) + .test((0..9).map { Event.Value(listOf(it)) } + Event.Complete) + + range(0, 10) + .bufferCount(3) + .test( + listOf( + Event.Value(listOf(0, 1, 2)), + Event.Value(listOf(3, 4, 5)), + Event.Value(listOf(6, 7, 8)), + Event.Value(listOf(9)), + Event.Complete, + ), + ) + + range(0, 10) + .bufferCount(11) + .test( + listOf( + Event.Value((0..9).toList()), + Event.Complete, + ), + ) } @Test