Cotton code
108 lines · 4.3 KB · SwiftRaw
// A screen's board, read (design note 26203e21): `definitions.board` answers// the person's own row for the screen, or this build's shipped default, and// `BoardSurface` draws whatever came back. Until the answer lands, and when// the call fails, the screen draws the Swift copy of the shipped default// (`BoardDefinition.home/.tools/.database`), so a board is never blank and// never waits on a read to draw its first frame.//// A board row moves with the `.core` family (`InvalidationMap`), so a fold// that names `cotton_board_definitions` reads the board again in place.import CottonKitimport Foundationimport Observation@MainActor@Observablefinal class BoardModel: Rereadable {    /// The screen's board id: "home", "tools", "database".    let id: String    /// What the screen draws now: the row the core answered, or this build's own.    private(set) var board: BoardDefinition    let families: Set<ScreenFamily> = [.core]    @ObservationIgnored private let boards: any BoardsCalling    /// The newest read asked: an older answer landing late is dropped.    @ObservationIgnored private var asking = 0    init(boards: any BoardsCalling, id: String) {        self.boards = boards        self.id = id        self.board = BoardDefinition.shipped(id)    }    func load() async {        asking += 1        let asked = asking        do {            let answer = try await boards.board(id)            guard asked == asking else { return }            if let why = answer.passedOver {                BoardFeedback.warnOnce(                    "board \(id): the stored board was passed over (\(why)); drawing the shipped one.",                    key: "row:\(id):\(why)")            }            guard let drawn = DefinitionCatalogue.board(answer) else {                BoardFeedback.warnOnce(                    "board \(id): the answer held no board this build can read; drawing its own.",                    key: "unreadable:\(id)")                return            }            board = drawn        } catch {            // The board this build ships stays drawn: a screen never goes blank            // because its layout could not be read.            Log.warn("board \(id): the board could not be read (\(error)); drawing this build's own")        }    }}/// A board pushed over Home (Tools, the database page): the core's answer/// for it, read as it shows and again when a fold names what it draws.@MainActor@Observablefinal class PushedBoardParts: Rereadable {    private(set) var parts: [BoardEntry] = []    let families: Set<ScreenFamily> = [.core]    @ObservationIgnored private let read: @MainActor () async -> [BoardEntry]?    init(read: @escaping @MainActor () async -> [BoardEntry]?) { self.read = read }    func load() async {        if let parts = await read() { self.parts = parts }    }}/// What a board passed over, said ONCE per launch. A board is drawn on every/// visit; a word per visit would bury the one fact under copies of itself.@MainActorenum BoardFeedback {    private static var said: Set<String> = []    /// True the first time `key` is seen in this process, false after.    static func firstTime(_ key: String) -> Bool {        said.insert(key).inserted    }    /// A part of a kind this build does not draw: one row in the Feedback    /// tool (`definitions.passedOver`, which files at most one per database,    /// board and part, ever), asked once per launch.    static func passedOver(part: String, board: String, databaseId: String?, databaseName: String?, boards: (any BoardsCalling)?) {        guard firstTime("part:\(databaseId ?? "personal"):\(board):\(part)") else { return }        Log.warn("board \(board): a part of kind \"\(part)\" is not drawn by this build; passed over.")        guard let boards else { return }        Task {            do {                try await boards.passedOver(board: board, part: part, databaseId: databaseId ?? "", databaseName: databaseName ?? "")            } catch {                Log.warn("board \(board): the passed-over \"\(part)\" could not be filed (\(error))")            }        }    }    /// Something about a board read worth one line in the log, once.    static func warnOnce(_ message: String, key: String) {        guard firstTime(key) else { return }        Log.warn(message)    }}