Skip to content

Commit

Permalink
docs(defer)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoc081098 committed May 20, 2022
1 parent e86f6ad commit 6a87141
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/commonMain/kotlin/com/hoc081098/flowext/defer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@
package com.hoc081098.flowext

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.flow

/**
* Creates a [Flow] that, on collection, calls a [Flow] factory to make a [Flow] for each new [FlowCollector].
*
* In some circumstances, waiting until the last minute (that is, until collection time)
* to generate the [Flow] can ensure that collectors receive the freshest data.
*/
public fun <T> defer(flowFactory: suspend () -> Flow<T>): Flow<T> = flow { emitAll(flowFactory()) }

0 comments on commit 6a87141

Please sign in to comment.