pub struct Producer<T> { /* private fields */ }Expand description
Publishes a JSON value over a track, choosing snapshots and deltas automatically.
An Encoder that owns its track: it writes each encoded frame and rolls a group whenever the
encoder emits a snapshot. When something else already owns the track, use the Encoder
directly.
A group runs until a joiner wants a fresh one, which poll_refresh answers, or until
it reaches the encoder’s caps. Set the track’s refresh ratio
when creating it, or joins never ask.
Set compression on that track’s info too: JSON is small and
repetitive, and moq-net compresses each delta against the snapshot earlier in its group.
Cheaply clonable: clones share one underlying track and publishing state, like other MoQ producers.
Implementations§
Source§impl<T: Serialize> Producer<T>
impl<T: Serialize> Producer<T>
Sourcepub fn new(track: Producer, config: Config) -> Self
pub fn new(track: Producer, config: Config) -> Self
Create a producer that publishes to the given track.
Sourcepub fn poll_refresh(&self, waiter: &Waiter) -> Poll<Result<()>>
pub fn poll_refresh(&self, waiter: &Waiter) -> Poll<Result<()>>
Wait for a joiner to want a fresh group, then restate the current value as a snapshot in one.
A join wants one once the open group’s deltas outweigh the track’s
refresh ratio times its snapshot (see
Demand::wanted). It is a soft trigger: that joiner still
reads the cached group, then the new one. The snapshot carries the newest timestamp written,
where its group starts; while that is the newest group’s own start, the refresh waits for a
later update, since two groups never share a start. Nothing happens before the first update,
or on a track without a ratio.
The producer has no runtime of its own, so the owner drives this: loop on it, or select on it beside the updates. Errors once the track is closed.
Sourcepub async fn refresh(&self) -> Result<()>
pub async fn refresh(&self) -> Result<()>
Wait for a joiner to want a fresh group, then restate the current value as a snapshot in one.
A join wants one once the open group’s deltas outweigh the track’s
refresh ratio times its snapshot (see
Demand::wanted). It is a soft trigger: that joiner still
reads the cached group, then the new one. The snapshot carries the newest timestamp written,
where its group starts; while that is the newest group’s own start, the refresh waits for a
later update, since two groups never share a start. Nothing happens before the first update,
or on a track without a ratio.
The producer has no runtime of its own, so the owner drives this: loop on it, or select on it beside the updates. Errors once the track is closed.
Awaits poll_refresh.
Sourcepub fn update(&mut self, value: &T, at: Timestamp) -> Result<Option<usize>>
pub fn update(&mut self, value: &T, at: Timestamp) -> Result<Option<usize>>
Publish a new value at at (see Timed::at), emitting a snapshot or a delta automatically.
Returns the encoded size of the frame written, or None if the value is unchanged from the
previous publish and nothing was written.
Sourcepub fn mutate(&mut self, f: impl FnOnce(&mut T), at: Timestamp) -> Result<()>where
T: Default + DeserializeOwned,
pub fn mutate(&mut self, f: impl FnOnce(&mut T), at: Timestamp) -> Result<()>where
T: Default + DeserializeOwned,
Edit the current value in place and publish it at at (see Timed::at).
The closure receives the current value: everything published through this producer so far,
composed, or T::default() if nothing has been. Edit it in place; on return the result is
published, a no-op if unchanged.
producer.mutate(|catalog| {
catalog["scte35"] = serde_json::json!({ "id": 1 });
}, moq_net::Timestamp::from_millis(100).unwrap())Independent owners can share one producer and each edit only their own keys: every call starts
from the latest value, so their sections compose instead of clobbering one another. This is
modify opened and committed for you; take the guard to hold the lock across
several edits, and update to replace the whole value.
Sourcepub fn modify(&mut self, at: Timestamp) -> Result<Guard<'_, T>>where
T: Default + DeserializeOwned,
pub fn modify(&mut self, at: Timestamp) -> Result<Guard<'_, T>>where
T: Default + DeserializeOwned,
Edit the current value, publishing on drop at at (see Timed::at).
The returned Guard derefs to the current value: everything published through this producer
so far, composed, or T::default() if nothing has been. Editing it through DerefMut marks
the guard dirty; when a dirty guard drops it publishes the result, a no-op if unchanged.
A rejected edit never reached the wire, so the current value stays the last one that did. The owner whose write failed sees the error and can retry; carrying the rejected edit forward would instead fail every later owner’s publish the same way, or publish a value its owner was told failed.
Reach for this over mutate when several edits belong to one publish. The
guard holds the producer’s lock for its lifetime, so independent owners are serialized: each
one starts from the latest value and their changes compose instead of clobbering. Don’t hold a
guard across an .await, since that keeps the lock held while suspended.
Fails if the track is closed. That is the one publication failure that happens in normal
operation, and the guard holds the lock that finish needs, so nothing is
left to check after the guard drops. Anything else that stops the drop from publishing (a
value that won’t serialize, or one too large for a frame) aborts the track with that error:
consumers see it instead of a stale value, and the next modify returns it here. Call
Guard::commit to get the error back immediately instead. A panic while the guard is held
discards the in-flight edit rather than publishing a torn value.
Sourcepub fn cut(&mut self) -> Result<()>
pub fn cut(&mut self) -> Result<()>
Close the open group, so the deltas already written stop being provisional.
No replacement group opens until the next update or poll_refresh, which emits a full
snapshot as its first frame even when the value is unchanged. A consumer joining at that group
therefore reads the whole value without the deltas that preceded it.
Idempotent: cutting when no group is open does nothing, so a caller can cut on its own schedule without tracking what has been published since the last one. Inert when deltas are disabled, where every frame already gets its own group.