field_ios

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

TeraEditingProtection.swift (3406B)


      1 import Foundation
      2 
      3 /// One replacement and one worker. These transient tokens are never persisted
      4 /// as composer identity, revision, or publication authority.
      5 @MainActor
      6 final class TeraEditingProtection: ObservableObject {
      7   enum Kind { case editing, reopen }
      8   @Published private(set) var failed = false {
      9     didSet {
     10       if failed != oldValue {
     11         changed()
     12       }
     13     }
     14   }
     15 
     16   @Published private(set) var isWorking = false {
     17     didSet {
     18       if isWorking != oldValue {
     19         changed()
     20       }
     21     }
     22   }
     23 
     24   var changed: () -> Void = {}
     25   @Published private(set) var reopened: UUID?
     26   var cancelled: () -> Void = {}
     27   private struct Pending {
     28     let token: UUID
     29     let kind: Kind
     30     let save: () async -> Bool
     31     let apply: () async -> Bool
     32   }
     33 
     34   private var pending: Pending?
     35   private var worker: Task<Bool, Never>?
     36   private var activeToken: UUID?
     37 
     38   var choiceToken: UUID? {
     39     pending?.token
     40   }
     41 
     42   deinit { worker?.cancel() }
     43 
     44   func replace(kind: Kind, save: @escaping () async -> Bool, apply: @escaping () async -> Bool) async -> Bool {
     45     guard let token = schedule(kind: kind, save: save, apply: apply), let worker else { return false }
     46     return await withTaskCancellationHandler { await worker.value } onCancel: {
     47       Task { @MainActor [weak self] in self?.cancel(token: token) }
     48     }
     49   }
     50 
     51   @discardableResult
     52   func schedule(kind: Kind, save: @escaping () async -> Bool, apply: @escaping () async -> Bool) -> UUID? {
     53     guard pending == nil, worker == nil, !Task.isCancelled else { return nil }
     54     let token = UUID()
     55     pending = Pending(token: token, kind: kind, save: save, apply: apply)
     56     start(save: true)
     57     return token
     58   }
     59 
     60   func retry() {
     61     start(save: true)
     62   }
     63 
     64   func discard() {
     65     start(save: false)
     66   }
     67 
     68   func discard(token: UUID) {
     69     guard pending?.token == token else { return }
     70     discard()
     71   }
     72 
     73   func cancel() {
     74     let hadRequest = pending != nil || activeToken != nil
     75     pending = nil
     76     activeToken = nil
     77     failed = false
     78     isWorking = false
     79     worker?.cancel()
     80     if hadRequest {
     81       cancelled()
     82     }
     83   }
     84 
     85   func editingChanged() {
     86     // Once the replacement is admitted, the Add owner's operation generation
     87     // fences its callbacks. Its own form assignment is not a new dirty edit.
     88     if pending != nil {
     89       cancel()
     90     }
     91   }
     92 
     93   func cancel(token: UUID) {
     94     if pending?.token == token || activeToken == token {
     95       cancel()
     96     }
     97   }
     98 
     99   private func start(save: Bool) {
    100     guard worker == nil, let pending, !Task.isCancelled else { return }
    101     failed = false
    102     isWorking = true
    103     activeToken = pending.token
    104     worker = Task { [weak self] in
    105       guard let self else { return false }
    106       return await run(pending, save: save)
    107     }
    108   }
    109 
    110   private func run(_ request: Pending, save: Bool) async -> Bool {
    111     defer {
    112       worker = nil
    113       isWorking = false
    114       activeToken = nil
    115     }
    116     let saved = if save {
    117       await request.save()
    118     } else {
    119       true
    120     }
    121     guard !Task.isCancelled, pending?.token == request.token, activeToken == request.token else { return false }
    122     guard saved else {
    123       failed = true
    124       return false
    125     }
    126     pending = nil
    127     let applied = await request.apply()
    128     guard !Task.isCancelled, activeToken == request.token, applied else { return false }
    129     if request.kind == .reopen {
    130       reopened = request.token
    131     }
    132     return true
    133   }
    134 }