blob: 13be53668ec74fee051dd9aa6447a235453326ec [file] [log] [blame]
Yigit Boyarf3673802020-12-28 12:08:30 -08001/*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.paging
18
19import kotlinx.coroutines.CoroutineScope
20import kotlinx.coroutines.Job
21import kotlinx.coroutines.channels.Channel
22import kotlinx.coroutines.channels.SendChannel
23import kotlinx.coroutines.coroutineScope
24import kotlinx.coroutines.flow.Flow
25import kotlinx.coroutines.flow.buffer
26import kotlinx.coroutines.flow.flow
27import kotlinx.coroutines.flow.internal.FusibleFlow
28import kotlinx.coroutines.launch
29import kotlinx.coroutines.suspendCancellableCoroutine
30import kotlin.coroutines.resume
31
32/**
33 * This is a simplified channelFlow implementation as a temporary measure until channel flow
34 * leaves experimental state.
35 *
36 * The exact same implementation is not possible due to [FusibleFlow] being an internal API. To
37 * get close to that implementation, internally we use a [Channel.RENDEZVOUS] channel and use a
38 * [buffer] ([Channel.BUFFERED]) operator on the resulting Flow. This gives us a close behavior
39 * where the default is buffered and any followup buffer operation will result in +1 value being
40 * produced.
41 */
42internal fun <T> simpleChannelFlow(
43 block: suspend SimpleProducerScope<T>.() -> Unit
44): Flow<T> {
45 return flow {
46 coroutineScope {
47 val channel = Channel<T>(capacity = Channel.RENDEZVOUS)
48 val producer = launch {
49 try {
50 // run producer in a separate inner scope to ensure we wait for its children
51 // to finish, in case it does more launches inside.
52 coroutineScope {
53 val producerScopeImpl = SimpleProducerScopeImpl(
54 scope = this,
55 channel = channel,
56 )
57 producerScopeImpl.block()
58 }
59 channel.close()
60 } catch (t: Throwable) {
61 channel.close(t)
62 }
63 }
64 for (item in channel) {
65 emit(item)
66 }
67 // in case channel closed before producer completes, cancel the producer.
68 producer.cancel()
69 }
70 }.buffer(Channel.BUFFERED)
71}
72
73internal interface SimpleProducerScope<T> : CoroutineScope, SendChannel<T> {
74 val channel: SendChannel<T>
75 suspend fun awaitClose(block: () -> Unit)
76}
77
78internal class SimpleProducerScopeImpl<T>(
79 scope: CoroutineScope,
80 override val channel: SendChannel<T>,
81) : SimpleProducerScope<T>, CoroutineScope by scope, SendChannel<T> by channel {
82 override suspend fun awaitClose(block: () -> Unit) {
83 try {
84 val job = checkNotNull(coroutineContext[Job]) {
85 "Internal error, context should have a job."
86 }
87 suspendCancellableCoroutine<Unit> { cont ->
88 job.invokeOnCompletion {
89 cont.resume(Unit)
90 }
91 }
92 } finally {
93 block()
94 }
95 }
96}