https://github.com/swhitty/identifiablecontinuation
Swift continuation that conforms to Identifiable and includes a cancellation handler.
https://github.com/swhitty/identifiablecontinuation
async async-await swift
Last synced: 8 days ago
JSON representation
Swift continuation that conforms to Identifiable and includes a cancellation handler.
- Host: GitHub
- URL: https://github.com/swhitty/identifiablecontinuation
- Owner: swhitty
- License: mit
- Created: 2023-05-19T22:46:55.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-06-03T05:52:40.000Z (17 days ago)
- Last Synced: 2025-06-03T18:26:52.844Z (16 days ago)
- Topics: async, async-await, swift
- Language: Swift
- Homepage:
- Size: 90.8 KB
- Stars: 27
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/swhitty/IdentifiableContinuation/actions/workflows/build.yml)
[](https://codecov.io/gh/swhitty/IdentifiableContinuation)
[](https://swiftpackageindex.com/swhitty/IdentifiableContinuation)
[](https://swiftpackageindex.com/swhitty/IdentifiableContinuation)# Introduction
**IdentifiableContinuation** is a lightweight wrapper around [`CheckedContinuation`](https://developer.apple.com/documentation/swift/checkedcontinuation) that conforms to [`Identifiable`](https://developer.apple.com/documentation/swift/identifiable) and includes an easy to use cancellation handler with the id.
# Installation
IdentifiableContinuation can be installed by using Swift Package Manager.
**Note:** IdentifiableContinuation requires Swift 5.10 on Xcode 15.4+. It runs on iOS 13+, tvOS 13+, macOS 10.15+, Linux and Windows.
To install using Swift Package Manager, add this to the `dependencies:` section in your Package.swift file:```swift
.package(url: "https://github.com/swhitty/IdentifiableContinuation.git", .upToNextMajor(from: "0.4.0"))
```# Usage
With Swift 6, usage is similar to existing continuations.
```swift
let val: String = await withIdentifiableContinuation {
continuations[$0.id] = $0
}
```The body closure is executed syncronously within the current isolation allowing actors to mutate their isolated state.
An optional cancellation handler is called when the task is cancelled. The handler is `@Sendable` and can be called at any time _after_ the body has completed.
```swift
let val: String = await withIdentifiableContinuation {
continuations[$0.id] = $0
} onCancel: { id in
// @Sendable closure executed outside of actor isolation requires `await` to mutate actor state
Task { await self.cancelContinuation(with: id) }
}
```> The above is also compatible in Swift 5 language mode using a Swift 6 compiler e.g. Xcode 16
## Swift 5
While behaviour is identical, Swift 5 compilers (Xcode 15) are unable to inherit actor isolation through the new `#isolation` keyword ([SE-420](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0420-inheritance-of-actor-isolation.md)) so an `isolated` reference to the current actor must always be passed.
```swift
let val: String = await withIdentifiableContinuation(isolation: self) {
continuations[$0.id] = $0
}
```# Credits
IdentifiableContinuation is primarily the work of [Simon Whitty](https://github.com/swhitty).
([Full list of contributors](https://github.com/swhitty/IdentifiableContinuation/graphs/contributors))