Skip to main content

init_time_source

Function init_time_source 

Source
pub fn init_time_source(ts: Rc<dyn TimeSource>)
Expand description

Set the platform time source used for time-budget accounting.

If no TimeSource is registered the executor runs every flush to completion without yielding, which is acceptable for short-running workloads but may cause frame drops in the browser.

Examples found in repository?
examples/counter.rs (line 40)
37fn main() {
38    // One-time executor setup.
39    init_flush_scheduler(Rc::new(CliScheduler));
40    init_time_source(Rc::new(CliClock::new()));
41
42    // ---- Signal basics ----
43    println!("=== Signal basics ===");
44    let count = Signal::new(0);
45    println!("count = {}", count.read());
46    count.set(42);
47    println!("count = {}", count.read());
48
49    // ---- Memo (auto-tracking computed value) ----
50    println!("\n=== Memo ===");
51    let a = Signal::new(2);
52    let b = Signal::new(3);
53    let a2 = a.clone();
54    let b2 = b.clone();
55    let sum = Memo::new(move || a2.read() + b2.read());
56    println!("2 + 3 = {}", sum.read());
57    a.set(10);
58    println!("10 + 3 = {}", sum.read());
59
60    // ---- TaskScope: spawn a task that watches a signal ----
61    println!("\n=== TaskScope (scoped signal watcher) ===");
62    let messages = Signal::new(vec!["hello".to_string()]);
63
64    {
65        let scope = TaskScope::new();
66        let msgs = messages.clone();
67        scope.spawn(async move {
68            loop {
69                let val = msgs.changed().await;
70                println!("  task observed: {:?}", val);
71            }
72        });
73
74        messages.set(vec!["hello".to_string(), "world".to_string()]);
75        messages.set(vec![
76            "hello".to_string(),
77            "world".to_string(),
78            "!".to_string(),
79        ]);
80
81        // Explicit drop — cancels the task and cleans up resources.
82        drop(scope);
83    }
84    println!("(scope dropped — task and subscriptions cleaned up)");
85
86    // ---- Batch: multiple sets, one notification ----
87    println!("\n=== Batch ===");
88    let x = Signal::new(0);
89    let notifications = Rc::new(Cell::new(0u32));
90    let n = Rc::clone(&notifications);
91    let x2 = x.clone();
92
93    let scope = TaskScope::new();
94    scope.spawn(async move {
95        loop {
96            x2.changed().await;
97            n.set(n.get() + 1);
98        }
99    });
100
101    batch(|| {
102        x.set(1);
103        x.set(2);
104        x.set(3);
105    });
106    println!(
107        "After 3 sets in a batch: {} notification(s)",
108        notifications.get()
109    );
110    // Should be 1 — not 3.
111
112    println!("\nDone.");
113}