Struct reactive_rs::Broadcast
source · Expand description
Event source that transmits context/value pairs to multiple observers.
In order to “fork” the broadcast (creating a new stream that will
be subscribed to it), the broadcast object can be simply cloned
via the Clone
trait. Note that cloning the broadcast only
increases its reference count; no values are being cloned or copied.
A broadcast may receive a value in one of two ways. First, the user
may explicitly call one of its methods: send()
, send_ctx()
,
feed()
, feed_ctx()
. Second, the broadcast may be created
from a parent stream via broadcast()
method of the stream object. Either way, each context/value pair received
is passed on to each of the subscribed observers, by reference.
Examples
let out = RefCell::new(Vec::new());
let stream = SimpleBroadcast::<i32>::new();
let child1 = stream
.clone()
.subscribe(|x| out.borrow_mut().push(*x + 1));
let child2 = stream
.clone()
.subscribe(|x| out.borrow_mut().push(*x + 7));
stream.feed(1..=3);
assert_eq!(&*out.borrow(), &[2, 8, 3, 9, 4, 10]);
Implementations§
source§impl<'a, C: 'a + ?Sized, T: 'a + ?Sized> Broadcast<'a, C, T>
impl<'a, C: 'a + ?Sized, T: 'a + ?Sized> Broadcast<'a, C, T>
sourcepub fn from_stream<S>(stream: S) -> Selfwhere
S: Stream<'a, Context = C, Item = T>,
pub fn from_stream<S>(stream: S) -> Selfwhere
S: Stream<'a, Context = C, Item = T>,
Create a broadcast from a stream, enabling multiple observers (“fork” the stream).
Note: this is equivalent to calling
broadcast()
on the stream object.
sourcepub fn send_ctx<K, B>(&self, ctx: K, value: B)where
K: Borrow<C>,
B: Borrow<T>,
pub fn send_ctx<K, B>(&self, ctx: K, value: B)where
K: Borrow<C>,
B: Borrow<T>,
Send a value along with context to all observers of the broadcast.
sourcepub fn send<B>(&self, value: B)where
B: Borrow<T>,
C: Default,
pub fn send<B>(&self, value: B)where
B: Borrow<T>,
C: Default,
Similar to send_ctx()
, but the context is set to the type’s default value.
Trait Implementations§
source§impl<'a, C: 'a + ?Sized, T: 'a + ?Sized> Stream<'a> for Broadcast<'a, C, T>
impl<'a, C: 'a + ?Sized, T: 'a + ?Sized> Stream<'a> for Broadcast<'a, C, T>
source§fn subscribe_ctx<O>(self, observer: O)where
O: FnMut(&Self::Context, &Self::Item) + 'a,
fn subscribe_ctx<O>(self, observer: O)where
O: FnMut(&Self::Context, &Self::Item) + 'a,
subscribe()
, but the closure receives two arguments
(context/value), by reference. Read moresource§fn subscribe<O>(self, observer: O)where
O: 'a + FnMut(&Self::Item),
fn subscribe<O>(self, observer: O)where
O: 'a + FnMut(&Self::Item),
source§fn broadcast(self) -> Broadcast<'a, Self::Context, Self::Item>where
Self: 'a,
fn broadcast(self) -> Broadcast<'a, Self::Context, Self::Item>where
Self: 'a,
Stream
trait method that incurs a slight runtime cost, due to the broadcast
object having to store observers as boxed trait objects in a reference-counted
container; all other methods can be inlined. Read moresource§fn ctx(self) -> Context<Self>
fn ctx(self) -> Context<Self>
source§fn with_ctx<T>(self, ctx: T) -> WithContext<Self, T>
fn with_ctx<T>(self, ctx: T) -> WithContext<Self, T>
source§fn with_ctx_map<F, T>(self, func: F) -> WithContextMap<Self, F>where
F: 'a + FnMut(&Self::Context, &Self::Item) -> T,
fn with_ctx_map<F, T>(self, func: F) -> WithContextMap<Self, F>where
F: 'a + FnMut(&Self::Context, &Self::Item) -> T,
source§fn map<F, T>(self, func: F) -> Map<Self, NoContext<F>>where
F: 'a + FnMut(&Self::Item) -> T,
fn map<F, T>(self, func: F) -> Map<Self, NoContext<F>>where
F: 'a + FnMut(&Self::Item) -> T,
source§fn map_ctx<F, T>(self, func: F) -> Map<Self, F>where
F: 'a + FnMut(&Self::Context, &Self::Item) -> T,
fn map_ctx<F, T>(self, func: F) -> Map<Self, F>where
F: 'a + FnMut(&Self::Context, &Self::Item) -> T,
map()
, but the closure receives two arguments
(context/value), by reference. Read moresource§fn map_both<F, C, T>(self, func: F) -> MapBoth<Self, NoContext<F>>where
F: 'a + FnMut(&Self::Item) -> (C, T),
fn map_both<F, C, T>(self, func: F) -> MapBoth<Self, NoContext<F>>where
F: 'a + FnMut(&Self::Item) -> (C, T),
map()
, but the closure is expected to return a (context, value)
tuple, so that both the context and the value can be changed at the same time. Read moresource§fn map_both_ctx<F, C, T>(self, func: F) -> MapBoth<Self, F>where
F: 'a + FnMut(&Self::Context, &Self::Item) -> (C, T),
fn map_both_ctx<F, C, T>(self, func: F) -> MapBoth<Self, F>where
F: 'a + FnMut(&Self::Context, &Self::Item) -> (C, T),
map_both()
, but the closure receives two arguments
(context/value), by reference. Read moresource§fn filter<F>(self, func: F) -> Filter<Self, NoContext<F>>where
F: 'a + FnMut(&Self::Item) -> bool,
fn filter<F>(self, func: F) -> Filter<Self, NoContext<F>>where
F: 'a + FnMut(&Self::Item) -> bool,
source§fn filter_ctx<F>(self, func: F) -> Filter<Self, F>where
F: 'a + FnMut(&Self::Context, &Self::Item) -> bool,
fn filter_ctx<F>(self, func: F) -> Filter<Self, F>where
F: 'a + FnMut(&Self::Context, &Self::Item) -> bool,
filter()
, but the closure receives two arguments
(context/value), by reference. Read moresource§fn filter_map<F, T>(self, func: F) -> FilterMap<Self, NoContext<F>>where
F: 'a + FnMut(&Self::Item) -> Option<T>,
fn filter_map<F, T>(self, func: F) -> FilterMap<Self, NoContext<F>>where
F: 'a + FnMut(&Self::Item) -> Option<T>,
source§fn filter_map_ctx<F, T>(self, func: F) -> FilterMap<Self, F>where
F: 'a + FnMut(&Self::Context, &Self::Item) -> Option<T>,
fn filter_map_ctx<F, T>(self, func: F) -> FilterMap<Self, F>where
F: 'a + FnMut(&Self::Context, &Self::Item) -> Option<T>,
filter_map()
, but the closure receives two arguments
(context/value), by reference. Read moresource§fn fold<F, T: 'a>(self, init: T, func: F) -> Fold<Self, NoContext<F>, T>where
F: 'a + FnMut(&T, &Self::Item) -> T,
fn fold<F, T: 'a>(self, init: T, func: F) -> Fold<Self, NoContext<F>, T>where
F: 'a + FnMut(&T, &Self::Item) -> T,
source§fn fold_ctx<F, T: 'a>(self, init: T, func: F) -> Fold<Self, F, T>where
F: 'a + FnMut(&Self::Context, &T, &Self::Item) -> T,
fn fold_ctx<F, T: 'a>(self, init: T, func: F) -> Fold<Self, F, T>where
F: 'a + FnMut(&Self::Context, &T, &Self::Item) -> T,
fold()
, but the closure receives three arguments
(context/accumulator/value), by reference. Read more