// 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`).importCottonKitimportFoundation@MainActor@ObservablepublicfinalclassNotificationBridge {publicstaticlet shared = NotificationBridge()privatevar held: [NotificationTap] = []privatevar 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.privatevar heldReceipts: [PushReceipt] = []privatevar 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.publicprivate(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.publicprivate(set) var tokenFailure: String?publicinit() {}/// A tap landed. Held while nobody is listening, delivered at once after.publicfunc arrive(_ tap: NotificationTap) {iflet sink { sink(tap)return } held.append(tap) }/// APNs handed over a token.publicfunc token(_ hex: String) { deviceToken = hex tokenFailure = nil }/// APNs would not.publicfunc 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.publicfunc attach(_ sink: @escaping (NotificationTap) -> Void) {let waiting = held held = []self.sink = sinkfor tap in waiting { sink(tap) } }/// Stop delivering. A tap or receipt arriving now is held again.publicfunc detach() { sink = nil receiptSink = nil }/// This phone met a push (`PushReceipt`). Held while nobody listens.publicfunc met(_ receipt: PushReceipt) {iflet receiptSink { receiptSink(receipt)return } heldReceipts.append(receipt) }/// Take every receipt waiting, and every later one straight through.publicfunc attachReceipts(_ sink: @escaping (PushReceipt) -> Void) {let waiting = heldReceipts heldReceipts = [] receiptSink = sinkfor receipt in waiting { sink(receipt) } }/// How many are still held. For the test, and for nothing else.publicvar heldCount: Int { held.count }}