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
Copy file name to clipboardExpand all lines: proposals/0475-observed.md
+8-2Lines changed: 8 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -173,7 +173,7 @@ Task.detached {
173
173
}
174
174
```
175
175
176
-
In this case both tasks will get the same values upon the same events. This can
176
+
In this case both tasks will get consistently safe accessed values. This can
177
177
be achieved without needing an extra buffer since the suspension of each side of
178
178
the iteration are continuations resuming all together upon the accessor's
179
179
execution on the specified isolation. This facilitates subject-like behavior
@@ -257,6 +257,12 @@ composition of potentially failable systems. Any thrown error will mean that the
257
257
terminate with that given failure. Subsequent calls then to `next` on those
258
258
iterators will return `nil` - indicating that the iteration is complete.
259
259
260
+
The type `Observations` will conform to `AsyncSequence`. This means that it
261
+
adheres to the cancellation behavior of other `AsyncSequence` types; if the task
262
+
is cancelled then the iterator will return nil, and any time it becomes
263
+
terminal for any reason that sequence will remain terminal and continue returning nil.
264
+
Termination by cancellation however is independent for each instance.
265
+
260
266
## Behavioral Notes
261
267
262
268
There are a number of scenarios of iteration that can occur. These can range from production rate to iteration rate differentials to isolation differentials to concurrent iterations. Enumerating all possible combinations is of course not possible but the following explanations should illustrate some key usages. `Observations` does not make unsafe code somehow safe - the concepts of isolation protection or exclusive access are expected to be brought to the table by the types involved. It does however require the enforcements via Swift Concurrency particularly around the marking of the types and closures being required to be `Sendable`. The following examples will only illustrate well behaved types and avoid fully unsafe behavior that would lead to crashes because the types being used are circumventing that language safety.
@@ -351,7 +357,7 @@ The result of the observation may print the following output, but the primary pr
351
357
3 3
352
358
```
353
359
354
-
This case dropped the last value of the iteration because the accumulated differential exceeded the production; however the potentially confusing part here is that the sleep in the iterate competes with the scheduling in the emitter. This becomes clearer of a relationship when the boundaries of isolation are crossed.
360
+
The last value is never observed because the program ends before it would be. If the program did not terminate then another value would be observed.
355
361
356
362
Observations can be used across boundaries of concurrency. This is where the iteration is done on a different isolation than the mutations. The types however are accessed always in the isolation that the creation of the Observations closure is executed. This means that if the `Observations` instance is created on the main actor then the subsequent calls to the closure will be done on the main actor.
0 commit comments