Cotton code
39 lines · 1.5 KB · SwiftRaw
import Foundation/// One accepted interaction. Its id stays the same if transport retries it.public struct UsageEvent: Encodable, Sendable {    public let id: String    public let kind: String    public let tool: String    public let view: String    public let record: String?    public init(id: String = UUID().uuidString, kind: String, tool: String, view: String = "", record: String? = nil) {        self.id = id; self.kind = kind; self.tool = tool; self.view = view; self.record = record    }}/// One place the person went lately (`read.recentVisits`): a tool's page by/// its view, or a record by its tool and id. The newest open stands for it.public struct RecentVisit: Decodable, Hashable, Sendable {    public enum Target: String, Decodable, Sendable { case tool, record }    public let target: Target    public let tool: String    public let view: String    public let record: String?    public let at: String}private struct RecentVisits: Decodable { let visits: [RecentVisit] }private struct RecentRequest: Encodable { let limit: Int }extension CottonCalling {    public func recordUsage(_ event: UsageEvent) async throws {        _ = try await callJSON(CottonMethod.usageRecord, event)    }    /// The person's last opens, newest first, one per target — always from    /// the personal database, whichever database Home shows.    public func recentVisits(limit: Int) async throws -> [RecentVisit] {        try await call(CottonMethod.readRecentVisits, RecentRequest(limit: limit), as: RecentVisits.self).visits    }}