[译] Kotlin 协程 Flow 官方文档(2021-2-4)翻译

原文:https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/

Flow (流)

interface Flow<out T> (source)

异步数据流,它顺序地发出值并正常或异常地完成。

流的中间运算符(例如 map, filter, take, zip 等)是应用于一个或多个上游流并返回下游流(可以在其中应用其他运算符)的函数。中间运算符不会在流中执行任何代码,并且自身不会挂起函数。它们只是建立了一连串的操作链,以便在将来执行并迅速返回。这被称为冷流特性。

流上的终端运算符要么是挂起功能,如 collect, single, reduce, toList 等,要么是在给定的 scope 内开始收集流的 launchIn 运算符。它们将应用于上游流并触发所有操作的执行。流的执行也称为收集流,并且始终以挂起的方式执行而实际没有阻塞。终端运算符是正常完成还是异常完成,取决于上游中所有流操作的执行成功或者失败。最基本的终端运算符就是 collect,例如:

try {
    flow.collect { value ->
        println("Received $value")
    }
} catch (e: Exception) {
    println("The flow has thrown an exception: $e")
}

默认情况下,流是顺序的,并且所有流操作都在同一协程中按顺序执行,除了一些专门设计用于将并发引入流执行的操作(例如 buffer 和 flatMapMerge)外。 有关详细信息,请参见其文档。

Flow 接口不携带信息,即关于该流是可以重复收集并在每次收集时触发执行相同代码的冷流,还是每次从同一个运行源收集时发出不同值的热流。 通常,流代表冷流,但是有一个 SharedFlow 子类型代表热流。 除此之外,任何流都可以通过 stateIn 和 shareIn 运算符转换为热流,或者通过 produceIn 运算符将流转换为热通道。

转载请说明出处:https://blog.csdn.net/hegan2010/article/details/113662541

Flow builders (流构建器)

创建流有以下几种基本方法:

  • flowOf(…) 根据一组固定的值创建流的函数。
  • asFlow() 可以将各种类型转换为流的扩展函数。
  • flow { … } builder function to construct arbitrary flows from sequential calls to emit function. 
    构建器函数,用于构造从顺序调用到发出函数的任意流(原文有点难以理解,翻译不太通顺)。
  • channelFlow { … } builder function to construct arbitrary flows from potentially concurrent calls to the send function.
    构建器函数, 用于构造从潜在并发调用到 send 函数的任意流(原文有点难以理解,翻译不太通顺)。
  • MutableStateFlow and MutableSharedFlow 定义了相应的构造函数,以创建可直接更新的热流。

Flow constraints (流约束)

Flow 接口的所有实现都必须遵守以下详细描述的两个关键属性:

  • 上下文保存。
  • 异常透明性。

这些属性确保了对流执行代码本地推理的能力,并使代码模块化,从而可以将上游流发送器与下游流收集器分开开发。 流的用户不需要知道其使用的上游流的实现细节。

Context preservation (上下文保存)

流具有上下文保留属性:它封装了自己的执行上下文,并且从不向下游传播或泄漏它,因此使对特定转换或终端运算的执行上下文的推理变得微不足道。

仅有一种更改流上下文的方法:flowOn 运算符更改上游上下文(“ flowOn 运算符上方的所有内容”)。 有关其他信息,请参阅其文档。

这种推理可以在实例中得到示范:

val flowA = flowOf(1, 2, 3)
    .map { it + 1 } // Will be executed in ctxA
    .flowOn(ctxA) // Changes the upstream context: flowOf and map

// Now we have a context-preserving flow: it is executed somewhere but this information is encapsulated in the flow itself

val filtered = flowA // ctxA is encapsulated in flowA
   .filter { it == 3 } // Pure operator without a context yet

withContext(Dispatchers.Main) {
    // All non-encapsulated operators will be executed in Main: filter and single
    val result = filtered.single()
    myUi.text = result
}

从实现的角度来看,这意味着所有的流实现都应仅从同一个协程中发出值。 默认的流构建器有效地遵守了此约束。 如果流的实现未启动任何协程,则应使用流构建器。 它的实现可以避免大多数开发错误:

val myFlow = flow {
   // GlobalScope.launch { // is prohibited
   // launch(Dispatchers.IO) { // is prohibited
   // withContext(CoroutineName("myFlow")) // is prohibited
   emit(1) // OK
   coroutineScope {
       emit(2) // OK -- still the same coroutine
   }
}

如果要将流的收集和发出分成多个协程,请使用 channelFlow。 它封装了所有的上下文保存工作,并允许您专注于特定于域的问题,而不是不变的实现细节。 可以在 channelFlow 中使用协程构建器的任何组合。

如果您正寻求高性能,并确保不会并发发出值和发生上下文跳转,那么可以将流构建器与 coroutineScope 或 supervisorScope 一起使用:

  • Scoped primitive should be used to provide a CoroutineScope.
    使用 Scoped 原语提供 CoroutineScope。
  • 无论是 withContext(ctx) 还是构建器参数(例如 launch(ctx)),都禁止更改发出的上下文。
  • 允许从单独的上下文中收集另一个流,但是这与将 flowOn 运算符应用于该流具有相同的效果,而后者的效率更高。

Exception transparency (异常透明性)

流的实现永远不会捕获或者处理下游流中发生的异常。 从实现的角度来看,这意味着永远不要将对 emit 和 emitAll 的调用包裹在 try {...} catch {...} 块中。 流中的异常处理应由 catch 运算符执行,并且旨在仅捕获来自上游流的异常,而传递所有下游异常。 类似地,终端运算符如 collect 会抛出在其代码或上游流中发生的任何未处理的异常,例如:

flow { emitData() }
    .map { computeOne(it) }
    .catch { ... } // catches exceptions in emitData and computeOne
    .map { computeTwo(it) }
    .collect { process(it) } // throws exceptions from process and computeTwo

可以对 onCompletion 运算符应用相同的推理,这是对 finally 块的声明性替换。

不遵守异常透明性要求可能会导致奇怪的行为,使得代码难以推理,因为 collect {...} 中的异常可能以某种方式被上游流“捕获”,从而限制了对代码的本地推理的能力。

流机制在运行时强制执行异常透明性,如果在上一次尝试中已经引发了异常,则在任何尝试发出值时都会引发 IllegalStateException。

Reactive streams (反应式流)

Flow 符合 Reactive Streams,您可以使用 kotlinx-coroutines-reactive 模块中的 Flow.asPublisher 和 Publisher.asFlow 安全地将其与反应式流互操作。

Not stable for inheritance (继承不稳定)

Flow 接口对于第三方库中的继承不稳定, 因为将来可能会向该接口添加新方法,但在使用时会保持稳定。 使用 flow {...} 构建器函数创建一个实现。

Functions (函数)

collect

abstract suspend fun collect(
    collector: FlowCollector<T>
): Unit

Accepts the given collector and emits values into it. This method should never be implemented or used directly.

接受给定的收集器并向其中发出值。 永远不要实现或直接使用此方法。

Extension Functions (扩展函数)

broadcastIn

fun <T> Flow<T>.broadcastIn(
    scope: CoroutineScope,
    start: CoroutineStart = CoroutineStart.LAZY
): BroadcastChannel<T>

Creates a broadcast coroutine that collects the given flow.

创建收集指定流的广播协程。

buffer

fun <T> Flow<T>.buffer(
    capacity: Int = BUFFERED,
    onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND
): Flow<T>

Buffers flow emissions via channel of a specified capacity and runs collector in a separate coroutine.

缓冲流通过指定容量的通道进行发出,并在单独的协程中运行收集器。

fun <T> Flow<T>.buffer(capacity: Int = BUFFERED): Flow<T>

cache

fun <T> Flow<T>.cache(): Flow<T>

cancellable

fun <T> Flow<T>.cancellable(): Flow<T>

Returns a flow which checks cancellation status on each emission and throws the corresponding cancellation cause if flow collector was cancelled. Note that flow builder and all implementations of SharedFlow are cancellable by default.

返回一个流,该流检查每个发出的取消状态,如果取消了流收集器,则抛出相应的取消原因。 请注意,流构建器和 SharedFlow 的所有实现默认都是可以取消的。

catch

fun <T> Flow<T>.catch(
    action: suspend FlowCollector<T>.(cause: Throwable) -> Unit
): Flow<T>

Catches exceptions in the flow completion and calls a specified action with the caught exception. This operator is transparent to exceptions that occur in downstream flow and does not catch exceptions that are thrown to cancel the flow.

在流完成过程中捕获异常,对捕获的异常调用指定的操作。 该运算符对下游流中发生的异常透明,并且不会捕获为取消流而抛出的异常。

collect

suspend fun Flow<*>.collect(): Unit

Terminal flow operator that collects the given flow but ignores all emitted values. If any exception occurs during collect or in the provided flow, this exception is rethrown from this method.

流终端运算符,它收集给定的流但忽略所有发出的值。 如果在收集期间或者在提供的流中发生任何异常,则从此方法重新抛出此异常。

suspend fun <T> Flow<T>.collect(
    action: suspend (value: T) -> Unit
): Unit

Terminal flow operator that collects the given flow with a provided action. If any exception occurs during collect or in the provided flow, this exception is rethrown from this method.

流终端运算符,通过提供的 action 操作来收集给定的流。 如果在收集期间或者在提供的流中发生任何异常,则从此方法重新抛出此异常。

collectIndexed

suspend fun <T> Flow<T>.collectIndexed(
    action: suspend (index: Int, value: T) -> Unit
): Unit

Terminal flow operator that collects the given flow with a provided action that takes the index of an element (zero-based) and the element. If any exception occurs during collect or in the provided flow, this exception is rethrown from this method.

流终端运算符,它使用提供的 action 操作来收集给定的流,该操作采用元素的索引(从零开始)和对应的元素。 如果在收集期间或者在提供的流中发生任何异常,则从此方法重新抛出此异常。

collectLatest

suspend fun <T> Flow<T>.collectLatest(
    action: suspend (value: T) -> Unit
): Unit

Terminal flow operator that collects the given flow with a provided action. The crucial difference from collect is that when the original flow emits a new value, action block for previous value is cancelled.

流终端运算符,通过提供的 action 操作来收集给定的流。 与 collect 的关键区别在于,当原始流发出新值时,将取消先前值的操作块。

combine

fun <T1, T2, R> Flow<T1>.combine(
    flow: Flow<T2>,
    transform: suspend (a: T1, b: T2) -> R
): Flow<R>

Returns a Flow whose values are generated with transform function by combining the most recently emitted values by each flow.

返回一个流,其值是通过组合每个流最近发出的值并使用转换函数生成的。

combineLatest

fun <T1, T2, R> Flow<T1>.combineLatest(
    other: Flow<T2>,
    transform: suspend (T1T2) -> R
): Flow<R>

fun <T1, T2, T3, R> Flow<T1>.combineLatest(
    other: Flow<T2>,
    other2: Flow<T3>,
    transform: suspend (T1T2T3) -> R
): Flow<R>

fun <T1, T2, T3, T4, R> Flow<T1>.combineLatest(
    other: Flow<T2>,
    other2: Flow<T3>,
    other3: Flow<T4>,
    transform: suspend (T1T2T3T4) -> R
): Flow<R>

fun <T1, T2, T3, T4, T5, R> Flow<T1>.combineLatest(
    other: Flow<T2>,
    other2: Flow<T3>,
    other3: Flow<T4>,
    other4: Flow<T5>,
    transform: suspend (T1T2T3T4T5) -> R
): Flow<R>

combineTransform

fun <T1, T2, R> Flow<T1>.combineTransform(
    flow: Flow<T2>,
    transform: suspend FlowCollector<R>.(a: T1, b: T2) -> Unit
): Flow<R>

Returns a Flow whose values are generated by transform function that process the most recently emitted values by each flow.

返回一个流,其值由转换函数生成,该转换函数处理每个流最近发出的值。

conflate

fun <T> Flow<T>.conflate(): Flow<T>

Conflates flow emissions via conflated channel and runs collector in a separate coroutine. The effect of this is that emitter is never suspended due to a slow collector, but collector always gets the most recent value emitted.

通过合并通道合并流发出,并在单独的协程中运行收集器。 这样做的结果是,发射器永远不会由于收集器速度较慢而挂起,并且收集器总是获得最新的发出值。

count

suspend fun <T> Flow<T>.count(): Int

Returns the number of elements in this flow.

suspend fun <T> Flow<T>.count(
    predicate: suspend (T) -> Boolean
): Int

Returns the number of elements matching the given predicate.

返回与给定 predicate 相匹配的元素数量。

debounce

fun <T> Flow<T>.debounce(timeoutMillis: Long): Flow<T>

fun <T> Flow<T>.debounce(timeoutMillis: (T) -> Long): Flow<T>

Returns a flow that mirrors the original flow, but filters out values that are followed by the newer values within the given timeout. The latest value is always emitted.

fun <T> Flow<T>.debounce(timeout: <ERROR CLASS>): Flow<T>

fun <T> Flow<T>.debounce(
    timeout: (T) -> <ERROR CLASS>
): Flow<T>

Returns a flow that mirrors the original flow, but filters out values that are followed by the newer values within the given timeout. The latest value is always emitted.

返回一个镜像原始流的流,但在给定的超时时间内过滤掉超时开始时的值的之后值。 始终会发出最新值。

distinctUntilChanged

fun <T> Flow<T>.distinctUntilChanged(): Flow<T>

Returns flow where all subsequent repetitions of the same value are filtered out.

fun <T> Flow<T>.distinctUntilChanged(
    areEquivalent: (old: T, new: T) -> Boolean
): Flow<T>

Returns flow where all subsequent repetitions of the same value are filtered out, when compared with each other via the provided areEquivalent function.

当通过提供的 areEquivalent 函数相互比较时,返回过滤掉一个值的所有后续重复值的流。

distinctUntilChangedBy

fun <T, K> Flow<T>.distinctUntilChangedBy(
    keySelector: (T) -> K
): Flow<T>

Returns flow where all subsequent repetitions of the same key are filtered out, where key is extracted with keySelector function.

返回流,其中过滤掉使用 keySelector 函数提取出的键的所有后续重复键。

drop

fun <T> Flow<T>.drop(count: Int): Flow<T>

Returns a flow that ignores first count elements. Throws IllegalArgumentException if count is negative.

返回忽略开头 count 个元素的流。 如果 count 为负数,则抛出 IllegalArgumentException。

dropWhile

fun <T> Flow<T>.dropWhile(
    predicate: suspend (T) -> Boolean
): Flow<T>

Returns a flow containing all elements except first elements that satisfy the given predicate.

返回包含除满足给定 predicate 的开头元素之后的所有元素的流。

或翻译:返回包含不满足给定 predicate 的第一个元素之后(含)的所有元素的流。

filter

fun <T> Flow<T>.filter(
    predicate: suspend (T) -> Boolean
): Flow<T>

Returns a flow containing only values of the original flow that matches the given predicate.

返回仅包含原始流中与给定 predicate 相匹配的值的流。

filterIsInstance

fun <R> Flow<*>.filterIsInstance(): Flow<R>

Returns a flow containing only values that are instances of specified type R.

返回仅包含作为指定类型 R 的实例的值的流。

filterNot

fun <T> Flow<T>.filterNot(
    predicate: suspend (T) -> Boolean
): Flow<T>

Returns a flow containing only values of the original flow that do not match the given predicate.

返回仅包含原始流中与给定 predicate 不相匹配的值的流。

filterNotNull

fun <T : AnyFlow<T?>.filterNotNull(): Flow<T>

Returns a flow containing only values of the original flow that are not null.

返回仅包含原始流中非 null 值的流。

first

suspend fun <T> Flow<T>.first(): T

The terminal operator that returns the first element emitted by the flow and then cancels flow’s collection. Throws NoSuchElementException if the flow was empty.

此终端运算符返回流程发出的第一个元素,然后取消流的收集。 如果流为空,则抛出 NoSuchElementException。

suspend fun <T> Flow<T>.first(
    predicate: suspend (T) -> Boolean
): T

The terminal operator that returns the first element emitted by the flow matching the given predicate and then cancels flow’s collection. Throws NoSuchElementException if the flow has not contained elements matching the predicate.

此终端运算符返回流发出的与给定 predicate 相匹配的第一个元素,然后取消流的收集。 如果流中没有包含与 predicate 相匹配的元素,则抛出 NoSuchElementException。

firstOrNull

suspend fun <T> Flow<T>.firstOrNull(): T?

The terminal operator that returns the first element emitted by the flow and then cancels flow’s collection. Returns null if the flow was empty.

此终端运算符返回流程发出的第一个元素,然后取消流的收集。 如果流为空,则返回 null。

suspend fun <T> Flow<T>.firstOrNull(
    predicate: suspend (T) -> Boolean
): T?

The terminal operator that returns the first element emitted by the flow matching the given predicate and then cancels flow’s collection. Returns null if the flow did not contain an element matching the predicate.

此终端运算符返回流发出的与给定 predicate 相匹配的第一个元素,然后取消流的收集。 如果流中没有包含与 predicate 相匹配的元素,则返回 null。

flatMapConcat

fun <T, R> Flow<T>.flatMapConcat(
    transform: suspend (value: T) -> Flow<R>
): Flow<R>

Transforms elements emitted by the original flow by applying transform, that returns another flow, and then concatenating and flattening these flows.

通过应用转换函数来转换原始流发出的元素,返回另一个流,然后将这些流串联并展平。

flatMapLatest

fun <T, R> Flow<T>.flatMapLatest(
    transform: suspend (value: T) -> Flow<R>
): Flow<R>

Returns a flow that switches to a new flow produced by transform function every time the original flow emits a value. When the original flow emits a new value, the previous flow produced by transform block is cancelled.

每次原始流发出值时,返回一个流,该流为由转换函数产生的新流。 当原始流发出新值时,由转换块产生的先前流将被取消。

flatMapMerge

fun <T, R> Flow<T>.flatMapMerge(
    concurrency: Int = DEFAULT_CONCURRENCY,
    transform: suspend (value: T) -> Flow<R>
): Flow<R>

Transforms elements emitted by the original flow by applying transform, that returns another flow, and then merging and flattening these flows.

通过应用转换函数来转换原始流发出的元素,返回另一个流,然后将这些流合并并展平。

flattenConcat

fun <T> Flow<Flow<T>>.flattenConcat(): Flow<T>

Flattens the given flow of flows into a single flow in a sequentially manner, without interleaving nested flows. This method is conceptually identical to flattenMerge(concurrency = 1) but has faster implementation.

将给定的流以顺序的方式展平为单个流,而不会交错嵌套的流。 该方法在概念上与 flattenMerge(concurrency = 1) 相同,但是实现速度更快。

flattenMerge

fun <T> Flow<Flow<T>>.flattenMerge(
    concurrency: Int = DEFAULT_CONCURRENCY
): Flow<T>

Flattens the given flow of flows into a single flow with a concurrency limit on the number of concurrently collected flows.

将并发收集流,并发数限制在 concurrency,将给定流展平为单个流。

flowOn

fun <T> Flow<T>.flowOn(context: CoroutineContext): Flow<T>

Changes the context where this flow is executed to the given context. This operator is composable and affects only preceding operators that do not have its own context. This operator is context preserving: context does not leak into the downstream flow.

将执行此流的上下文更改为给定的上下文。 该运算符是可组合的,并且仅影响没有自己上下文的先前运算符。 此运算符是上下文保留的:上下文不会泄漏到下游流中。

flowWith

fun <T, R> Flow<T>.flowWith(
    flowContext: CoroutineContext,
    bufferSize: Int = BUFFERED,
    builder: Flow<T>.() -> Flow<R>
): Flow<R>

The operator that changes the context where all transformations applied to the given flow within a builder are executed. This operator is context preserving and does not affect the context of the preceding and subsequent operations.

更改上下文的运算符,在该上下文中将执行在构建器中应用于给定流的所有转换。 此运算符是上下文保留的,并且不影响先前和后续运算符的上下文。

fold

suspend fun <T, R> Flow<T>.fold(
    initial: R,
    operation: suspend (acc: R, value: T) -> R
): R

Accumulates value starting with initial value and applying operation current accumulator value and each element

从初始值开始累加值,然后对当前累加器值和每个元素应用 operation 操作

launchIn

fun <T> Flow<T>.launchIn(scope: CoroutineScope): Job

Terminal flow operator that launches the collection of the given flow in the scope. It is a shorthand for scope.launch { flow.collect() }.

流终端运算符,可在 scope 内启动给定流的收集。 它是 scope.launch { flow.collect() } 的简写。

map

fun <T, R> Flow<T>.map(
    transform: suspend (value: T) -> R
): Flow<R>

Returns a flow containing the results of applying the given transform function to each value of the original flow.

返回包含对原始流中的每个值应用给定的转换函数的结果的流。

mapLatest

fun <T, R> Flow<T>.mapLatest(
    transform: suspend (value: T) -> R
): Flow<R>

Returns a flow that emits elements from the original flow transformed by transform function. When the original flow emits a new value, computation of the transform block for previous value is cancelled.

返回一个流,该流发出从原始流中的元素通过 transform 函数转换得到的元素。 当原始流发出新值时,将取消对先前值的转换块的计算。

mapNotNull

fun <T, R : AnyFlow<T>.mapNotNull(
    transform: suspend (value: T) -> R?
): Flow<R>

Returns a flow that contains only non-null results of applying the given transform function to each value of the original flow.

返回仅包含将给定的转换函数应用于原始流中的每个值的非 null 结果的流。

onCompletion

fun <T> Flow<T>.onCompletion(
    action: suspend FlowCollector<T>.(cause: Throwable?) -> Unit
): Flow<T>

Returns a flow that invokes the given action after the flow is completed or cancelled, passing the cancellation exception or failure as cause parameter of action.

返回在流完成或取消后调用给定的 action 操作的流程,并将取消异常或者失败异常作为 action 操作的 cause 参数传递。

onEach

fun <T> Flow<T>.onEach(action: suspend (T) -> Unit): Flow<T>

Returns a flow that invokes the given action before each value of the upstream flow is emitted downstream.

返回在上游流向下游流发出每个值之前调用给定的 action 操作的流。

onEmpty

fun <T> Flow<T>.onEmpty(
    action: suspend FlowCollector<T>.() -> Unit
): Flow<T>

Invokes the given action when this flow completes without emitting any elements. The receiver of the action is FlowCollector, so onEmpty can emit additional elements. For example:

在此流完成却没有发出任何元素时调用给定的 action 操作。 action 的接收者是 FlowCollector,因此 onEmpty 可以发出其他元素。 例如:

onStart

fun <T> Flow<T>.onStart(
    action: suspend FlowCollector<T>.() -> Unit
): Flow<T>

Returns a flow that invokes the given action before this flow starts to be collected.

返回在开始收集该流之前调用给定的 action 操作的流。

produceIn

fun <T> Flow<T>.produceIn(
    scope: CoroutineScope
): ReceiveChannel<T>

Creates a produce coroutine that collects the given flow.

创建一个 produce 协程来收集给定的流。

publish

fun <T> Flow<T>.publish(): Flow<T>

fun <T> Flow<T>.publish(bufferSize: Int): Flow<T>

reduce

suspend fun <S, T : SFlow<T>.reduce(
    operation: suspend (accumulator: S, value: T) -> S
): S

Accumulates value starting with the first element and applying operation to current accumulator value and each element. Throws NoSuchElementException if flow was empty.

从第一个元素开始累加值,然后对当前累加器值与每个元素应用 operation 运算。 如果流为空,则抛出 NoSuchElementException。

replay

fun <T> Flow<T>.replay(): Flow<T>

fun <T> Flow<T>.replay(bufferSize: Int): Flow<T>

retry

fun <T> Flow<T>.retry(
    retries: Long = Long.MAX_VALUE,
    predicate: suspend (cause: Throwable) -> Boolean = { true }
): Flow<T>

Retries collection of the given flow up to retries times when an exception that matches the given predicate occurs in the upstream flow. This operator is transparent to exceptions that occur in downstream flow and does not retry on exceptions that are thrown to cancel the flow.

当上游流中出现与给定 predicate 相匹配的异常时,重试收集给定的流,直到 retries 重试次数为止。 此运算符对下游流中发生的异常透明,并且不会在为取消流而抛出异常时进行重试。

fun <T> Flow<T>.retry(
    retries: Int = Int.MAX_VALUE,
    predicate: (Throwable) -> Boolean = { true }
): Flow<T>

retryWhen

fun <T> Flow<T>.retryWhen(
    predicate: suspend FlowCollector<T>.(cause: Throwable, attempt: Long) -> Boolean
): Flow<T>

Retries collection of the given flow when an exception occurs in the upstream flow and the predicate returns true. The predicate also receives an attempt number as parameter, starting from zero on the initial call. This operator is transparent to exceptions that occur in downstream flow and does not retry on exceptions that are thrown to cancel the flow.

当上游流中发生异常并且 predicate 返回 true 时,重试收集给定的流。 predicate 还会收到一个尝试数作为参数,从首次调用为零开始。 此运算符对下游流中发生的异常透明,并且不会在为取消流而抛出异常时进行重试。

runningReduce

fun <T> Flow<T>.runningReduce(
    operation: suspend (accumulator: T, value: T) -> T
): Flow<T>

Reduces the given flow with operation, emitting every intermediate result, including initial value. The first element is taken as initial value for operation accumulator. This operator has a sibling with initial value – scan.

通过 operation 减少给定的流,发出每个中间结果值,包括初始值。 第一个元素被用作 operation 累加器的初始值。 该运算符有一个带有初始值的兄弟运算符 - scan。

sample

fun <T> Flow<T>.sample(periodMillis: Long): Flow<T>

Returns a flow that emits only the latest value emitted by the original flow during the given sampling period.

返回在给定采样期间内只发出原始流的最新值的流。

fun <T> Flow<T>.sample(period: <ERROR CLASS>): Flow<T>

Returns a flow that emits only the latest value emitted by the original flow during the given sampling period.

返回在给定采样期间内只发出原始流的最新值的流。

scan

fun <T, R> Flow<T>.scan(
    initial: R,
    operation: suspend (accumulator: R, value: T) -> R
): Flow<R>

Folds the given flow with operation, emitting every intermediate result, including initial value. Note that initial value should be immutable (or should not be mutated) as it is shared between different collectors. For example:

通过 operation 折叠给定的流,发出每个中间结果值,包括初始值。 请注意,初始值应该是不可变的(或不应改变),因为它在不同的收集器之间共享。 例如:

scanReduce

fun <T> Flow<T>.scanReduce(
    operation: suspend (accumulator: T, value: T) -> T
): Flow<T>

shareIn

fun <T> Flow<T>.shareIn(
    scope: CoroutineScope,
    started: SharingStarted,
    replay: Int = 0
): SharedFlow<T>

Converts a cold Flow into a hot SharedFlow that is started in the given coroutine scope, sharing emissions from a single running instance of the upstream flow with multiple downstream subscribers, and replaying a specified number of replay values to new subscribers. See the SharedFlow documentation for the general concepts of shared flows.

将冷流转换为在给定协程 scope 内启动的热 SharedFlow,与多个下游订阅者共享上游流的单个运行实例的发出值,并向新订阅者重播指定数量的重播值。 有关共享流的一般概念,请参见 SharedFlow 文档。

single

suspend fun <T> Flow<T>.single(): T

The terminal operator that awaits for one and only one value to be emitted. Throws NoSuchElementException for empty flow and IllegalStateException for flow that contains more than one element.

等待一个值并且仅有一个被发送值的终端运算符。 对于空流将引发 NoSuchElementException,对于包含多个元素的流将引发 IllegalStateException。

singleOrNull

suspend fun <T> Flow<T>.singleOrNull(): T?

The terminal operator that awaits for one and only one value to be emitted. Returns the single value or null, if the flow was empty or emitted more than one value.

等待一个值并且仅有一个被发送值的终端运算符。 如果流为空或发出了多个值,则返回单个值或者 null。

stateIn

fun <T> Flow<T>.stateIn(
    scope: CoroutineScope,
    started: SharingStarted,
    initialValue: T
): StateFlow<T>

Converts a cold Flow into a hot StateFlow that is started in the given coroutine scope, sharing the most recently emitted value from a single running instance of the upstream flow with multiple downstream subscribers. See the StateFlow documentation for the general concepts of state flows.

将冷流转换为在给定协程 scope 内启动的热 StateFlow,并与多个下游订阅者共享上游流的单个运行实例中最新发出的值。 有关状态流的一般概念,请参见 StateFlow 文档。

suspend fun <T> Flow<T>.stateIn(
    scope: CoroutineScope
): StateFlow<T>

Starts the upstream flow in a given scope, suspends until the first value is emitted, and returns a hot StateFlow of future emissions, sharing the most recently emitted value from this running instance of the upstream flow with multiple downstream subscribers. See the StateFlow documentation for the general concepts of state flows.

在给定的协程 scope 内启动上游流,挂起直到发出第一个值,然后返回将来发出值的热 StateFlow,并与多个下游订阅者共享上游流的此运行实例的最新发出的值。 有关状态流的一般概念,请参见 StateFlow 文档。

switchMap

fun <T, R> Flow<T>.switchMap(
    transform: suspend (value: T) -> Flow<R>
): Flow<R>

take

fun <T> Flow<T>.take(count: Int): Flow<T>

Returns a flow that contains first count elements. When count elements are consumed, the original flow is cancelled. Throws IllegalArgumentException if count is not positive.

返回包含开头 count 个元素的流。 当 count 个元素被消费完后,原始流将被取消。 如果 count 不是正数,则抛出 IllegalArgumentException。

takeWhile

fun <T> Flow<T>.takeWhile(
    predicate: suspend (T) -> Boolean
): Flow<T>

Returns a flow that contains first elements satisfying the given predicate.

返回包含满足给定 predicate 的开头元素的流。

toCollection

suspend fun <T, C : MutableCollection<in T>> Flow<T>.toCollection(
    destination: C
): C

Collects given flow into a destination

将给定的流收集到一个 Collection。

toList

suspend fun <T> Flow<T>.toList(
    destination: MutableList<T> = ArrayList()
): List<T>

Collects given flow into a destination

将给定的流收集到一个 List。

toSet

suspend fun <T> Flow<T>.toSet(
    destination: MutableSet<T> = LinkedHashSet()
): Set<T>

Collects given flow into a destination

将给定的流收集到一个 Set。

transform

fun <T, R> Flow<T>.transform(
    transform: suspend FlowCollector<R>.(value: T) -> Unit
): Flow<R>

Applies transform function to each value of the given flow.

将转换函数应用于给定流的每个值。

transformLatest

fun <T, R> Flow<T>.transformLatest(
    transform: suspend FlowCollector<R>.(value: T) -> Unit
): Flow<R>

Returns a flow that produces element by transform function every time the original flow emits a value. When the original flow emits a new value, the previous transform block is cancelled, thus the name transformLatest.

每次原始流发出一个值时,返回一个通过转换函数产生元素的流。 当原始流发出新值时,先前的转换块将被取消,因此名称为 TransformLatest。

transformWhile

fun <T, R> Flow<T>.transformWhile(
    transform: suspend FlowCollector<R>.(value: T) -> Boolean
): Flow<R>

Applies transform function to each value of the given flow while this function returns true.

只要转换函数返回 true 时,对于给定流的每个值应用转换函数(直到返回 false)。

withIndex

fun <T> Flow<T>.withIndex(): Flow<IndexedValue<T>>

Returns a flow that wraps each element into IndexedValue, containing value and its index (starting from zero).

返回一个将每个元素包装到 IndexedValue 中的流,其中包含值及其索引(从零开始)。

zip

fun <T1, T2, R> Flow<T1>.zip(
    other: Flow<T2>,
    transform: suspend (T1T2) -> R
): Flow<R>

Zips values from the current flow (this) with other flow using provided transform function applied to each pair of values. The resulting flow completes as soon as one of the flows completes and cancel is called on the remaining flow.

通过提供的转换函数将当前流(this)与其他流进行压缩,将转换函数应用于每对值。 一旦其中一个流完成,结果流就完成,并在剩余流上调用 cancel。

Inheritors (继承类)

AbstractFlow

abstract class AbstractFlow<T> : Flow<T>, CancellableFlow<T>

Base class for stateful implementations of Flow. It tracks all the properties required for context preservation and throws an IllegalStateException if any of the properties are violated.

流的状态实现的基类。 它会跟踪上下文保存所需的所有属性,如果违反任何属性,则抛出一个 IllegalStateException。

SharedFlow

interface SharedFlow<out T> : Flow<T>

hot Flow that shares emitted values among all its collectors in a broadcast fashion, so that all collectors get all emitted values. A shared flow is called hot because its active instance exists independently of the presence of collectors. This is opposed to a regular Flow, such as defined by the flow { ... } function, which is cold and is started separately for each collector.

一个热流,它以广播的方式在它的所有收集器之间共享发出值,以使所有的收集器都获得所有发出值。 共享流之所以称为热流,是因为其活跃实例独立于收集器的存在而存在。 这与普通流相反,常规流如 flow {...} 函数所定义,该流是冷流并针对每个收集器单独启动。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值