field_ios

In-the-field app for Radroots on iOS
git clone https://radroots.dev/git/field_ios.git
Log | Files | Refs | README | LICENSE

TeraTodayReconciliationTask.swift (1059B)


      1 import Foundation
      2 
      3 /// Serial presentation ownership. Paging waits for an active reconciliation;
      4 /// visibility updates can still supersede a page read already in flight.
      5 @MainActor
      6 final class TeraTodayReconciliationTask {
      7   private var task: Task<Void, Never>?
      8   private var identity: UUID?
      9 
     10   deinit { task?.cancel() }
     11 
     12   func cancel() {
     13     task?.cancel()
     14     task = nil
     15     identity = nil
     16   }
     17 
     18   func wait() async {
     19     while let current = task, !Task.isCancelled {
     20       // A page waiter does not own this mandatory visibility refresh.
     21       await current.value
     22     }
     23   }
     24 
     25   func run(_ operation: @escaping @MainActor () async -> Void) async {
     26     cancel()
     27     let id = UUID()
     28     identity = id
     29     let current = Task { [weak self] in
     30       defer {
     31         if self?.identity == id {
     32           self?.task = nil
     33           self?.identity = nil
     34         }
     35       }
     36       guard !Task.isCancelled else { return }
     37       await operation()
     38     }
     39     task = current
     40     await withTaskCancellationHandler { await current.value } onCancel: { current.cancel() }
     41   }
     42 }