pub struct StatePublisher<T: DeltaState> { /* private fields */ }Expand description
Publishes application-state updates as DeltaStream packets or bytes.
A publisher owns the sending side of one state stream. It observes each new application state and emits either a snapshot or a delta packet according to the configured adaptive policy. The usual flow is:
application state -> Publisher -> snapshot or delta packet -> serialized bytes.
Use Publisher::encode for the simple byte-oriented API, or Publisher::update
when an integration needs direct access to packet metadata before serialization.
§Example
use delta_stream::{DeltaState, Publisher};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, DeltaState)]
struct State {
progress: u8,
}
let mut publisher = Publisher::<State>::new();
let bytes = publisher.encode(&State { progress: 10 })?;
assert!(!bytes.is_empty());Implementations§
Source§impl<T: DeltaState> StatePublisher<T>
impl<T: DeltaState> StatePublisher<T>
Sourcepub fn builder() -> PublisherBuilder<T>
pub fn builder() -> PublisherBuilder<T>
Starts building a publisher with custom settings.
Sourcepub fn with_policy(policy: AdaptivePolicy) -> Self
pub fn with_policy(policy: AdaptivePolicy) -> Self
Creates a publisher with an explicit adaptive encoding policy.
Sourcepub fn encode(&mut self, state: &T) -> Result<Vec<u8>, DeltaError>
pub fn encode(&mut self, state: &T) -> Result<Vec<u8>, DeltaError>
Encodes a new authoritative state into transport-independent bytes.
This is the high-level publishing path. It creates either a snapshot or delta
with the same logic as Publisher::update, then serializes that packet with
Packet::to_bytes. The returned bytes can be carried by any byte-capable
transport, including TCP, WebSocket, PubNub, MQTT, NATS, UDP with reliability
handled externally, files, IPC, or an in-memory channel.
Sourcepub fn update(&mut self, state: &T) -> Result<Packet, DeltaError>
pub fn update(&mut self, state: &T) -> Result<Packet, DeltaError>
Encodes a new authoritative state as a packet.
The first update is a snapshot. Later updates may be deltas or snapshots, and
raw or zstd-compressed, depending on the active AdaptivePolicy. This method
preserves the stream sequence, schema hash, base hash, and compression behavior
used by the lower-level packet API.
Sourcepub fn force_snapshot(&mut self, state: &T) -> Result<Packet, DeltaError>
pub fn force_snapshot(&mut self, state: &T) -> Result<Packet, DeltaError>
Forces a new snapshot and advances the stream sequence.
Sourcepub fn recovery_snapshot(&self, state: &T) -> Result<Packet, DeltaError>
pub fn recovery_snapshot(&self, state: &T) -> Result<Packet, DeltaError>
Builds a recovery snapshot at the current stream sequence.
This does not advance the publisher sequence, so recovering one client does not create a sequence gap for healthy clients.
Sourcepub fn last_decision(&self) -> Option<EncodeDecision>
pub fn last_decision(&self) -> Option<EncodeDecision>
Returns the most recent adaptive encoding decision, if one exists.