Cotton code
91 lines · 4.2 KB · SwiftRaw
// THE COUNTER SAYS WHAT A TOOL'S ROWS ARE DOING (owner, 2 Nov 2026): the// deck's counter top-left ("1 / 135") says, while rows of the showing tool's// table are arriving or leaving, what is happening — "347 of 950 todos// arriving", "25 todos sending" — and is the plain count again once the// table is settled. Same place, same type, same ink; no new control. So Home// can go later; Home stays the one sync card for now.//// The core decides: This Device's live read (`device.facts`, the read Home's// device card draws) carries `sync_table_progress`, each table that is moving// by its id — `arriving`/`arrived` while the pass has rows of it to come,// `sending` while this phone has writes of it queued — and names no settled// table. The words are the kit's (`cards.deck.moving`).import CottonKitimport CottonValuesimport Foundation/// What the showing table's rows are doing.enum DeckMoving: Equatable {    /// Nothing of it is moving: the plain count.    case settled    /// The pass has rows of it still to come: `arrived` of `arriving` have.    case arriving(arrived: Int, of: Int)    /// This phone has `n` writes of it waiting to go up.    case sending(Int)    /// `table`'s state in `progress` (`sync_table_progress`): arriving before    /// sending, as rows coming is what the deck is short of.    static func of(_ table: String, in progress: RecordValue?) -> DeckMoving {        guard case .object(let tables)? = progress, case .object(let moving)? = tables[table] else { return .settled }        func count(_ key: String) -> Int? {            if case .number(let n)? = moving[key] { return Int(n) }            return nil        }        if let of = count("arriving"), let arrived = count("arrived"), arrived < of { return .arriving(arrived: arrived, of: of) }        if let n = count("sending"), n > 0 { return .sending(n) }        return .settled    }    /// Whether the counter reads again after reading `now`: while the table    /// moves, and while a pass is in flight — its pages land inside one    /// cycle, so its first read comes before the object has said a table's    /// figures (walk 3, 2 Nov 2026: a read that stopped at "settled" never    /// saw the rows arriving).    static func readsAgain(_ now: DeckMoving, passing: Bool) -> Bool {        now != .settled || passing    }    /// The counter's words for this state, of a table named `name` (one)    /// and `plural`; nil when settled — the plain count stands.    func line(name: String, plural: String) -> String? {        let one = name.lowercased(), many = plural.lowercased()        switch self {        case .settled:            return nil        case .arriving(let arrived, let of):            return ShellLines.line("cards", of == 1 ? "deck.moving.arriving_one" : "deck.moving.arriving",                                   ["at": String(arrived), "of": String(of), "name": one, "plural": many])        case .sending(let n):            return ShellLines.line("cards", n == 1 ? "deck.moving.sending_one" : "deck.moving.sending",                                   ["n": String(n), "name": one, "plural": many])        }    }}enum DeckMovingKnobs {    /// Seconds between reads while the showing table is moving.    static let every: Double = 1.5}/// This Device's `sync_table_progress`, as its live read says it now; nil/// when this build does not sync or the read fails (the plain count stands).enum DeckMovingRead {    private struct Facts: Decodable { let data: [String: RecordValue] }    /// Whether this build syncs at all: Rigby keeps no object and reads none.    static let syncs = FlavourManifest.current?.sync ?? false    @MainActor private static var thisDevice: String?    @MainActor static func read(_ core: (any CottonCalling)?) async -> RecordValue? {        guard syncs, let core else { return nil }        if thisDevice == nil {            thisDevice = (try? await core.callJSON(CottonMethod.readDevices))?.untrimmed("thisDevice")        }        guard let id = thisDevice,              let facts = try? await core.call(CottonMethod.deviceFacts, ["deviceId": RecordValue.text(id)], as: Facts.self)        else { return nil }        return facts.data["sync_table_progress"]    }}