Download this file
| 1 | /// Main-actor reentrancy can interleave two async publication sequences even |
| 2 | /// though each individual operation is actor-isolated. Coalesce requests while |
| 3 | /// retaining one owner of source retirement, snapshot creation and publication. |
| 4 | @MainActor final class DocumentPublication { |
| 5 | private var running = false |
| 6 | private var requested = false |
| 7 | func run(_ operation: @MainActor () async -> Void) async { |
| 8 | requested = true |
| 9 | guard !running else { return } |
| 10 | running = true |
| 11 | defer { running = false } |
| 12 | while requested { |
| 13 | requested = false |
| 14 | await operation() |
| 15 | } |
| 16 | } |
| 17 | } |
| 18 | |