Struct async_utils::hanging_get::server::HangingGetInner
source · pub struct HangingGetInner<S, K, O, F: Fn(&S, O) -> bool> { /* private fields */ }
Expand description
A HangingGetInner object manages some internal state S
and notifies observers of type O
when their view of the state is outdated.
While it can be used directly, most API consumers will want to use the higher level
HangingGet object. HangingGet
and its companion types provide Send
for use from multiple threads or async tasks.
§Type parameters
K
- the Key by which Observers are identified.- For other type args see HangingGet.
Implementations§
source§impl<S, K, O, F> HangingGetInner<S, K, O, F>
impl<S, K, O, F> HangingGetInner<S, K, O, F>
sourcepub fn new(state: Option<S>, notify: F) -> Self
pub fn new(state: Option<S>, notify: F) -> Self
Create a new HangingGetInner
.
state
is the initial state of the HangingGetInner
notify
is a Fn
that is used to notify observers of state.
sourcepub fn set(&mut self, state: S)
pub fn set(&mut self, state: S)
Set the internal state value to state
and notify all observers.
Note that notification is not conditional on the value set by calling the set
function.
Notification will occur regardless of whether the state
value differs from the value
currently stored by HangingGetInner
.
sourcepub fn update(&mut self, state_update: impl FnOnce(&mut Option<S>) -> bool)
pub fn update(&mut self, state_update: impl FnOnce(&mut Option<S>) -> bool)
Modify the internal state in-place using the state_update
function. Notify all
observers if state_update
returns true.
sourcepub fn subscribe(
&mut self,
key: K,
observer: O,
) -> Result<(), HangingGetServerError>
pub fn subscribe( &mut self, key: K, observer: O, ) -> Result<(), HangingGetServerError>
Register an observer as a subscriber of the state.
Observers are grouped by key and all observers will the same key are assumed to have received the latest state update. If an observer with a previously unseen key subscribes, it is immediately notified to the stated. If an observer with a known key subscribes, it will only be notified when the state is updated since last sent to an observer with the same key. All unresolved observers will be resolved to the same value immediately after the state is updated. If there is no stored state, then the notification will be delayed until an update is made.
sourcepub fn unsubscribe(&mut self, key: K)
pub fn unsubscribe(&mut self, key: K)
Deregister all observers that subscribed with key
. If an observer is subsequently
subscribed with the same key
value, it will be treated as a previously unseen key
.