pub struct EventStream<T> { /* private fields */ }Expand description
A stream of discrete events. It has no current value.
Implementations§
Source§impl<T> EventStream<T>where
T: 'static,
impl<T> EventStream<T>where
T: 'static,
Sourcepub fn debounce(&self, timer: Rc<dyn Timer>, window: Duration) -> EventStream<T>
pub fn debounce(&self, timer: Rc<dyn Timer>, window: Duration) -> EventStream<T>
Emit only after the stream has been quiet for window.
Each event restarts the window; when it elapses without a newer event, the latest event is emitted. Superseded timer firings are no-ops.
Source§impl<T> EventStream<T>where
T: 'static,
impl<T> EventStream<T>where
T: 'static,
Sourcepub fn observe<F>(&self, f: F) -> Subscription
pub fn observe<F>(&self, f: F) -> Subscription
Observe this stream. The callback fires for every event while the
returned Subscription is alive. Streams are started by their first
observer and stopped when the last observer is removed.
Examples found in repository?
14fn main() {
15 const ROWS: usize = 100_000;
16 const UPDATES: usize = 10_000;
17
18 let model = ListModel::new();
19 let events = Rc::new(Cell::new(0usize));
20 let events_rc = events.clone();
21 let _sub = model
22 .changes()
23 .observe(move |_| events_rc.set(events_rc.get() + 1));
24
25 let rows: Vec<String> = (0..ROWS).map(|i| format!("row-{i:05}")).collect();
26
27 let started = Instant::now();
28 model.replace_all(rows);
29 let load = started.elapsed();
30
31 let started = Instant::now();
32 for i in 0..UPDATES {
33 model.set(i % ROWS, format!("updated-{i:05}"));
34 }
35 let updates = started.elapsed();
36
37 let started = Instant::now();
38 model.sort_by(|a, b| a.cmp(b));
39 let sort = started.elapsed();
40
41 println!(
42 "rows={ROWS} updates={UPDATES} change-events={} ({} Reset + {UPDATES} Update, no rebuilds)\n\
43 load={load:?} {UPDATES} per-row updates={updates:?} sort={sort:?}",
44 events.get(),
45 events.get() - UPDATES
46 );
47}More examples
11fn main() {
12 // Counter state.
13 let count = Var::new(0);
14
15 // Derived state: a doubled value and a validation message.
16 let doubled = count.signal().map(|c| c * 2);
17 let validation = count.signal().map(|c| {
18 if *c < 0 {
19 "negative"
20 } else if *c > 10 {
21 "too large"
22 } else {
23 "ok"
24 }
25 });
26
27 // Event sampling: what was the count when a "click" happened?
28 let clicks = Sink::new();
29 let clicked_value = clicks.stream().sample(&count.signal());
30
31 let log: Rc<RefCell<Vec<(i32, &str)>>> = Rc::new(RefCell::new(Vec::new()));
32 let log_rc = log.clone();
33 let count_rc = count.clone();
34 let validation_rc = validation.clone();
35 let _count_sub = count_rc.signal().observe(move |value| {
36 log_rc.borrow_mut().push((*value, *validation_rc.value()));
37 });
38 let log_rc = log.clone();
39 let _clicked_sub = clicked_value.observe(move |value| {
40 log_rc.borrow_mut().push((*value, "sampled"));
41 });
42
43 count.set(5);
44 clicks.send(());
45 count.set(12);
46
47 assert_eq!(*doubled.value(), 24);
48 assert_eq!(*validation.value(), "too large");
49 assert_eq!(
50 *log.borrow(),
51 vec![(0, "ok"), (5, "ok"), (5, "sampled"), (12, "too large")]
52 );
53 println!(
54 "counter={} doubled={} validation={}",
55 count.value(),
56 doubled.value(),
57 validation.value()
58 );
59}Sourcepub fn observe_with(&self, observer: &Observer<T>) -> Subscription
pub fn observe_with(&self, observer: &Observer<T>) -> Subscription
Observe this stream with a shared Observer.
Sourcepub fn map<U, F>(&self, f: F) -> EventStream<U>
pub fn map<U, F>(&self, f: F) -> EventStream<U>
Map every event.
Sourcepub fn filter<F>(&self, f: F) -> EventStream<T>
pub fn filter<F>(&self, f: F) -> EventStream<T>
Keep only events that satisfy the predicate.
Sourcepub fn filter_map<U, F>(&self, f: F) -> EventStream<U>
pub fn filter_map<U, F>(&self, f: F) -> EventStream<U>
Map and filter in one step.
Sourcepub fn fold<U, F>(&self, init: U, f: F) -> EventStream<U>
pub fn fold<U, F>(&self, init: U, f: F) -> EventStream<U>
Emit the running fold of every event.
Sourcepub fn distinct(&self) -> EventStream<T>where
T: PartialEq,
pub fn distinct(&self) -> EventStream<T>where
T: PartialEq,
Drop consecutive duplicates.
Sourcepub fn merge(&self, other: &EventStream<T>) -> EventStream<T>
pub fn merge(&self, other: &EventStream<T>) -> EventStream<T>
Merge events from two streams, preserving arrival order.
Sourcepub fn merge_all(streams: Vec<EventStream<T>>) -> EventStream<T>
pub fn merge_all(streams: Vec<EventStream<T>>) -> EventStream<T>
Merge events from many streams, preserving arrival order.
Sourcepub fn sample<S>(&self, signal: &Signal<S>) -> EventStream<S>where
S: 'static,
pub fn sample<S>(&self, signal: &Signal<S>) -> EventStream<S>where
S: 'static,
Emit the sampled signal’s current value whenever this stream fires.
Examples found in repository?
11fn main() {
12 // Counter state.
13 let count = Var::new(0);
14
15 // Derived state: a doubled value and a validation message.
16 let doubled = count.signal().map(|c| c * 2);
17 let validation = count.signal().map(|c| {
18 if *c < 0 {
19 "negative"
20 } else if *c > 10 {
21 "too large"
22 } else {
23 "ok"
24 }
25 });
26
27 // Event sampling: what was the count when a "click" happened?
28 let clicks = Sink::new();
29 let clicked_value = clicks.stream().sample(&count.signal());
30
31 let log: Rc<RefCell<Vec<(i32, &str)>>> = Rc::new(RefCell::new(Vec::new()));
32 let log_rc = log.clone();
33 let count_rc = count.clone();
34 let validation_rc = validation.clone();
35 let _count_sub = count_rc.signal().observe(move |value| {
36 log_rc.borrow_mut().push((*value, *validation_rc.value()));
37 });
38 let log_rc = log.clone();
39 let _clicked_sub = clicked_value.observe(move |value| {
40 log_rc.borrow_mut().push((*value, "sampled"));
41 });
42
43 count.set(5);
44 clicks.send(());
45 count.set(12);
46
47 assert_eq!(*doubled.value(), 24);
48 assert_eq!(*validation.value(), "too large");
49 assert_eq!(
50 *log.borrow(),
51 vec![(0, "ok"), (5, "ok"), (5, "sampled"), (12, "too large")]
52 );
53 println!(
54 "counter={} doubled={} validation={}",
55 count.value(),
56 doubled.value(),
57 validation.value()
58 );
59}Sourcepub fn start_with(&self, initial: T) -> Signal<T>where
T: PartialEq,
pub fn start_with(&self, initial: T) -> Signal<T>where
T: PartialEq,
Turn this stream into a signal that starts with initial and holds the
latest event.
Sourcepub fn flat_map_switch<U, F>(&self, f: F) -> EventStream<U>
pub fn flat_map_switch<U, F>(&self, f: F) -> EventStream<U>
Switch to the latest inner stream produced by f; only the current
inner stream’s events are emitted.