@@ -69,13 +69,6 @@ private fun <U, T: U> setupTimeout(
69
69
return coroutine.startUndispatchedOrReturn(coroutine, block)
70
70
}
71
71
72
- /* *
73
- * @suppress **Deprecated**: for binary compatibility only
74
- */
75
- @Deprecated(" for binary compatibility only" , level= DeprecationLevel .HIDDEN )
76
- public suspend fun <T > withTimeout (time : Long , unit : TimeUnit = TimeUnit .MILLISECONDS , block : suspend () -> T ): T =
77
- withTimeout(time, unit) { block() }
78
-
79
72
private open class TimeoutCoroutine <U , in T : U >(
80
73
@JvmField val time : Long ,
81
74
@JvmField val unit : TimeUnit ,
@@ -140,32 +133,21 @@ public suspend fun <T> withTimeoutOrNull(time: Int, block: suspend CoroutineScop
140
133
*/
141
134
public suspend fun <T > withTimeoutOrNull (time : Long , unit : TimeUnit = TimeUnit .MILLISECONDS , block : suspend CoroutineScope .() -> T ): T ? {
142
135
if (time <= 0L ) return null
143
- return suspendCoroutineUninterceptedOrReturn { uCont ->
144
- setupTimeout(TimeoutOrNullCoroutine (time, unit, uCont), block)
145
- }
146
- }
147
-
148
- /* *
149
- * @suppress **Deprecated**: for binary compatibility only
150
- */
151
- @Deprecated(" for binary compatibility only" , level= DeprecationLevel .HIDDEN )
152
- public suspend fun <T > withTimeoutOrNull (time : Long , unit : TimeUnit = TimeUnit .MILLISECONDS , block : suspend () -> T ): T ? =
153
- withTimeoutOrNull(time, unit) { block() }
154
136
155
- private class TimeoutOrNullCoroutine < T >(
156
- time : Long ,
157
- unit : TimeUnit ,
158
- uCont : Continuation < T ?> // unintercepted continuation
159
- ) : TimeoutCoroutine<T?, T>(time, unit, uCont) {
160
- @Suppress( " UNCHECKED_CAST " )
161
- internal override fun onCompletionInternal ( state : Any? , mode : Int ) {
162
- if (state is CompletedExceptionally ) {
163
- val exception = state.cause
164
- if ( exception is TimeoutCancellationException && exception.coroutine == = this )
165
- uCont.resumeUninterceptedMode( null , mode) else
166
- uCont.resumeUninterceptedWithExceptionMode(exception, mode)
167
- } else
168
- uCont.resumeUninterceptedMode(state as T , mode)
137
+ var coroutine : TimeoutCoroutine < T ?, T ?> ? = null
138
+ try {
139
+ return suspendCoroutineUninterceptedOrReturn { uCont ->
140
+ TimeoutCoroutine (time, unit, uCont). let {
141
+ coroutine = it
142
+ setupTimeout< T ?, T ?>(it, block )
143
+ }
144
+ }
145
+ } catch (e : TimeoutCancellationException ) {
146
+ // Return null iff it's our exception, otherwise propagate it upstream (e.g. in case of nested withTimeouts )
147
+ if (e.coroutine == = coroutine) {
148
+ return null
149
+ }
150
+ throw e
169
151
}
170
152
}
171
153
0 commit comments