// One screen's writes.//// Every write on a scaffolded screen is one `write { tool, args }`, and what// comes back carries `tables` — which IS the invalidation (design P5). The// screen does not re-read for itself: the write lands, the core emits a// `data` event, and `ScreenPage`'s one subscription re-reads whatever claimed// those tables. A screen that re-read by hand would be a second answer to the// same question, and the two would eventually disagree.importCottonKitimportSwiftUI/// One screen's writes.////// Held by the screen, given the same core its read has. `busy` is what a/// button reads to disable itself, and `refusal` is the tool's own sentence —/// shown as the toast the Expo screens showed, never rewritten here.@MainActor@ObservablepublicfinalclassScreenWriter {publicprivate(set) var busy = false/// The last refusal, for a screen that shows one inline rather than as a/// toast. Cleared by the next call that succeeds.publicprivate(set) var refusal: String?privatelet core: any CottonCallingprivatelet reload: (@MainActor () async -> Void)?/// `reload` is the screen's own read, and it is called in ONE case: the/// write moved tables no family claims — see `run`.publicinit(core: any CottonCalling, reload: (@MainActor () async -> Void)? = nil) {self.core = coreself.reload = reload }/// Run one tool. `true` when it landed.////// Nothing is re-read here on purpose: `tables` in the answer is what the/// core turns into a `data` event, and the event is what re-reads every/// screen that claims those tables — including this one, and including the/// same record open on another device. @discardableResultpublicfunc run(_ tool: String, _ args: [String: JSONValue] = [:]) async -> Bool {await answering(tool, args) != nil }/// One METHOD on the bus that is not a tool — `capture.media`,/// `media.ensure`, `launch.create`, `session.create`, a `read.*` asked in/// another mode. The phone's own acts, which `write { tool }` does not/// carry.////// Passed through as it is: a method answers its own shape and refuses in/// its own words, so `busy` and `refusal` are not touched — the screen/// that asked reads the answer or catches the error, as it would have/// asking the core itself. It is HERE so that a screen never holds the/// core: its read is `ScreenRead`'s, its tools are `run`, and its methods/// are this.publicfunc call(_ method: String, _ request: someEncodable & Sendable = EmptyRequest()) asyncthrows -> JSONValue {tryawait core.callJSON(method, request) }/// The same call, decoded into the shape the BOUNDARY declares for it.////// A screen that read `answer.text("route")` would be spelling a key the/// boundary already owns, in the one module `ReadKeySweepTests` holds/// against the crate's own writers — and a method's answer shape is not a/// read's. So the shape lives beside the method name in `CottonKit`/// (`MeetingCreated` is the first), and this is how a screen gets one/// without holding the core.publicfunc call<Response: Decodable>( _ method: String, _ request: someEncodable & Sendable = EmptyRequest(),as _: Response.Type ) asyncthrows -> Response {tryawait core.call(method, request, as: Response.self) }/// The same call, keeping what the tool ANSWERED.////// For the one shape a single tool cannot express: a record minted here and/// then filed somewhere — `create_todo` then `link_to_project` — where the/// second call needs the first's id. `nil` is a refusal, and a caller that/// only wants "did it land" reads `run` instead.publicfunc answering(_ tool: String, _ args: [String: JSONValue] = [:]) async -> JSONValue? { busy = truedefer { busy = false }do {let answer: WriteAnswer = tryawait core.call(CottonMethod.write,WriteRequest(tool: tool, args: .object(args)) ) refusal = nil// WHERE THE MAP IS SILENT, THE SCREEN THAT WROTE RE-READS ITSELF.//// `InvalidationMap` is the Expo registry ported table for table,// and `InvalidationParityTests` holds the two equal in BOTH// directions — so a table the registry never named cannot be added// to it here. Several of this lane's tables are exactly that:// `cotton_pages`, the four news tables, `cotton_meetings`,// `cotton_stores`. On the Expo phone the mutation called// `invalidateQueries` for itself and the screen re-read; here the// `data` event is the only path, and for those tables it names no// family and nobody hears it.//// So: when the write moved something a family claims, the EVENT is// still the only re-read — two answers to one question is what// `ScreenPage`'s single subscription exists to avoid, and it reaches// the same record open on another screen as well. When it moved// nothing anyone claims, this screen asks again, because it is the// one screen that certainly must see its own write.ifInvalidationMap.families(for: answer.tables ?? []).isEmpty {await reload?() }// A tool that answers nothing still LANDED, so the absence of an// answer must not read as a refusal: `.null` is the value for// "it worked and had nothing to say".return answer.answer ?? .null } catchlet error asCottonCallError { refusal = error.messagereturnnil } catch { refusal = String(describing: error)returnnil } }}