Cotton code
107 lines · 4.0 KB · SwiftRaw
// Where the app delegate's two facts wait for the model that wants them.//// SwiftUI builds the `UIApplicationDelegateAdaptor` before it builds the view// tree, and iOS delivers a COLD-START tap and the APNs token to that delegate —// both of which can land before `RootView` has made a `NotificationsModel` to// hear them. So there is one object between them, and it holds exactly two// things: the taps nobody has taken yet, and the last token APNs handed over.//// **This is not a second `PendingTaps`.** They buffer different absences and// both are needed. This one holds a tap while there is no MODEL — a window that// closes on the first frame. `PendingTaps` holds a resolved `Route` while there// is no STACK — a window that closes when the boot gate resolves, which can be// a whole snapshot download later. A tap at launch passes through both.//// It is a singleton because the delegate has no other way to reach anything:// `UIApplicationDelegateAdaptor` constructs the class itself, with no// initialiser of ours. `attach` replaces rather than adds, on `PendingTaps`'// own reasoning — a second sink would mean two screens pushed for one tap.//// It carries a third fact the same way: a push this phone MET — presented,// tapped — whose receipt the model tells the serving Mac (`PushReceipt`).import CottonKitimport Foundation@MainActor@Observablepublic final class NotificationBridge {    public static let shared = NotificationBridge()    private var held: [NotificationTap] = []    private var sink: ((NotificationTap) -> Void)?    /// Receipts of pushes this phone met before a model listened — a push    /// presented at launch is owed its receipt as much as a later one.    private var heldReceipts: [PushReceipt] = []    private var receiptSink: ((PushReceipt) -> Void)?    /// The last raw APNs token, lowercase hex, or `nil` if none has arrived.    ///    /// Kept rather than passed through, because it outlives every model: iOS    /// hands it over once per launch and a model built afterwards would    /// otherwise never see it.    public private(set) var deviceToken: String?    /// Why APNs would not give one. Recorded and shown to nobody: a device with    /// no token is a quiet phone, not an error screen.    public private(set) var tokenFailure: String?    public init() {}    /// A tap landed. Held while nobody is listening, delivered at once after.    public func arrive(_ tap: NotificationTap) {        if let sink {            sink(tap)            return        }        held.append(tap)    }    /// APNs handed over a token.    public func token(_ hex: String) {        deviceToken = hex        tokenFailure = nil    }    /// APNs would not.    public func tokenFailed(_ reason: String) {        tokenFailure = reason    }    /// Take everything waiting, in ARRIVAL order, and every later tap straight    /// through. Two taps while the splash was up are two screens, and the    /// second is the one on top — which is what tapping them in that order    /// would have done had the app been open.    public func attach(_ sink: @escaping (NotificationTap) -> Void) {        let waiting = held        held = []        self.sink = sink        for tap in waiting { sink(tap) }    }    /// Stop delivering. A tap or receipt arriving now is held again.    public func detach() {        sink = nil        receiptSink = nil    }    /// This phone met a push (`PushReceipt`). Held while nobody listens.    public func met(_ receipt: PushReceipt) {        if let receiptSink {            receiptSink(receipt)            return        }        heldReceipts.append(receipt)    }    /// Take every receipt waiting, and every later one straight through.    public func attachReceipts(_ sink: @escaping (PushReceipt) -> Void) {        let waiting = heldReceipts        heldReceipts = []        receiptSink = sink        for receipt in waiting { sink(receipt) }    }    /// How many are still held. For the test, and for nothing else.    public var heldCount: Int { held.count }}