Cotton code
71 lines · 3.5 KB · SwiftRaw
// The wire between a `data` event and a pushed screen's re-read.//// `RootView` owns the ONE `cotton_subscribe` handler and fans every// `ClientEvent` to four models. A pushed screen is not one of those four and// must not become one: 4g alone has thirty-one hosts, each holding a model// that lives and dies with its push, and a root-level fan-out would have to// know which of them is on screen — which is the stack's own question, not the// subscription's.//// So the sync facts travel DOWN, through the environment, and each host asks// the one question it can answer: *did the last fold touch me, and am I still// mounted?* `.rereading { model.reread($0) }` is that question, and it is the// only production caller of the twenty-eight `reread` members 4g wrote.//// **The trigger is `revision`, never `invalidated`.** `onChange` fires on a// change of VALUE, and two folds in a row that both touch `cotton_photos` set// the same two-element list twice — the second is not a change, and the grid// would keep drawing the library as it was before the second import. So the// counter says THAT something moved and the list says WHAT; `SyncModel` states// the same rule beside the counter.//// An unmounted host runs no `onChange` at all, so a screen that has been popped// costs nothing — which is the other half of why this is not a root fan-out.import CottonKitimport SwiftUI// **The environment key is 4e's, in `SyncModel.swift`.** Both lanes needed the// same thing at once and both wrote `EnvironmentValues.cottonSync`; 4e's landed// on main first, its reasoning is the same one written above, and two keys for// one fact is exactly the drift this file exists to close. What is here is only// the modifier — 4e's screens read `sync.revision` inline with a `noticed(_:)`// seam, and the thirty-one hosts below take the same fact through// `.rereading { … }` because a host owns a model rather than being one.private struct RereadOnDataEvent: ViewModifier {    @Environment(\.cottonSync) private var sync    /// The window's database: a `data` event from another database re-reads    /// nothing here (several-databases P8, `SyncModel.reaches`).    @Environment(DatabaseScene.self) private var scene: DatabaseScene?    let acrossDatabases: Bool    let reread: ([ScreenFamily]) -> Task<Void, Never>?    func body(content: Content) -> some View {        content.onChange(of: sync?.revision ?? 0) { _, _ in            guard let sync, sync.reaches(shown: scene?.shownId, acrossDatabases: acrossDatabases) else { return }            // The task is answered so a test can await it; on screen it is            // fire-and-forget, and the model's own `concerns` decides whether            // there is one at all.            _ = reread(sync.invalidated)        }    }}extension View {    /// Read again when a `data` event names a family this screen draws.    ///    /// Applied on the HOST rather than on the screen, because the host is what    /// owns the model: a screen is re-made on every redraw and a modifier there    /// would be watching a value that outlives it.    ///    /// Only for the database this window shows, unless `acrossDatabases`:    /// a screen that reads every held database (Today, search, "Asked of    /// you") re-reads for any of them.    public func rereading(        acrossDatabases: Bool = false,        _ reread: @escaping ([ScreenFamily]) -> Task<Void, Never>?    ) -> some View {        modifier(RereadOnDataEvent(acrossDatabases: acrossDatabases, reread: reread))    }}