@@ -84,15 +84,18 @@ public suspend fun <T: Any> Flow<T>.singleOrNull(): T? {
84
84
*/
85
85
public suspend fun <T > Flow<T>.first (): T {
86
86
var result: Any? = NULL
87
- try {
88
- collect { value ->
87
+ val collector = object : FlowCollector < T > {
88
+ override suspend fun emit ( value : T ) {
89
89
result = value
90
- throw AbortFlowException (NopCollector )
90
+ throw AbortFlowException (this )
91
91
}
92
+ }
93
+ try {
94
+ collect(collector)
92
95
} catch (e: AbortFlowException ) {
93
- // Do nothing
96
+ // Do not suppress other cancellation sources
97
+ e.checkOwnership(collector)
94
98
}
95
-
96
99
if (result == = NULL ) throw NoSuchElementException (" Expected at least one element" )
97
100
return result as T
98
101
}
@@ -103,17 +106,20 @@ public suspend fun <T> Flow<T>.first(): T {
103
106
*/
104
107
public suspend fun <T > Flow<T>.first (predicate : suspend (T ) -> Boolean ): T {
105
108
var result: Any? = NULL
106
- try {
107
- collect { value ->
109
+ val collector = object : FlowCollector < T > {
110
+ override suspend fun emit ( value : T ) {
108
111
if (predicate(value)) {
109
112
result = value
110
- throw AbortFlowException (NopCollector )
113
+ throw AbortFlowException (this )
111
114
}
112
115
}
116
+ }
117
+ try {
118
+ collect(collector)
113
119
} catch (e: AbortFlowException ) {
114
- // Do nothing
120
+ // Do not suppress other cancellation sources
121
+ e.checkOwnership(collector)
115
122
}
116
-
117
123
if (result == = NULL ) throw NoSuchElementException (" Expected at least one element matching the predicate $predicate " )
118
124
return result as T
119
125
}
@@ -124,13 +130,17 @@ public suspend fun <T> Flow<T>.first(predicate: suspend (T) -> Boolean): T {
124
130
*/
125
131
public suspend fun <T : Any > Flow<T>.firstOrNull (): T ? {
126
132
var result: T ? = null
127
- try {
128
- collect { value ->
133
+ val collector = object : FlowCollector < T > {
134
+ override suspend fun emit ( value : T ) {
129
135
result = value
130
- throw AbortFlowException (NopCollector )
136
+ throw AbortFlowException (this )
131
137
}
138
+ }
139
+ try {
140
+ collect(collector)
132
141
} catch (e: AbortFlowException ) {
133
- // Do nothing
142
+ // Do not suppress other cancellation sources
143
+ e.checkOwnership(collector)
134
144
}
135
145
return result
136
146
}
@@ -141,15 +151,19 @@ public suspend fun <T : Any> Flow<T>.firstOrNull(): T? {
141
151
*/
142
152
public suspend fun <T : Any > Flow<T>.firstOrNull (predicate : suspend (T ) -> Boolean ): T ? {
143
153
var result: T ? = null
144
- try {
145
- collect { value ->
154
+ val collector = object : FlowCollector < T > {
155
+ override suspend fun emit ( value : T ) {
146
156
if (predicate(value)) {
147
157
result = value
148
- throw AbortFlowException (NopCollector )
158
+ throw AbortFlowException (this )
149
159
}
150
160
}
161
+ }
162
+ try {
163
+ collect(collector)
151
164
} catch (e: AbortFlowException ) {
152
- // Do nothing
165
+ // Do not suppress other cancellation sources
166
+ e.checkOwnership(collector)
153
167
}
154
168
return result
155
169
}
0 commit comments