Skip to content

Commit c775257

Browse files
author
Francesco Vasco
committed
Release block on LazyXxxCoroutine start
1 parent bafe7fb commit c775257

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

common/kotlinx-coroutines-core-common/src/Builders.common.kt

+10-2
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,13 @@ private open class DeferredCoroutine<T>(
104104

105105
private class LazyDeferredCoroutine<T>(
106106
parentContext: CoroutineContext,
107-
private val block: suspend CoroutineScope.() -> T
107+
block: suspend CoroutineScope.() -> T
108108
) : DeferredCoroutine<T>(parentContext, active = false) {
109+
private var block: (suspend CoroutineScope.() -> T)? = block
110+
109111
override fun onStart() {
112+
val block = checkNotNull(this.block) { "Already started" }
113+
this.block = null
110114
block.startCoroutineCancellable(this, this)
111115
}
112116
}
@@ -161,9 +165,13 @@ private open class StandaloneCoroutine(
161165

162166
private class LazyStandaloneCoroutine(
163167
parentContext: CoroutineContext,
164-
private val block: suspend CoroutineScope.() -> Unit
168+
block: suspend CoroutineScope.() -> Unit
165169
) : StandaloneCoroutine(parentContext, active = false) {
170+
private var block: (suspend CoroutineScope.() -> Unit)? = block
171+
166172
override fun onStart() {
173+
val block = checkNotNull(this.block) { "Already started" }
174+
this.block = null
167175
block.startCoroutineCancellable(this, this)
168176
}
169177
}

common/kotlinx-coroutines-core-common/src/channels/Broadcast.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,10 @@ private open class BroadcastCoroutine<E>(
106106
private class LazyBroadcastCoroutine<E>(
107107
parentContext: CoroutineContext,
108108
channel: BroadcastChannel<E>,
109-
private val block: suspend ProducerScope<E>.() -> Unit
109+
block: suspend ProducerScope<E>.() -> Unit
110110
) : BroadcastCoroutine<E>(parentContext, channel, active = false) {
111+
private var block: (suspend ProducerScope<E>.() -> Unit)? = block
112+
111113
override fun openSubscription(): ReceiveChannel<E> {
112114
// open subscription _first_
113115
val subscription = _channel.openSubscription()
@@ -117,6 +119,8 @@ private class LazyBroadcastCoroutine<E>(
117119
}
118120

119121
override fun onStart() {
122+
val block = checkNotNull(this.block) { "Already started" }
123+
this.block = null
120124
block.startCoroutineCancellable(this, this)
121125
}
122126
}

0 commit comments

Comments
 (0)