pub struct HangingGet<S, O, F: Fn(&S, O) -> bool> { /* private fields */ }
Expand description
A broker of updates to some state, using the hanging get pattern.
The broker stores a state, and mediates state changes between Publishers and Subscribers. Any Publisher can change the state at any time. When the state is changed, all Subscribers are notified with the new state.
To follow the hanging get pattern, when a Subscriber is first created, the current state is sent immediately. Subsequent updates to the same subscriber are only sent when the state is modified.
- Use HangingGet::new to create a new broker for a single state item.
- Use HangingGet::new_publisher to create an object that allows you to update the state.
- Use HangingGet::new_subscriber to create an object that monitors for updates to the state.
§Type parameters
S
: the type of the stored hanging get state. This is the state that gets communicated to subscribers.O
: The type of the object used to notify of state change.F
: The type of a function used to send the new state content to an instance ofO
.F
gets passed the content of the new state, the object that it needs to notify, and is expected to returntrue
if the notification was a success; otherwise it must returnfalse
.
Implementations§
Source§impl<S, O, F> HangingGet<S, O, F>
impl<S, O, F> HangingGet<S, O, F>
Sourcepub fn new(state: S, notify: F) -> Self
pub fn new(state: S, notify: F) -> Self
Create a new broker.
§Args:
state
is the initial state of the HangingGet.notify
is a function to notify observers of state of the state change.
Sourcepub fn new_unknown_state(notify: F) -> Self
pub fn new_unknown_state(notify: F) -> Self
Create a new broker, but delays any subscribers from being notified until the value is initialized.
§Args:
notify
is a function to notify observers of state of the state change.
§Disclaimer:
This initialier is more prone to cause hangs if code is not properly setup. This should only be used for patterns where the is no useful default state.
Sourcepub fn new_publisher(&self) -> Publisher<S, O, F>
pub fn new_publisher(&self) -> Publisher<S, O, F>
Create a new Publisher that can make atomic updates to the state value.
Sourcepub fn new_subscriber(&mut self) -> Subscriber<S, O, F>
pub fn new_subscriber(&mut self) -> Subscriber<S, O, F>
Create a new Subscriber that represents a single hanging get client.
The newly-created subscriber will be notified with the current state immediately. After the first notification, the subscriber will be notified only if the state changes.