// The buffering seam for a tap that arrives before the app is ready. // // 4b builds ONLY this. There is no push token here, no `NOTIFICATION_ROUTES` // map and no `aps-environment` entitlement — those are 4j's, and the entitlement // in particular breaks device signing if it lands before the capability exists. // What 4b owes is the RULE, because it is a boot-path rule and this is the // sub-step that owns the boot path: // // **A tap that arrives before the gate resolves is HELD, and replayed exactly // once when it does.** A cold-start tap on a notification reaches the app long // before `read.boot` has answered, let alone before a file is open. Pushing the // route then means pushing a screen over a stack that does not exist yet; and // dropping it means a person who tapped a notification landed on Home with no // explanation. `useNotificationTaps({isReady})` held them; so does this. // // Held taps replay in ARRIVAL order: 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. After the gate resolves a // tap goes STRAIGHT through, so the buffer is a cold-start device and not a // permanent queue. // // A tap carries the database it names (`CottonKit/DatabaseLink.swift`), held // with it, so a cold-start link into a project database still opens there. import CottonKit import Foundation @MainActor @Observable public final class PendingTaps { private var held: [DatabaseLink] = [] private var deliver: ((DatabaseLink) -> Void)? /// Links taken from the hold and still being placed. private var placingLinks = 0 /// A link has arrived and its screen is not open yet — held for the gate, /// or still being placed. Home reads nothing meanwhile: a cold launch into /// a list read Home's 26 tiles beside that list's own read (`BoardCover`). public var arriving: Bool { !held.isEmpty || placingLinks > 0 } /// The last link still being placed, so the next one waits its turn. @ObservationIgnored private var placing: Task? public init() {} /// A tap landed, with the database it names. Held while the gate is still /// deciding; delivered at once after. public func arrive(_ link: DatabaseLink) { if let deliver { deliver(link) return } held.append(link) } /// A tap that names no database. public func arrive(_ route: Route) { arrive(DatabaseLink(route: route)) } /// The stack exists: take everything that was waiting, in order, and send /// every later tap straight through. /// /// Idempotent. `.task` can run again on a re-created view, and a replay that /// happened twice would push the same screen twice. public func open(_ deliver: @escaping (Route) -> Void) { openLinks { deliver($0.route) } } /// `open`, with a step between a link and its route: `place` shows the /// link's database in the window (`DatabaseLinkPlacing`) and answers the /// route to open, or nil when nothing opens. Placing waits on the held /// list, so it is async — and each link still waits for the one before /// it, which keeps the arrival order. public func open( placing place: @escaping @MainActor (DatabaseLink) async -> Route?, then deliver: @escaping (Route) -> Void ) { openLinks { [weak self] link in guard let self else { return } let previous = placing placingLinks += 1 placing = Task { @MainActor in await previous?.value if let route = await place(link) { deliver(route) } placingLinks -= 1 } } } private func openLinks(_ deliver: @escaping (DatabaseLink) -> Void) { let waiting = held held = [] self.deliver = deliver for link in waiting { deliver(link) } } /// Stop delivering — the gate went back to waiting (a leave, a refusal), so /// a tap arriving now is held again rather than pushed onto a stack that is /// no longer there. public func close() { deliver = nil } /// How many are still held. For the test, and for nothing else. public var heldCount: Int { held.count } }