Skip to content

Commit 3758c62

Browse files
committed
feat(chunked): add Flow.chunked as an alias to Flow.bufferCount.
Related issues: - Kotlin/kotlinx.coroutines#1290 - Kotlin/kotlinx.coroutines#902 - Kotlin/kotlinx.coroutines#1558 - Kotlin/kotlinx.coroutines#2378
1 parent c626d2c commit 3758c62

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# Change Log
22

3+
## [Unreleased] - TODO
4+
5+
### Changed
6+
7+
- Update dependencies
8+
- `Kotlin` to `1.9.10`.
9+
- `Gradle` to `8.4`.
10+
11+
### Added
12+
13+
- Add `Flow.chunked` operator, it is an alias to `Flow.bufferCount` operator.
14+
315
## [0.7.2] - Oct 7, 2023
416

517
### Changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,12 @@ dependencies {
120120
- [`timer`](#timer)
121121

122122
- Intermediate operators
123-
- [`bufferCount`](#buffercount)
123+
- [`bufferCount`](#buffercount--chunked)
124124
- [`combine`](#combine)
125125
- [`cast`](#cast--castnotnull--castnullable--safeCast)
126126
- [`castNotNull`](#cast--castnotnull--castnullable--safeCast)
127127
- [`castNullable`](#cast--castnotnull--castnullable--safeCast)
128+
- [`chunked`](#buffercount--chunked)
128129
- [`safeCast`](#cast--castnotnull--castnullable--safeCast)
129130
- [`concatWith`](#concatwith)
130131
- [`startWith`](#startwith)
@@ -157,13 +158,14 @@ dependencies {
157158
- [`throttleTime`](#throttletime)
158159
- [`withLatestFrom`](#withlatestfrom)
159160

160-
#### bufferCount
161+
#### bufferCount / chunked
161162

162163
- Similar to [RxJS bufferCount](https://rxjs.dev/api/operators/bufferCount)
163164
- Similar
164165
to [RxJava buffer](http://reactivex.io/RxJava/3.x/javadoc/io/reactivex/rxjava3/core/Observable.html#buffer-int-int-)
165166

166167
Buffers the source `Flow` values until the size hits the maximum `bufferSize` given.
168+
Note, `chunked` is an alias to `bufferCount`.
167169

168170
```kotlin
169171
range(start = 0, count = 10)

api/FlowExt.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
public final class com/hoc081098/flowext/BufferCountKt {
22
public static final fun bufferCount (Lkotlinx/coroutines/flow/Flow;I)Lkotlinx/coroutines/flow/Flow;
33
public static final fun bufferCount (Lkotlinx/coroutines/flow/Flow;II)Lkotlinx/coroutines/flow/Flow;
4+
public static final fun chunked (Lkotlinx/coroutines/flow/Flow;I)Lkotlinx/coroutines/flow/Flow;
45
}
56

67
public final class com/hoc081098/flowext/CastKt {

src/commonMain/kotlin/com/hoc081098/flowext/bufferCount.kt

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,38 @@ package com.hoc081098.flowext
2727
import kotlinx.coroutines.flow.Flow
2828
import kotlinx.coroutines.flow.flow
2929

30+
/**
31+
* This function is an alias to [bufferCount] operator.
32+
*
33+
* @see bufferCount
34+
*/
35+
public fun <T> Flow<T>.chunked(bufferSize: Int): Flow<List<T>> = bufferCount(bufferSize)
36+
3037
/**
3138
* Buffers the source [Flow] values until the size hits the maximum [bufferSize] given.
39+
*
40+
* Returns a [Flow] that emits buffers of items it collects from the current [Flow].
41+
* It emits connected, non-overlapping buffers, each containing [bufferSize] items.
42+
* When the current [Flow] completes, the resulting [Flow] emits the current buffer
43+
* and propagates the complete event from the current [Flow].
44+
* Note that if the current [Flow] throws an exception,
45+
* that exception is passed on immediately without first emitting the buffer it is in the process of assembling.
46+
*
3247
* @param bufferSize The maximum size of the buffer emitted.
3348
*/
3449
public fun <T> Flow<T>.bufferCount(bufferSize: Int): Flow<List<T>> = bufferExact(bufferSize)
3550

3651
/**
37-
* Buffers the source [Flow] values until the size hits the maximum [bufferSize] given.
52+
* Buffers a number of values from the source [Flow] by [bufferSize]
53+
* then emits the buffer and clears it, and starts a new buffer each [startBufferEvery] values.
54+
*
55+
* When the current [Flow] completes, the resulting [Flow] emits active buffers
56+
* and propagates the complete event from the current [Flow].
57+
* Note that if the current [Flow] throws an exception,
58+
* that exception is passed on immediately without first emitting the buffer it is in the process of assembling.
3859
*
3960
* @param bufferSize The maximum size of the buffer emitted.
40-
* @param startBufferEvery Optional. Default is null.
41-
* Interval at which to start a new buffer.
61+
* @param startBufferEvery Interval at which to start a new buffer.
4262
* For example if [startBufferEvery] is 2, then a new buffer will be started on every other value from the source.
4363
* A new buffer is started at the beginning of the source by default.
4464
*/

0 commit comments

Comments
 (0)