Skip to content
This repository was archived by the owner on Feb 13, 2020. It is now read-only.
This repository was archived by the owner on Feb 13, 2020. It is now read-only.

Auto batching #118

Description

@jcornaz

Implementation example:

fun <T : Any> ReceiveChannel<T>.chunkedAuto(max: Int = Int.MAX_VALUE): ReceiveChannel<List<T>> = transform { input, output ->
  require(max > 0) { "Invalid chunk auto max argument: $max (should be > 0)" }

  var chunk: MutableList<T>

  while (isActive) {
    chunk = arrayListOf(input.receiveOrNull() ?: return@transform)

    while (chunk.size < max) {
      input.poll()?.let(chunk::add) ?: break
    }

    output.send(chunk)
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureAdd a new feature

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions