// A count as the core says it (`count.words`,// `crates/cotton-ffi/src/methods/shell_lines.rs`): the number grouped, its// plural form picked by CLDR, said through a line set or a definition's// units. The app never words a count (contract 7: Swift draws).//// The core answers asynchronously and a face asks while it draws, so the// words are held here once said: the first ask sends the call and answers// nil, the answer lands as an observed change and the face draws again. The// held words are let go when the shown database changes or a sync lands,// because the database's `lines` rows may have reworded them.importCottonKitimportFoundation@MainActor@ObservablepublicfinalclassCountLines {publicstaticlet shared = CountLines() @ObservationIgnored privatevar core: (any CottonCalling)?privatevar said: [String: String] = [:] @ObservationIgnored privatevar asking: Set<String> = []/// The core to ask; the words held so far are let go.publicfunc use(_ core: any CottonCalling) {self.core = core said = [:] asking = [] }/// The line `line` of `set` filled with the count `n` (and `fills`), the/// form the count calls for; nil until the core has said it.publicfunc line(_ set: String, _ line: String, n: Int, fills: [String: String] = [:]) -> String? { ask(Request(n: n, set: set, line: line, fills: fills.isEmpty ? nil : fills, units: nil)) }/// The count `n` in a definition's units ("6 orders", "Sold out"); nil/// until the core has said it.publicfunc units(_ units: ColumnUnits, n: Int) -> String? { ask(Request(n: n, set: nil, line: nil, fills: nil, units: units)) }privatefunc ask(_ request: Request) -> String? {guardlet key = request.key else { returnnil }iflet words = said[key] { return words }guardlet core, !asking.contains(key) else { returnnil } asking.insert(key)Task { [weakself] inlet words = try? await core.call(CottonMethod.countWords, request, as: Said.self).wordsguardletselfelse { return }self.asking.remove(key)iflet words { self.said[key] = words } }returnnil }structRequest: Encodable, Sendable {let n: Intlet set: String?let line: String?let fills: [String: String]?let units: ColumnUnits?var key: String? {let encoder = JSONEncoder() encoder.outputFormatting = .sortedKeysreturn (try? encoder.encode(self)).map { String(decoding: $0, as: UTF8.self) } } }structSaid: Decodable, Sendable {let words: String }}