Cotton code
40 lines · 1.7 KB · SwiftRaw
// The wire between "bytes landed on this phone" and a picture that said they// had not.//// `read.mediaPath` never fetches, so a tile that answers `absent` is honest —// and, until this file, PERMANENT: the eager sweep collected six hundred// megabytes of thumbnails and no screen learned of a single one until the// person happened to rebuild the view. The crate now sends the same `data`// event a pull carries, naming `_media`, whenever bytes move on this device// (the sweep's tick, a tap's `media.ensure`, the budget's eviction); Swift maps// `_media` to `.storage`; and `SyncModel.mediaEpoch` counts those. This is the// one modifier that watches the counter.//// **Only the ABSENT ask again.** A picture that is drawn stays drawn — a// re-decode over it is a flicker the person can see — so every caller guards// on its own slot's `isAbsent` before it re-asks, and `MediaArrivalSweepTests`// holds every renderer to wearing this modifier at all.import SwiftUIprivate struct ReaskOnMediaArrival: ViewModifier {    @Environment(\.cottonSync) private var sync    let reask: () async -> Void    func body(content: Content) -> some View {        content.onChange(of: sync?.mediaEpoch ?? 0) { _, _ in            Task { await reask() }        }    }}extension View {    /// Ask `read.mediaPath` again when bytes have moved on this device.    ///    /// The caller decides whether it has anything to ask — a slot that is    /// `.ready` returns at once. `.task(id: key)` remains the FIRST ask; this    /// is the second and every later one.    public func onMediaArrival(_ reask: @escaping () async -> Void) -> some View {        modifier(ReaskOnMediaArrival(reask: reask))    }}