|
| 1 | +// |
| 2 | +// Continuation+Extensions.swift |
| 3 | +// FlyingFox |
| 4 | +// |
| 5 | +// Created by Simon Whitty on 17/02/2022. |
| 6 | +// Copyright © 2022 Simon Whitty. All rights reserved. |
| 7 | +// |
| 8 | +// Distributed under the permissive MIT license |
| 9 | +// Get the latest version from here: |
| 10 | +// |
| 11 | +// https://github.com/swhitty/FlyingFox |
| 12 | +// |
| 13 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 14 | +// of this software and associated documentation files (the "Software"), to deal |
| 15 | +// in the Software without restriction, including without limitation the rights |
| 16 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 17 | +// copies of the Software, and to permit persons to whom the Software is |
| 18 | +// furnished to do so, subject to the following conditions: |
| 19 | +// |
| 20 | +// The above copyright notice and this permission notice shall be included in all |
| 21 | +// copies or substantial portions of the Software. |
| 22 | +// |
| 23 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 26 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 28 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 29 | +// SOFTWARE. |
| 30 | +// |
| 31 | + |
| 32 | +import Foundation |
| 33 | + |
| 34 | +func withCancellingContinuation<T>(function: String = #function, |
| 35 | + returning returnType: T.Type, |
| 36 | + body: (CheckedContinuation<T, Error>, inout CancellationHandler) -> Void) async throws -> T { |
| 37 | + let inner = CancellationHandler.Inner() |
| 38 | + return try await withTaskCancellationHandler( |
| 39 | + operation: { |
| 40 | + try await withCheckedThrowingContinuation(function: function) { (continuation: CheckedContinuation<T, Error>) in |
| 41 | + var handler = CancellationHandler(inner: inner) |
| 42 | + body(continuation, &handler) |
| 43 | + } |
| 44 | + }, |
| 45 | + onCancel: inner.cancel) |
| 46 | +} |
| 47 | + |
| 48 | +struct CancellationHandler { |
| 49 | + |
| 50 | + fileprivate let inner: Inner |
| 51 | + |
| 52 | + @Sendable |
| 53 | + mutating func onCancel(_ handler: @escaping () -> Void) { |
| 54 | + inner.onCancel(handler) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +extension CancellationHandler { |
| 59 | + |
| 60 | + final class Inner { |
| 61 | + |
| 62 | + private let lock = NSLock() |
| 63 | + private var isCancelled: Bool = false |
| 64 | + private var handler: (() -> Void)? |
| 65 | + |
| 66 | + @Sendable |
| 67 | + func onCancel(_ handler: @escaping () -> Void) { |
| 68 | + lock.lock() |
| 69 | + self.handler = handler |
| 70 | + let isCancelled = self.isCancelled |
| 71 | + lock.unlock() |
| 72 | + |
| 73 | + if isCancelled { |
| 74 | + handler() |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Sendable |
| 79 | + fileprivate func cancel() { |
| 80 | + lock.lock() |
| 81 | + isCancelled = true |
| 82 | + let handler = self.handler |
| 83 | + lock.unlock() |
| 84 | + |
| 85 | + handler?() |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | + |
0 commit comments