You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -22,26 +22,35 @@ To install using Swift Package Manager, add this to the `dependencies:` section
22
22
23
23
# Usage
24
24
25
-
Usage is similar to existing continuations, but requires an `Actor` to ensure the closure is executed within the actors isolation:
25
+
With Swift 6, usage is similar to existing continuations where the closure is executed syncronously within the current isolation allowing actors to mutate their isolated state.
26
26
27
27
```swift
28
-
let val: String=awaitwithIdentifiableContinuation(isolation: self) {
29
-
$0.resume(returning: "bar")
28
+
let val: String=awaitwithIdentifiableContinuation {
29
+
continuations[$0.id] =$0
30
30
}
31
31
```
32
32
33
-
This allows actors to synchronously start continuations and mutate their isolated state _before_ suspension occurs. The `onCancel:`handler is `@Sendable` and can be called at any time _after_ the body has completed. Manually check `Task.isCancelled` before creating the continuation to prevent performing unrequired work.
33
+
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.
34
34
35
35
```swift
36
-
let val: String=awaitwithIdentifiableContinuation(isolation: self) {
37
-
// executed within actor isolation so can immediately mutate actor state
36
+
let val: String=awaitwithIdentifiableContinuation {
38
37
continuations[$0.id] =$0
39
-
} onCancel: { id in
38
+
} onCancel { id in
40
39
// @Sendable closure executed outside of actor isolation requires `await` to mutate actor state
41
40
Task { awaitself.cancelContinuation(with: id) }
42
41
}
43
42
```
44
43
44
+
## Swift 5
45
+
46
+
While behaviour is identical, Swift 5 is unable to automatically 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.
47
+
48
+
```swift
49
+
let val: String=awaitwithIdentifiableContinuation(isolation: self) {
50
+
continuations[$0.id] =$0
51
+
}
52
+
```
53
+
45
54
# Credits
46
55
47
56
IdentifiableContinuation is primarily the work of [Simon Whitty](https://github.com/swhitty).
0 commit comments