Cotton code
64 lines · 2.9 KB · SwiftRaw
// A database switch's timeline, as log lines (subsystem `com.cottondb.fan`,// category `switch`; lane stalepic, 15 Oct 2026): the moment a cap is chosen// — the caps' arc heads for it as the finger lifts or the tap lands — and// then, once each, the milliseconds since then at which the window shows the// database (`shown`: its Home is read and the switch turns), its fan mounts// (`fan`), its page area first draws something (`ghost`, `picture`), its// tiles are answered (`arranged`) and its live page has drawn arranged// (`live`).//// A cold launch is a timeline of its own (`launch`, 295 B): from the window's// first appearance, the moment Home's first read is asked (`home-asked`), the// moment the shown database's read has come back (`home-read`), and the frame// the splash goes (`splash-down`), beside the fan's own marks.// `scripts/home-preview/switch-times.py` reads them off a walk.#if os(iOS)import OSLog@MainActorenum SwitchClock {    private static var asked: (database: String, at: ContinuousClock.Instant)?    private static var marked: Set<String> = []    private static var launched = false    private static let log = Logger(subsystem: "com.cottondb.fan", category: "switch")    /// The launch's timeline, named as a database's is.    static let launch = "launch"    /// The window has appeared for the first time this process: the launch's    /// timeline starts now. Once a process — a second window is no launch.    static func launching() {        guard !launched else { return }        launched = true        asked(launch)    }    /// A cap was chosen: the switch to `database` starts now. Asking for the    /// database already being switched to keeps its start.    static func asked(_ database: String) {        guard asked?.database != database else { return }        asked = (database, .now)        marked = []        log.notice("switch: \(database, privacy: .public) asked")    }    /// `what` happened for the switch under way, once: logged with the    /// milliseconds since the cap was chosen. A mark for another database    /// than the one asked for (nil: whichever is) says nothing — but the    /// launch's timeline takes the marks of whichever database it opens on.    static func mark(_ what: String, _ database: String? = nil) {        guard let asked, database == nil || database == asked.database || asked.database == launch,              !marked.contains(what) else { return }        marked.insert(what)        let elapsed = ContinuousClock.now - asked.at        let ms = Int((Double(elapsed.components.seconds) * 1000 + Double(elapsed.components.attoseconds) / 1e15).rounded())        log.notice("switch: \(asked.database, privacy: .public) \(what, privacy: .public) \(ms) ms")    }    /// The same, on the frame after this one: what was just set is drawn.    static func markDrawn(_ what: String, _ database: String? = nil) {        DispatchQueue.main.async { mark(what, database) }    }}#endif