// The protocol under every screen model's re-read. // // A `data` event names the screen families whose tables moved. A model that // draws one of them reads again; one that does not costs nothing. Before the // scaffold, that decision was written thirty times as a `static concerns` and // a `reread` whose bodies were byte-identical but for the type name — and a // model that forgot the pair (eleven did) was a screen that never re-read at // all, which `ScreenRead`'s header calls broken rather than stale. // // So the pair lives here, once, over two facts a model already has: the // families it claims and its one `load()`. `Hosted` applies `.rereading` to // every model that conforms, so conforming IS being on the wire. // // **`families` and `concerns` are instance members.** `ScreenRead`'s were // always per instance (a read is built with its families), and the models // that shared one static set did so by delegating to a sibling — the instance // form fits both without a second spelling. import CottonKit import Foundation @MainActor public protocol Rereadable: AnyObject { /// The families this model draws — read off `InvalidationMap` where a test /// can hold them to the tables, never listed from memory. var families: Set { get } /// The model's one read. A model with two reads behind one screen runs /// both here, so a fold that names either re-reads the whole page. func load() async /// Read again if the fold touched us. A REQUIREMENT, with the usual body /// below as its default, so a model that answers a family differently — /// the library, whose `.storage` re-asks thumbnails and reads nothing — /// is the one `Hosted` calls. As an extension method alone, `Hosted`'s /// generic call dispatched statically to the default and a model's own /// `reread` was never run on screen. @discardableResult func reread(_ invalidated: [ScreenFamily]) -> Task? /// Whether this model reads every held database (Today, search, "Asked /// of you") rather than the one its window shows — so a `data` event from /// any database re-reads it. A requirement with `false` as its default, /// for `reread`'s reason: `Hosted` asks it generically. var readsAcrossDatabases: Bool { get } } extension Rereadable { public var readsAcrossDatabases: Bool { false } /// Whether a `data` event that invalidated these families is one of ours. public func concerns(_ invalidated: [ScreenFamily]) -> Bool { invalidated.contains(where: families.contains) } /// Read again if the fold touched us. Answers the task it started so a /// test can `await model.reread([.money])?.value` rather than sleep; on /// screen it is fire-and-forget. `nil` is "not ours", and costs nothing. @discardableResult public func reread(_ invalidated: [ScreenFamily]) -> Task? { guard concerns(invalidated) else { return nil } return Task { [weak self] in await self?.load() } } }