-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.rs
More file actions
656 lines (545 loc) · 21.4 KB
/
Copy pathruntime.rs
File metadata and controls
656 lines (545 loc) · 21.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use tokio::sync::{mpsc, RwLock};
use crate::core::event_registry::EventRegistry;
use crate::core::view_tree::ViewTree;
use crate::hooks::hook_store::HookStore;
use crate::shared::ViewId;
use crate::views::view::{BuildContext, Element, View};
/// Message types for the runtime event loop.
#[derive(Debug)]
pub enum RuntimeMessage {
Event {
widget_id: String,
event_name: String,
args: serde_json::Value,
},
Navigate {
app_id: String,
state: serde_json::Value,
},
Rebuild {
view_id: ViewId,
},
Shutdown,
}
/// The application runtime manages the view tree, state, and event dispatch.
pub struct Runtime {
view_tree: ViewTree,
tree: Arc<RwLock<Option<Element>>>,
hook_stores: HashMap<ViewId, HookStore>,
dirty_views: HashSet<ViewId>,
event_registry: Arc<RwLock<EventRegistry>>,
nav_handler: Option<Arc<dyn Fn(String, serde_json::Value) + Send + Sync>>,
event_tx: mpsc::Sender<RuntimeMessage>,
event_rx: mpsc::Receiver<RuntimeMessage>,
rebuild_tx: mpsc::Sender<ViewId>,
rebuild_rx: mpsc::Receiver<ViewId>,
}
impl Runtime {
pub fn new(root: impl View) -> Self {
let (event_tx, event_rx) = mpsc::channel(2048);
let (rebuild_tx, rebuild_rx) = mpsc::channel(256);
let view_tree = ViewTree::new(Arc::new(root));
Runtime {
view_tree,
tree: Arc::new(RwLock::new(None)),
hook_stores: HashMap::new(),
dirty_views: HashSet::new(),
event_registry: Arc::new(RwLock::new(EventRegistry::new())),
nav_handler: None,
event_tx,
event_rx,
rebuild_tx,
rebuild_rx,
}
}
/// Get a sender for dispatching events to the runtime.
pub fn event_sender(&self) -> mpsc::Sender<RuntimeMessage> {
self.event_tx.clone()
}
/// Register a navigation handler that will be called when Navigate messages are received.
pub fn set_nav_handler<F>(&mut self, handler: F)
where
F: Fn(String, serde_json::Value) + Send + Sync + 'static,
{
self.nav_handler = Some(Arc::new(handler));
}
/// Get a clone of the rebuild sender (for passing to State handles).
pub fn rebuild_sender(&self) -> mpsc::Sender<ViewId> {
self.rebuild_tx.clone()
}
/// Build the entire view tree from root, or rebuild a specific subtree.
///
/// - `build(None)` — rebuilds from root (initial build)
/// - `build(Some(view_id))` — rebuilds only that view's subtree
pub async fn build(&mut self) -> Element {
self.build_view(None).await
}
/// Build a specific view (or root if None).
async fn build_view(&mut self, target_view_id: Option<ViewId>) -> Element {
let view_id = target_view_id.unwrap_or_else(|| self.view_tree.root_id());
// Get old children before rebuild (for cleanup detection)
let old_children: Vec<ViewId> = self.view_tree.children(&view_id);
// Synchronous build phase — construct element, extract registry and effects
let (element, registry, effects) = {
let store = self.hook_stores.entry(view_id).or_default();
let rebuild_tx = self.rebuild_tx.clone();
let mut ctx = BuildContext::with_view_id(store, Some(rebuild_tx), view_id);
ctx.reset();
// Clone the Arc<dyn View> so we can borrow view immutably while
// store is borrowed mutably — no raw pointer needed.
let view_clone = self
.view_tree
.get(&view_id)
.expect("view_id not in tree")
.view
.clone();
let element = view_clone.build(&mut ctx);
let mut element = element;
element.assign_ids(&mut ctx);
let registry = ctx.take_event_registry();
let effects = ctx.drain_effects();
(element, registry, effects)
// ctx is dropped here, before any .await
};
// Async phase — update event registry
{
let mut reg = self.event_registry.write().await;
*reg = registry;
}
// Execute effects
let store = self.hook_stores.get_mut(&view_id).unwrap();
for effect_record in effects {
let idx = effect_record.hook_index;
if let Some(entry) = store.effects.get_mut(&idx) {
if let Some(cleanup) = entry.cleanup.take() {
cleanup();
}
}
let cleanup = (effect_record.callback)();
if let Some(entry) = store.effects.get_mut(&idx) {
entry.cleanup = cleanup;
}
}
// Detect removed children and clean up their HookStores
let new_children: Vec<ViewId> = self.view_tree.children(&view_id);
for old_child in &old_children {
if !new_children.contains(old_child) {
let removed_ids = self.view_tree.remove(*old_child);
for removed_id in removed_ids {
if let Some(mut removed_store) = self.hook_stores.remove(&removed_id) {
removed_store.cleanup_all_effects();
}
}
}
}
// Remove from dirty set
self.dirty_views.remove(&view_id);
let mut tree = self.tree.write().await;
*tree = Some(element.clone());
element
}
/// Mark a view as dirty (needs rebuild).
pub fn mark_dirty(&mut self, view_id: ViewId) {
self.dirty_views.insert(view_id);
}
/// Run the event loop, processing messages until shutdown.
pub async fn run(&mut self) {
// Initial build
let _ = self.build().await;
loop {
tokio::select! {
msg = self.event_rx.recv() => {
match msg {
Some(RuntimeMessage::Event { widget_id, event_name, args }) => {
{
let registry = self.event_registry.read().await;
registry.dispatch(&widget_id, &event_name, args);
}
let _ = self.build().await;
}
Some(RuntimeMessage::Navigate { app_id, state }) => {
if let Some(handler) = &self.nav_handler {
handler(app_id, state);
}
let _ = self.build().await;
}
Some(RuntimeMessage::Rebuild { view_id }) => {
self.dirty_views.insert(view_id);
// For now, rebuild from root (future: targeted subtree rebuild)
let _ = self.build().await;
}
Some(RuntimeMessage::Shutdown) | None => break,
}
}
Some(view_id) = self.rebuild_rx.recv() => {
self.dirty_views.insert(view_id);
// Rebuild from root for correctness; the dirty_views set
// records which view triggered it for future targeted rebuilds
let _ = self.build().await;
}
}
}
// Cleanup all effects on shutdown
for (_, mut store) in self.hook_stores.drain() {
store.cleanup_all_effects();
}
}
/// Get the current widget tree as serialized JSON.
pub async fn current_tree(&self) -> Option<serde_json::Value> {
let tree = self.tree.read().await;
tree.as_ref()
.map(|el| serde_json::to_value(el).unwrap_or_default())
}
/// Get read access to the view tree.
pub fn view_tree(&self) -> &ViewTree {
&self.view_tree
}
/// Get mutable access to the hook stores (for child_view to access).
pub fn hook_stores_mut(&mut self) -> &mut HashMap<ViewId, HookStore> {
&mut self.hook_stores
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::views::view::{BuildContext, Element, View};
use crate::widgets::button::Button;
use crate::widgets::text::TextBlock;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Mutex;
struct TestView;
impl View for TestView {
fn build(&self, _ctx: &mut BuildContext) -> Element {
Element::Widget(Box::new(TextBlock::new("Hello from runtime")))
}
}
#[tokio::test]
async fn test_runtime_build() {
let mut runtime = Runtime::new(TestView);
let tree = runtime.build().await;
let json = serde_json::to_value(&tree).unwrap();
assert!(json.to_string().contains("Hello from runtime"));
}
#[tokio::test]
async fn test_runtime_build_assigns_ids_automatically() {
use crate::widgets::layout::Layout;
struct AutoIdView;
impl View for AutoIdView {
fn build(&self, _ctx: &mut BuildContext) -> Element {
Layout::vertical()
.child(TextBlock::new("First"))
.child(TextBlock::new("Second"))
.child(Button::new("Click"))
.into()
}
}
let mut runtime = Runtime::new(AutoIdView);
let tree = runtime.build().await;
let json = serde_json::to_value(&tree).unwrap();
let json_str = json.to_string();
assert!(json_str.contains("\"id\":\"w-0\""));
assert!(json_str.contains("\"id\":\"w-1\""));
assert!(json_str.contains("\"id\":\"w-2\""));
assert!(json_str.contains("\"id\":\"w-3\""));
}
#[tokio::test]
async fn test_runtime_auto_id_registers_events() {
let clicked = Arc::new(AtomicBool::new(false));
let clicked_clone = clicked.clone();
struct AutoClickView {
clicked: Arc<AtomicBool>,
}
impl View for AutoClickView {
fn build(&self, _ctx: &mut BuildContext) -> Element {
let clicked = self.clicked.clone();
Button::new("Auto Click")
.on_click(move || {
clicked.store(true, Ordering::SeqCst);
})
.into()
}
}
let mut runtime = Runtime::new(AutoClickView {
clicked: clicked_clone.clone(),
});
let _ = runtime.build().await;
let tx = runtime.event_sender();
tx.send(RuntimeMessage::Event {
widget_id: "w-0".to_string(),
event_name: "click".to_string(),
args: serde_json::Value::Null,
})
.await
.unwrap();
tokio::spawn(async move {
runtime.run().await;
});
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
assert!(clicked_clone.load(Ordering::SeqCst));
}
#[tokio::test]
async fn test_button_click_dispatched() {
let clicked = Arc::new(AtomicBool::new(false));
let clicked_clone = clicked.clone();
struct ClickView {
clicked: Arc<AtomicBool>,
}
impl View for ClickView {
fn build(&self, _ctx: &mut BuildContext) -> Element {
let clicked = self.clicked.clone();
Button::new("Click me")
.on_click(move || {
clicked.store(true, Ordering::SeqCst);
})
.into()
}
}
let mut runtime = Runtime::new(ClickView {
clicked: clicked_clone.clone(),
});
let _ = runtime.build().await;
let tx = runtime.event_sender();
tx.send(RuntimeMessage::Event {
widget_id: "w-0".to_string(),
event_name: "click".to_string(),
args: serde_json::Value::Null,
})
.await
.unwrap();
tokio::spawn(async move {
runtime.run().await;
});
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
assert!(clicked_clone.load(Ordering::SeqCst));
}
#[tokio::test]
async fn test_text_input_change_dispatched() {
let received = Arc::new(Mutex::new(String::new()));
let received_clone = received.clone();
struct InputView {
received: Arc<Mutex<String>>,
}
impl View for InputView {
fn build(&self, _ctx: &mut BuildContext) -> Element {
let received = self.received.clone();
crate::widgets::input::TextInput::new()
.on_change(move |val| {
let mut r = received.lock().unwrap();
*r = val;
})
.into()
}
}
let mut runtime = Runtime::new(InputView {
received: received_clone.clone(),
});
let _ = runtime.build().await;
let tx = runtime.event_sender();
tx.send(RuntimeMessage::Event {
widget_id: "w-0".to_string(),
event_name: "change".to_string(),
args: serde_json::json!({"value": "hello world"}),
})
.await
.unwrap();
tokio::spawn(async move {
runtime.run().await;
});
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
let val = received_clone.lock().unwrap();
assert_eq!(*val, "hello world");
}
#[tokio::test]
async fn test_rebuild_carries_view_id() {
use crate::hooks::use_state::use_state;
struct StatefulView;
impl View for StatefulView {
fn build(&self, ctx: &mut BuildContext) -> Element {
let count = use_state(ctx, 0i32);
Element::Widget(Box::new(TextBlock::new(&format!("Count: {}", count.get()))))
}
}
let mut runtime = Runtime::new(StatefulView);
let _ = runtime.build().await;
// The root view_id is now tracked in the view_tree
let root_id = runtime.view_tree().root_id();
assert!(runtime.hook_stores_mut().contains_key(&root_id));
}
#[test]
fn test_child_view_independent_hooks() {
use crate::hooks::use_state::use_state;
struct ChildView;
impl View for ChildView {
fn build(&self, ctx: &mut BuildContext) -> Element {
let count = use_state(ctx, 100i32);
Element::Widget(Box::new(TextBlock::new(&format!("Child: {}", count.get()))))
}
}
let mut parent_store = HookStore::new();
let parent_view_id = uuid::Uuid::new_v4();
let mut ctx = BuildContext::with_view_id(&mut parent_store, None, parent_view_id);
// Parent uses its own state
let parent_state = use_state(&mut ctx, 0i32);
parent_state.set(42);
// Embed a child view — it should get its own isolated HookStore
let (child_element, child_id, child_store) = ctx.child_view(ChildView, None);
// Verify the child produced its element
let json = serde_json::to_value(&child_element).unwrap().to_string();
assert!(json.contains("Child: 100"));
// Verify the child got a different ViewId
assert_ne!(parent_view_id, child_id);
// Verify the child has its own HookStore with state
assert!(
!child_store.states.is_empty(),
"Child should have its own state"
);
// Parent state should still be 42
assert_eq!(parent_state.get(), 42);
}
#[tokio::test]
async fn test_subtree_rebuild_only() {
use crate::hooks::use_state::use_state;
use std::sync::atomic::{AtomicUsize, Ordering};
let root_build_count = Arc::new(AtomicUsize::new(0));
let root_build_count_clone = root_build_count.clone();
struct CountingView {
build_count: Arc<AtomicUsize>,
}
impl View for CountingView {
fn build(&self, ctx: &mut BuildContext) -> Element {
self.build_count.fetch_add(1, Ordering::SeqCst);
let count = use_state(ctx, 0i32);
Element::Widget(Box::new(TextBlock::new(&format!("Count: {}", count.get()))))
}
}
let mut runtime = Runtime::new(CountingView {
build_count: root_build_count_clone,
});
// Initial build
let _ = runtime.build().await;
assert_eq!(root_build_count.load(Ordering::SeqCst), 1);
// Second build (full rebuild) should increment
let _ = runtime.build().await;
assert_eq!(root_build_count.load(Ordering::SeqCst), 2);
// The dirty_views mechanism is in place — when state changes trigger
// rebuild via ViewId, the runtime knows which view changed
let root_id = runtime.view_tree().root_id();
runtime.mark_dirty(root_id);
assert!(runtime.dirty_views.contains(&root_id));
}
#[test]
fn test_context_ancestor_walk() {
use crate::hooks::use_context::{create_context, use_context};
#[derive(Clone, Debug, PartialEq)]
struct Theme {
color: String,
}
struct ChildView;
impl View for ChildView {
fn build(&self, ctx: &mut BuildContext) -> Element {
let theme: Theme = use_context(ctx);
Element::Widget(Box::new(TextBlock::new(&format!("Theme: {}", theme.color))))
}
}
let mut parent_store = HookStore::new();
let parent_view_id = uuid::Uuid::new_v4();
let mut ctx = BuildContext::with_view_id(&mut parent_store, None, parent_view_id);
// Create context in parent
create_context(
&mut ctx,
Theme {
color: "blue".to_string(),
},
);
// Child view should be able to read the parent's context via ancestor walk
let (child_element, _child_id, _child_store) = ctx.child_view(ChildView, None);
let json = serde_json::to_value(&child_element).unwrap().to_string();
assert!(
json.contains("Theme: blue"),
"Expected child to read parent context, got: {}",
json
);
}
#[tokio::test]
async fn test_navigate_dispatches_to_handler() {
let received_app_id = Arc::new(Mutex::new(String::new()));
let received_state = Arc::new(Mutex::new(serde_json::Value::Null));
let app_id_clone = received_app_id.clone();
let state_clone = received_state.clone();
let mut runtime = Runtime::new(TestView);
runtime.set_nav_handler(move |app_id, state| {
*app_id_clone.lock().unwrap() = app_id;
*state_clone.lock().unwrap() = state;
});
let _ = runtime.build().await;
let tx = runtime.event_sender();
tx.send(RuntimeMessage::Navigate {
app_id: "my-app".to_string(),
state: serde_json::json!({"path": "/home"}),
})
.await
.unwrap();
tx.send(RuntimeMessage::Shutdown).await.unwrap();
runtime.run().await;
assert_eq!(*received_app_id.lock().unwrap(), "my-app");
assert_eq!(
*received_state.lock().unwrap(),
serde_json::json!({"path": "/home"})
);
}
#[tokio::test]
async fn test_navigate_without_handler_is_noop() {
let mut runtime = Runtime::new(TestView);
let _ = runtime.build().await;
let tx = runtime.event_sender();
tx.send(RuntimeMessage::Navigate {
app_id: "my-app".to_string(),
state: serde_json::json!({"path": "/about"}),
})
.await
.unwrap();
tx.send(RuntimeMessage::Shutdown).await.unwrap();
// Should not panic — navigate without handler is a no-op
runtime.run().await;
// Verify the runtime still works — tree should exist after rebuild
let tree = runtime.current_tree().await;
assert!(tree.is_some());
}
#[test]
fn test_cleanup_on_view_removal() {
use crate::core::view_tree::ViewTree;
use std::sync::atomic::{AtomicBool, Ordering};
let cleanup_called = Arc::new(AtomicBool::new(false));
let cleanup_called_clone = cleanup_called.clone();
// Create a view tree with a child
let mut tree = ViewTree::new(Arc::new(|_ctx: &mut BuildContext| Element::Empty));
let root_id = tree.root_id();
let child_id = tree.insert(root_id, Arc::new(|_ctx: &mut BuildContext| Element::Empty));
// Set up HookStores with an effect cleanup for the child
let mut hook_stores: HashMap<ViewId, HookStore> = HashMap::new();
let child_store = hook_stores.entry(child_id).or_default();
child_store.effects.insert(
0,
crate::hooks::hook_store::EffectEntry {
prev_deps: None,
cleanup: Some(Box::new(move || {
cleanup_called_clone.store(true, Ordering::SeqCst);
})),
has_run: true,
},
);
// Remove the child from the tree
let removed = tree.remove(child_id);
assert!(removed.contains(&child_id));
// Clean up the removed HookStores
for removed_id in removed {
if let Some(mut store) = hook_stores.remove(&removed_id) {
store.cleanup_all_effects();
}
}
// Verify effect cleanup was called
assert!(cleanup_called.load(Ordering::SeqCst));
}
}