com.lightningkite.lightningdb.Flow.ext.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of server-core Show documentation
Show all versions of server-core Show documentation
A set of tools to fill in/replace what Ktor is lacking in.
The newest version!
package com.lightningkite.lightningdb
import kotlinx.coroutines.flow.Flow
/**
* Will gather instances into a list of chunkSize before passing them into the action provided. If the flow contains less
* than chunkSize elements it will pass the remaining elements into action.
*
* @param chunkSize The size of list you wish to operate on in the action.
* @param action The action you wish to perform on a list of *Model* with size chunkSize
*/
suspend inline fun Flow.collectChunked(chunkSize: Int, crossinline action: suspend (List) -> Unit) {
val list = ArrayList()
this.collect {
list.add(it)
if (list.size >= chunkSize) {
action(list)
list.clear()
}
}
if(list.isNotEmpty()) action(list)
}