|
| 1 | +/* |
| 2 | + * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +@file:JvmMultifileClass |
| 6 | +@file:JvmName("FlowKt") |
| 7 | + |
| 8 | +package kotlinx.coroutines.flow |
| 9 | + |
| 10 | +import kotlinx.coroutines.* |
| 11 | +import kotlinx.coroutines.channels.* |
| 12 | +import kotlinx.coroutines.flow.internal.* |
| 13 | +import kotlin.jvm.* |
| 14 | + |
| 15 | +/** |
| 16 | + * A "cached" [Flow] which will record the last [history] collected values. |
| 17 | + * |
| 18 | + * When a collector begins collecting after values have already been recorded, |
| 19 | + * those values will be collected *before* values from the receiver [Flow] are collected. |
| 20 | + * |
| 21 | + * example: |
| 22 | + * ```Kotlin |
| 23 | + * val ints = flowOf(1, 2, 3, 4).cache(2) // cache the last 2 values |
| 24 | + * |
| 25 | + * ints.take(4).collect { ... } // 4 values are emitted, but also recorded. The last 2 remain. |
| 26 | + * |
| 27 | + * ints.collect { ... } // collects [3, 4, 1, 2, 3, 4] |
| 28 | + * ``` |
| 29 | + * |
| 30 | + * Throws [IllegalArgumentException] if size parameter is not greater than 0 |
| 31 | + * |
| 32 | + * @param history the number of items to keep in the [Flow]'s history -- must be greater than 0 |
| 33 | + */ |
| 34 | +@FlowPreview |
| 35 | +public fun <T> Flow<T>.cache(history: Int): Flow<T> = asCachedFlow(history) |
| 36 | + |
| 37 | +/** |
| 38 | + * Creates a [broadcast] coroutine which collects the [Flow] receiver and shares with multiple collectors. |
| 39 | + * |
| 40 | + * A [BroadcastChannel] with [default][Channel.Factory.BUFFERED] buffer size is created. |
| 41 | + * Use [buffer] operator on the flow before calling `shareIn` to specify a value other than |
| 42 | + * default and to control what happens when data is produced faster than it is consumed, |
| 43 | + * that is to control back-pressure behavior. |
| 44 | + * |
| 45 | + * Concurrent collectors will all collect from a single [broadcast] flow. This flow will be cancelled automatically |
| 46 | + * when it is no longer being collected, and the underlying channel will be closed. |
| 47 | + * |
| 48 | + * If a new collector is added after the channel has been closed, a new channel will be created. |
| 49 | + * |
| 50 | + * By default, this flow is effectively **stateless** in that collectors will only receive values emitted after collection begins. |
| 51 | + * |
| 52 | + * example: |
| 53 | + * |
| 54 | + * ``` |
| 55 | + * val sourceFlow = flowOf(1, 2, 3, 4, 5) |
| 56 | + * .onStart { println("start source") } |
| 57 | + * .onEach { println("emit $it") } |
| 58 | + * .onCompletion { println("complete source") } |
| 59 | + * .shareIn(this) |
| 60 | + * |
| 61 | + * val a = async { sourceFlow.toList() } |
| 62 | + * val b = async { sourceFlow.toList() } // collect concurrently |
| 63 | + * |
| 64 | + * println(a.await()) |
| 65 | + * println(b.await()) |
| 66 | + * |
| 67 | + * println("** break **") |
| 68 | + * |
| 69 | + * println(sourceFlow.toList()) |
| 70 | + * |
| 71 | + * prints: |
| 72 | + * |
| 73 | + * start source |
| 74 | + * emit 1 |
| 75 | + * emit 2 |
| 76 | + * emit 3 |
| 77 | + * emit 4 |
| 78 | + * emit 5 |
| 79 | + * complete source |
| 80 | + * [1, 2, 3, 4, 5] |
| 81 | + * [1, 2, 3, 4, 5] |
| 82 | + * ** break ** |
| 83 | + * start source |
| 84 | + * emit 1 |
| 85 | + * emit 2 |
| 86 | + * emit 3 |
| 87 | + * emit 4 |
| 88 | + * emit 5 |
| 89 | + * complete source |
| 90 | + * [1, 2, 3, 4, 5] |
| 91 | + * |
| 92 | + * ``` |
| 93 | + * ### Caching |
| 94 | + * |
| 95 | + * When a shared flow is cached, the values are recorded as they are emitted from the source Flow. |
| 96 | + * They are then replayed for each new subscriber. |
| 97 | + * |
| 98 | + * When a shared flow is reset, the cached values are cleared. |
| 99 | + * |
| 100 | + * example: |
| 101 | + * |
| 102 | + * ``` |
| 103 | + * val sourceFlow = flowOf(1, 2, 3, 4, 5) |
| 104 | + * .onEach { |
| 105 | + * delay(50) |
| 106 | + * println("emit $it") |
| 107 | + * }.shareIn(this, 1) |
| 108 | + * |
| 109 | + * val a = async { sourceFlow.toList() } |
| 110 | + * delay(125) |
| 111 | + * val b = async { sourceFlow.toList() } // begin collecting after "emit 3" |
| 112 | + * |
| 113 | + * println(a.await()) |
| 114 | + * println(b.await()) |
| 115 | + * |
| 116 | + * println("** break **") |
| 117 | + * |
| 118 | + * println(sourceFlow.toList()) // the shared flow has been reset, so the cached values are cleared |
| 119 | + * |
| 120 | + * prints: |
| 121 | + * |
| 122 | + * emit 1 |
| 123 | + * emit 2 |
| 124 | + * emit 3 |
| 125 | + * emit 4 |
| 126 | + * emit 5 |
| 127 | + * [1, 2, 3, 4, 5] |
| 128 | + * [2, 3, 4, 5] |
| 129 | + * ** break ** |
| 130 | + * emit 1 |
| 131 | + * emit 2 |
| 132 | + * emit 3 |
| 133 | + * emit 4 |
| 134 | + * emit 5 |
| 135 | + * [1, 2, 3, 4, 5] |
| 136 | + * |
| 137 | + * ``` |
| 138 | + * |
| 139 | + * In order to have cached values persist across resets, use `cache(n)` before `shareIn(...)`. |
| 140 | + * |
| 141 | + * example: |
| 142 | + * |
| 143 | + * ``` |
| 144 | + * // resets cache whenever the Flow is reset |
| 145 | + * flowOf(1, 2, 3).shareIn(myScope, 3) |
| 146 | + * |
| 147 | + * // persists cache across resets |
| 148 | + * flowOf(1, 2, 3).cached(3).shareIn(myScope) |
| 149 | + * ``` |
| 150 | + * |
| 151 | + * ### Cancellation semantics |
| 152 | + * 1) Flow consumer is cancelled when the original channel is cancelled. |
| 153 | + * 2) Flow consumer completes normally when the original channel completes (~is closed) normally. |
| 154 | + * 3) Collection is cancelled when the (scope)[CoroutineScope] parameter is cancelled, |
| 155 | + * thereby ending the consumer when it has run out of elements. |
| 156 | + * 4) If the flow consumer fails with an exception, subscription is cancelled. |
| 157 | + * |
| 158 | + * @param scope The [CoroutineScope] used to create the [broadcast] coroutine. Cancellation of this scope |
| 159 | + * will close the underlying [BroadcastChannel]. |
| 160 | + * @param cacheHistory (default = 0). Any value greater than zero will add a [cache] to the shared Flow. |
| 161 | + * |
| 162 | + */ |
| 163 | +@FlowPreview |
| 164 | +fun <T> Flow<T>.shareIn( |
| 165 | + scope: CoroutineScope, cacheHistory: Int = 0 |
| 166 | +): Flow<T> = asSharedFlow(scope, cacheHistory) |
0 commit comments