Skip to content

Commit 3fcc383

Browse files
committed
Simplify ContinuationBox implementation
1 parent 4af9827 commit 3fcc383

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

Sources/Sharing/SharedContinuations.swift

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ public struct SaveContinuation: Sendable {
210210
private final class ContinuationBox<Value>: Sendable {
211211
private let callback: Mutex<(@Sendable (Result<Value?, any Error>) -> Void)?>
212212
private let description: @Sendable () -> String
213-
private let resumeCount = Mutex(0)
214213

215214
init(
216215
callback: @escaping @Sendable (Result<Value?, any Error>) -> Void,
@@ -221,35 +220,30 @@ private final class ContinuationBox<Value>: Sendable {
221220
}
222221

223222
deinit {
224-
let isComplete = resumeCount.withLock { $0 } > 0
225-
if !isComplete {
223+
if let callback = callback.withLock(\.self) {
226224
reportIssue(
227225
"""
228226
\(description()) leaked its continuation without one of its resume methods being \
229227
invoked. This will cause tasks waiting on it to resume immediately.
230228
"""
231229
)
232-
callback.withLock(\.self)?(.success(nil))
230+
callback(.success(nil))
233231
}
234232
}
235233

236234
func resume(with result: Result<Value?, any Error>) {
237-
let resumeCount = resumeCount.withLock {
238-
$0 += 1
239-
return $0
235+
let callback = callback.withLock { callback in
236+
defer { callback = nil }
237+
return callback
240238
}
241-
guard resumeCount == 1 else {
239+
guard let callback else {
242240
reportIssue(
243241
"""
244242
\(description()) tried to resume its continuation more than once.
245243
"""
246244
)
247245
return
248246
}
249-
let callback = callback.withLock { callback in
250-
defer { callback = nil }
251-
return callback
252-
}
253-
callback?(result)
247+
callback(result)
254248
}
255249
}

0 commit comments

Comments
 (0)