pub struct Subscriber<T, L: Lock = SyncLock> { /* private fields */ }
Expand description
A subscriber for updates of an Observable
.
Implementations§
Source§impl<T> Subscriber<T>
impl<T> Subscriber<T>
Sourcepub fn next(&mut self) -> Next<'_, T> ⓘwhere
T: Clone,
pub fn next(&mut self) -> Next<'_, T> ⓘwhere
T: Clone,
Wait for an update and get a clone of the updated value.
Awaiting returns Some(_)
after an update happened, or None
after the
Observable
(and all clones for shared::Observable
) is dropped.
This method is a convenience so you don’t have to import a Stream
extension trait such as futures::StreamExt
or
tokio_stream::StreamExt
.
Sourcepub fn next_now(&mut self) -> Twhere
T: Clone,
pub fn next_now(&mut self) -> Twhere
T: Clone,
Get a clone of the inner value without waiting for an update.
If the returned value has not been observed by this subscriber before,
it is marked as observed such that a subsequent call of
next
or next_ref
won’t return the
same value again. See get
for a function that doesn’t
mark the value as observed.
Sourcepub async fn next_ref(&mut self) -> Option<ObservableReadGuard<'_, T>>
pub async fn next_ref(&mut self) -> Option<ObservableReadGuard<'_, T>>
Wait for an update and get a read lock for the updated value.
Awaiting returns Some(_)
after an update happened, or None
after the
Observable
(and all clones for shared::Observable
) is dropped.
You can use this method to get updates of an Observable
where the
inner type does not implement Clone
. However, the Observable
will be locked (not updateable) while any read guards are alive.
Sourcepub fn next_ref_now(&mut self) -> ObservableReadGuard<'_, T>
pub fn next_ref_now(&mut self) -> ObservableReadGuard<'_, T>
Lock the inner value for reading without waiting for an update.
Note that as long as the returned ObservableReadGuard
is kept alive,
the associated Observable
is locked and can not be updated.
If the returned value has not been observed by this subscriber before,
it is marked as observed such that a subsequent call of
next
or next_ref
won’t return the
same value again. See get
for a function that doesn’t
mark the value as observed.
Sourcepub fn read(&self) -> ObservableReadGuard<'_, T>
pub fn read(&self) -> ObservableReadGuard<'_, T>
Lock the inner value for reading without waiting for an update.
Note that as long as the returned ObservableReadGuard
is kept alive,
the associated Observable
is locked and can not be updated.
If the returned value has not been observed by this subscriber before,
it is not marked as observed such that a subsequent call of
next
or next_ref
will return the
same value again.
Source§impl<T, L: Lock> Subscriber<T, L>
impl<T, L: Lock> Subscriber<T, L>
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Reset the observed version of the inner value.
After calling this, it is guaranteed that the next call to
.next().await
or .next_ref().await
will resolve immediately.
This is only useful if you do this before passing the subscriber to some
other generic function or returning it, if you would be calling
.next().await
right afterwards, you can call
.next_now()
instead (same for .reset()
plus
.next_ref().await
, which can be expressed by
.next_ref_now()
).
Sourcepub fn clone_reset(&self) -> Selfwhere
L::SubscriberState<T>: Clone,
pub fn clone_reset(&self) -> Selfwhere
L::SubscriberState<T>: Clone,
Trait Implementations§
Source§impl<T, L: Lock> Clone for Subscriber<T, L>where
L::SubscriberState<T>: Clone,
Clone this Subscriber
exactly, including the observed version of the inner
value.
impl<T, L: Lock> Clone for Subscriber<T, L>where
L::SubscriberState<T>: Clone,
Clone this Subscriber
exactly, including the observed version of the inner
value.
That means that if the original Subscriber
was up-to-date with the latest
value of the observable, the new one will be as well, and vice-versa.
See clone_reset
for a convenient way of making a new
Subscriber
from an existing one without inheriting the observed version of
the inner value.