pub struct ValueWriter<T: Serialize> { /* private fields */ }Expand description
Writes a latest-value JSON track, stamping each update with the time it is made.
Each update gets the moq clock’s time, Timestamp::from(Instant::now())
at the track’s timescale, or one tick after the previous stamp if the clock
has not moved past it. A tick is a millisecond on a track that counts
milliseconds, as the tracks iroh-live creates do. The stamps strictly
increase, as the track’s group starts must, so a burst of updates within
one millisecond runs a few milliseconds ahead of the clock and meets it
again once the updates slow down.
The track must be timed: on a track without a
timescale, every update fails with
moq_net::Error::TimestampMismatch.
Clones share the track and the stamps, and each update stamps and writes under one lock, so clones on several threads cannot reorder their stamps. The last clone to drop finishes the track, so readers see it end.
Implementations§
Source§impl<T: Serialize> ValueWriter<T>
impl<T: Serialize> ValueWriter<T>
Sourcepub fn new(track: Producer, config: Config) -> Self
pub fn new(track: Producer, config: Config) -> Self
Creates a writer that publishes to track.
The track must be timed, and nothing else should write to it.
Sourcepub fn update(&self, value: &T) -> Result<Option<usize>, ValueError>
pub fn update(&self, value: &T) -> Result<Option<usize>, ValueError>
Publishes value, stamped now.
Returns the encoded size of the frame written, or None if value
equals the current value and nothing was written.
§Errors
Fails with moq_net::Error::Closed once the track is finished,
with moq_net::Error::TimestampMismatch on an untimed track, and
with any error of snapshot::Producer::update.
Sourcepub fn update_at(
&self,
value: &T,
at: Timestamp,
) -> Result<Option<usize>, ValueError>
pub fn update_at( &self, value: &T, at: Timestamp, ) -> Result<Option<usize>, ValueError>
Publishes value at at, a time the caller chose.
The caller owns the order: an at that starts a group at or before
the previous group fails with moq_net::Error::TimestampMismatch. A
later update stamps after at.
§Errors
As update, and fails with
moq_net::Error::TimestampMismatch for an at the track cannot
take.
Sourcepub fn mutate(&self, edit: impl FnOnce(&mut T)) -> Result<(), ValueError>where
T: Default + DeserializeOwned,
pub fn mutate(&self, edit: impl FnOnce(&mut T)) -> Result<(), ValueError>where
T: Default + DeserializeOwned,
Edits the current value in place and publishes it, stamped now.
The closure receives the current value: everything published so far,
or T::default() if nothing has been. An unchanged value publishes
nothing. See snapshot::Producer::mutate.
§Errors
As update, and fails if the current value does not
deserialize as T.
Sourcepub fn finish(self) -> Result<(), ValueError>
pub fn finish(self) -> Result<(), ValueError>
Finishes the track, so readers see it end.
Clones then fail to update with moq_net::Error::Closed. Dropping
the last clone finishes the track too, so call this only to see the
error or to end the track while clones remain.
§Errors
Fails if the track was aborted, or if finishing it fails.