Skip to main content

mln_engine/tile_store/
loading.rs

1//! What the engine keeps of a tile's loading, beside its origin's state of
2//! it. Each load takes a value of the source's counter, and its replies
3//! arrive in the store's inbox naming it; the store hands one to the tile
4//! only while that load is the tile's latest.
5
6use std::any::Any;
7
8use crate::storage::chrono::Timestamp;
9use crate::storage::resource::RequestOrder;
10use crate::tile_store::tile::{TileLoadOrder, TileNecessity};
11
12/// What a tile's origin says of the times of its data: when it was
13/// modified, when it expires, and until when it may be shown at all, which
14/// the cache keeps to.
15#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
16pub struct TileTimes {
17    /// When the data was last modified, if known.
18    pub modified: Option<Timestamp>,
19    /// When the data stops being fresh; `None` if it never does, or isn't
20    /// known.
21    pub expires: Option<Timestamp>,
22    /// When the data may no longer be shown until its origin loads it
23    /// again; `None` if it always may. A tile in the cache whose time has
24    /// come goes as the view needs it again, rather than show its data,
25    /// and a new tile loads in its place.
26    pub usable_until: Option<Timestamp>,
27}
28
29impl TileTimes {
30    /// Whether the data may still be shown at `now`.
31    pub(crate) fn is_usable(&self, now: Timestamp) -> bool {
32        self.usable_until.is_none_or(|until| until > now)
33    }
34}
35
36/// A tile's loading: what the cover asks of it, its latest load, and its
37/// origin's state of it.
38pub(crate) struct Load {
39    /// How much the cover last needed the tile.
40    pub(crate) necessity: TileNecessity,
41    /// When the tile's requests start among those waiting, shared with
42    /// them.
43    pub(crate) order: RequestOrder,
44    /// The load whose replies apply.
45    pub(crate) current: Option<u64>,
46    /// The tile has done what it can while optional.
47    pub(crate) tried_optional: bool,
48    /// The origin's last word on the tile was that its load failed and
49    /// waits for a retry
50    /// ([`LoadContext::fail_retrying`](crate::source::LoadContext::fail_retrying)),
51    /// and nothing has begun or brought anything since. Such a tile isn't
52    /// loading: the cache lets go of it rather than wait for it.
53    pub(crate) failed: bool,
54    pub(crate) times: TileTimes,
55    /// The origin's state of the tile: `None` until the origin starts the
56    /// tile, and while one of its hooks has it.
57    pub(crate) state: Option<Box<dyn Any>>,
58}
59
60impl Load {
61    /// A new tile's loading, whose requests wait behind every other tile's
62    /// until a cover retains it.
63    pub(crate) fn new() -> Self {
64        let order = RequestOrder::default();
65        order.set(TileLoadOrder::UNWANTED.value());
66        Load {
67            necessity: TileNecessity::Optional,
68            order,
69            current: None,
70            tried_optional: false,
71            failed: false,
72            times: TileTimes::default(),
73            state: None,
74        }
75    }
76
77    /// Whether the replies of load `load` apply.
78    pub(crate) fn awaits(&self, load: u64) -> bool {
79        self.current == Some(load)
80    }
81
82    /// Sets when the tile's network request starts among those waiting,
83    /// including one that waits now.
84    pub(crate) fn set_order(&self, order: TileLoadOrder) {
85        self.order.set(order.value());
86    }
87}