pub struct HangingGet<S, K, O, F: Fn(&S, O) -> bool> { /* private fields */ }
Expand description
Deprecated: New code should use [async_utils::hanging_get::server::HangingGet
].
TODO(https://fxbug.dev/42055741): Remove this struct.
A HangingGet
object manages some internal state S
and notifies observers of type O
when their view of the state is outdated.
S
- the type of State to be watched
O
- the type of Observers of S
F
- the type of observer notification behavior, where F: Fn(&S, O)
K
- the Key by which Observers are identified
While it can be used directly, most API consumers will want to use the higher level
HangingGetBroker
object. HangingGetBroker
and its companion types provide Send
for use from multiple threads or async tasks.
Implementations§
Source§impl<S, K, O, F> HangingGet<S, K, O, F>
impl<S, K, O, F> HangingGet<S, K, O, F>
Sourcepub fn new(state: S, notify: F) -> Self
pub fn new(state: S, notify: F) -> Self
Create a new HangingGet
.
state
is the initial state of the HangingGet
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 HangingGet
.
Sourcepub fn update(&mut self, state_update: impl FnOnce(&mut S) -> bool)
pub fn update(&mut self, state_update: impl FnOnce(&mut 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.
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
.