Skip to main content

freya_core/
runner.rs

1use std::{
2    any::TypeId,
3    cell::RefCell,
4    cmp::Ordering,
5    collections::{
6        HashMap,
7        HashSet,
8        VecDeque,
9    },
10    fmt::Debug,
11    rc::Rc,
12    sync::atomic::AtomicU64,
13};
14
15use futures_lite::{
16    FutureExt,
17    StreamExt,
18};
19use itertools::Itertools;
20use pathgraph::PathGraph;
21use rustc_hash::{
22    FxHashMap,
23    FxHashSet,
24};
25
26use crate::{
27    current_context::CurrentContext,
28    diff_key::DiffKey,
29    element::{
30        Element,
31        ElementExt,
32        EventHandlerType,
33    },
34    events::{
35        data::{
36            Event,
37            EventType,
38        },
39        name::EventName,
40    },
41    node_id::NodeId,
42    path_element::PathElement,
43    prelude::{
44        Task,
45        TaskId,
46    },
47    reactive_context::ReactiveContext,
48    scope::{
49        PathNode,
50        Scope,
51        ScopeStorage,
52    },
53    scope_id::ScopeId,
54    tree::DiffModifies,
55};
56
57#[derive(Debug, PartialEq, Eq)]
58pub enum MutationRemove {
59    /// Because elements always have a different parent we can easily get their position relatively to their parent
60    Element { id: NodeId, index: u32 },
61    /// In the other hand, roots of Scopes are manually connected to their parent scopes, so getting their index is not worth the effort.
62    Scope { id: NodeId },
63}
64
65impl PartialOrd for MutationRemove {
66    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
67        Some(self.cmp(other))
68    }
69}
70
71impl Ord for MutationRemove {
72    fn cmp(&self, other: &Self) -> Ordering {
73        use MutationRemove::*;
74        match (self, other) {
75            // Order Element removals by index descending (so larger indices come first)
76            (Element { index: a, .. }, Element { index: b, .. }) => b.cmp(a),
77            // Elements come before Scopes
78            (Element { .. }, Scope { .. }) => Ordering::Less,
79            (Scope { .. }, Element { .. }) => Ordering::Greater,
80            // Order Scopes by id descending as well
81            (Scope { id: a }, Scope { id: b }) => b.cmp(a),
82        }
83    }
84}
85
86impl MutationRemove {
87    pub fn node_id(&self) -> NodeId {
88        match self {
89            Self::Element { id, .. } => *id,
90            Self::Scope { id } => *id,
91        }
92    }
93}
94
95pub struct MutationAdd {
96    pub node_id: NodeId,
97    pub parent_id: NodeId,
98    pub index: u32,
99    pub element: Rc<dyn ElementExt>,
100}
101
102pub struct MutationModified {
103    pub node_id: NodeId,
104    pub element: Rc<dyn ElementExt>,
105    pub flags: DiffModifies,
106}
107
108#[derive(Debug)]
109pub struct MutationMove {
110    pub index: u32,
111    pub node_id: NodeId,
112}
113
114#[derive(Default)]
115pub struct Mutations {
116    pub added: Vec<MutationAdd>,
117    pub modified: Vec<MutationModified>,
118    pub removed: Vec<MutationRemove>,
119    pub moved: HashMap<NodeId, Vec<MutationMove>>,
120}
121
122impl Debug for Mutations {
123    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
124        f.write_fmt(format_args!(
125            "Added: {:?} Modified: {:?} Removed: {:?} Moved: {:?}",
126            self.added
127                .iter()
128                .map(|m| (m.node_id, m.parent_id, m.index))
129                .collect::<Vec<_>>(),
130            self.modified.iter().map(|m| m.node_id).collect::<Vec<_>>(),
131            self.removed,
132            self.moved
133                .iter()
134                .map(|(parent_id, moves)| {
135                    (
136                        parent_id,
137                        moves
138                            .iter()
139                            .map(|m| (m.index, m.node_id))
140                            .collect::<Vec<_>>(),
141                    )
142                })
143                .collect::<Vec<_>>()
144        ))
145    }
146}
147
148pub enum Message {
149    MarkScopeAsDirty(ScopeId),
150    PollTask(TaskId),
151}
152
153pub struct Runner {
154    pub scopes: FxHashMap<ScopeId, Rc<RefCell<Scope>>>,
155    pub scopes_storages: Rc<RefCell<FxHashMap<ScopeId, ScopeStorage>>>,
156
157    pub(crate) dirty_scopes: FxHashSet<ScopeId>,
158    pub(crate) dirty_tasks: VecDeque<TaskId>,
159
160    pub node_to_scope: FxHashMap<NodeId, ScopeId>,
161
162    pub(crate) node_id_counter: NodeId,
163    pub(crate) scope_id_counter: ScopeId,
164    pub(crate) task_id_counter: Rc<AtomicU64>,
165
166    pub(crate) tasks: Rc<RefCell<FxHashMap<TaskId, Rc<RefCell<Task>>>>>,
167
168    pub(crate) sender: futures_channel::mpsc::UnboundedSender<Message>,
169    pub(crate) receiver: futures_channel::mpsc::UnboundedReceiver<Message>,
170}
171
172impl Debug for Runner {
173    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
174        f.debug_struct("Runner")
175            .field("dirty_scopes", &self.dirty_scopes.len())
176            .field("dirty_tasks", &self.dirty_tasks.len())
177            .field("node_to_scope", &self.node_to_scope.len())
178            .field("scopes", &self.scopes.len())
179            .field("scopes_storages", &self.scopes_storages.borrow().len())
180            .field("tasks", &self.tasks.borrow().len())
181            .finish()
182    }
183}
184
185impl Drop for Runner {
186    fn drop(&mut self) {
187        // Graceful shutdown of scopes based on their height, starting from the deepest
188        for (scope_id, _scope) in self
189            .scopes
190            .drain()
191            .sorted_by_key(|s| s.1.borrow().height)
192            .rev()
193        {
194            CurrentContext::run_with_reactive(
195                CurrentContext {
196                    scope_id,
197                    scopes_storages: self.scopes_storages.clone(),
198                    tasks: self.tasks.clone(),
199                    task_id_counter: self.task_id_counter.clone(),
200                    sender: self.sender.clone(),
201                },
202                || {
203                    let mut _removed_tasks = Vec::new();
204
205                    self.tasks.borrow_mut().retain(|task_id, task| {
206                        if task.borrow().scope_id == scope_id {
207                            _removed_tasks.push((*task_id, task.clone()));
208                            false
209                        } else {
210                            true
211                        }
212                    });
213                    drop(_removed_tasks);
214                    let _scope = self.scopes_storages.borrow_mut().remove(&scope_id);
215                },
216            );
217        }
218    }
219}
220
221impl Runner {
222    pub fn new(root: impl Fn() -> Element + 'static) -> Self {
223        let (sender, receiver) = futures_channel::mpsc::unbounded::<Message>();
224        Self {
225            scopes: FxHashMap::from_iter([(
226                ScopeId::ROOT,
227                Rc::new(RefCell::new(Scope {
228                    parent_node_id_in_parent: NodeId::ROOT,
229                    path_in_parent: Box::from([]),
230                    height: 0,
231                    parent_id: None,
232                    id: ScopeId::ROOT,
233                    key: DiffKey::Root,
234                    comp: Rc::new(move |_| root()),
235                    props: Rc::new(()),
236                    element: None,
237                    nodes: {
238                        let mut map = PathGraph::new();
239                        map.insert(
240                            &[],
241                            PathNode {
242                                node_id: NodeId::ROOT,
243                                scope_id: None,
244                            },
245                        );
246                        map
247                    },
248                })),
249            )]),
250            scopes_storages: Rc::new(RefCell::new(FxHashMap::from_iter([(
251                ScopeId::ROOT,
252                ScopeStorage::new(None, |writer| {
253                    ReactiveContext::new_for_scope(sender.clone(), ScopeId::ROOT, writer)
254                }),
255            )]))),
256
257            node_to_scope: FxHashMap::from_iter([(NodeId::ROOT, ScopeId::ROOT)]),
258
259            node_id_counter: NodeId::ROOT,
260            scope_id_counter: ScopeId::ROOT,
261            task_id_counter: Rc::default(),
262
263            dirty_tasks: VecDeque::default(),
264            dirty_scopes: FxHashSet::from_iter([ScopeId::ROOT]),
265
266            tasks: Rc::default(),
267
268            sender,
269            receiver,
270        }
271    }
272
273    #[cfg(all(debug_assertions, feature = "debug-integrity"))]
274    #[cfg_attr(feature = "hotpath", hotpath::measure)]
275    pub fn verify_scopes_integrity(&self) {
276        let mut visited = FxHashSet::default();
277        let size = self.scopes.len();
278        let mut buffer = vec![ScopeId::ROOT];
279        while let Some(scope_id) = buffer.pop() {
280            if visited.contains(&scope_id) {
281                continue;
282            }
283            visited.insert(scope_id);
284            let scope = self.scopes.get(&scope_id).unwrap();
285            let scope = scope.borrow();
286            if let Some(parent) = scope.parent_id {
287                buffer.push(parent);
288            }
289            scope.nodes.traverse(&[], |_, &PathNode { scope_id, .. }| {
290                if let Some(scope_id) = scope_id {
291                    buffer.push(scope_id);
292                }
293            });
294        }
295        assert_eq!(size, visited.len())
296    }
297
298    pub fn provide_root_context<T: 'static + Clone>(&mut self, context: impl FnOnce() -> T) -> T {
299        CurrentContext::run(
300            CurrentContext {
301                scope_id: ScopeId::ROOT,
302                scopes_storages: self.scopes_storages.clone(),
303                tasks: self.tasks.clone(),
304                task_id_counter: self.task_id_counter.clone(),
305                sender: self.sender.clone(),
306            },
307            move || {
308                let context = context();
309                let mut scopes_storages = self.scopes_storages.borrow_mut();
310                let root_scope_storage = scopes_storages.get_mut(&ScopeId::ROOT).unwrap();
311                root_scope_storage
312                    .contexts
313                    .insert(TypeId::of::<T>(), Rc::new(context.clone()));
314
315                context
316            },
317        )
318    }
319
320    pub fn handle_event(
321        &mut self,
322        node_id: impl Into<NodeId>,
323        event_name: EventName,
324        event_type: EventType,
325        bubbles: bool,
326    ) -> bool {
327        let node_id = node_id.into();
328        #[cfg(debug_assertions)]
329        tracing::info!("Handling event {event_name:?} for {node_id:?}");
330        let propagate = Rc::new(RefCell::new(bubbles));
331        let default = Rc::new(RefCell::new(true));
332
333        let Some(scope_id) = self.node_to_scope.get(&node_id) else {
334            return false;
335        };
336        let Some(path) = self
337            .scopes
338            .get(scope_id)
339            .unwrap()
340            .borrow()
341            .nodes
342            .find_path(|value| {
343                value
344                    == Some(&PathNode {
345                        node_id,
346                        scope_id: None,
347                    })
348            })
349        else {
350            return false;
351        };
352
353        let mut current_target = Some((path, *scope_id));
354        while let Some((path, scope_id)) = current_target.take() {
355            let scope = self.scopes.get(&scope_id).cloned().unwrap();
356            scope.borrow().with_element(&path, |element| {
357                match element {
358                    PathElement::Component { .. } => {
359                        unreachable!()
360                    }
361                    PathElement::Element { element, .. } => {
362                        CurrentContext::run(
363                            CurrentContext {
364                                scope_id,
365                                scopes_storages: self.scopes_storages.clone(),
366                                tasks: self.tasks.clone(),
367                                task_id_counter: self.task_id_counter.clone(),
368                                sender: self.sender.clone(),
369                            },
370                            || {
371                                match &event_type {
372                                    EventType::Mouse(data) => {
373                                        let event_handlers = element.events_handlers();
374                                        if let Some(event_handlers) = event_handlers {
375                                            match event_handlers.get(&event_name) {
376                                                Some(EventHandlerType::Mouse(handler)) => {
377                                                    handler.call(Event {
378                                                        data: data.clone(),
379                                                        propagate: propagate.clone(),
380                                                        default: default.clone(),
381                                                    });
382                                                }
383                                                Some(_) => unreachable!(),
384                                                _ => {}
385                                            }
386                                        }
387                                    }
388                                    EventType::Keyboard(data) => {
389                                        let event_handlers = element.events_handlers();
390                                        if let Some(event_handlers) = event_handlers {
391                                            match event_handlers.get(&event_name) {
392                                                Some(EventHandlerType::Keyboard(handler)) => {
393                                                    handler.call(Event {
394                                                        data: data.clone(),
395                                                        propagate: propagate.clone(),
396                                                        default: default.clone(),
397                                                    });
398                                                }
399                                                Some(_) => unreachable!(),
400                                                _ => {}
401                                            }
402                                        }
403                                    }
404                                    EventType::Sized(data) => {
405                                        let event_handlers = element.events_handlers();
406                                        if let Some(event_handlers) = event_handlers {
407                                            match event_handlers.get(&event_name) {
408                                                Some(EventHandlerType::Sized(handler)) => {
409                                                    handler.call(Event {
410                                                        data: data.clone(),
411                                                        propagate: propagate.clone(),
412                                                        default: default.clone(),
413                                                    });
414                                                }
415                                                Some(_) => unreachable!(),
416                                                _ => {}
417                                            }
418                                        }
419                                    }
420                                    EventType::Wheel(data) => {
421                                        let event_handlers = element.events_handlers();
422                                        if let Some(event_handlers) = event_handlers {
423                                            match event_handlers.get(&event_name) {
424                                                Some(EventHandlerType::Wheel(handler)) => {
425                                                    handler.call(Event {
426                                                        data: data.clone(),
427                                                        propagate: propagate.clone(),
428                                                        default: default.clone(),
429                                                    });
430                                                }
431                                                Some(_) => unreachable!(),
432                                                _ => {}
433                                            }
434                                        }
435                                    }
436                                    EventType::Touch(data) => {
437                                        let event_handlers = element.events_handlers();
438                                        if let Some(event_handlers) = event_handlers {
439                                            match event_handlers.get(&event_name) {
440                                                Some(EventHandlerType::Touch(handler)) => {
441                                                    handler.call(Event {
442                                                        data: data.clone(),
443                                                        propagate: propagate.clone(),
444                                                        default: default.clone(),
445                                                    });
446                                                }
447                                                Some(_) => unreachable!(),
448                                                _ => {}
449                                            }
450                                        }
451                                    }
452                                    EventType::Pointer(data) => {
453                                        let event_handlers = element.events_handlers();
454                                        if let Some(event_handlers) = event_handlers {
455                                            match event_handlers.get(&event_name) {
456                                                Some(EventHandlerType::Pointer(handler)) => {
457                                                    handler.call(Event {
458                                                        data: data.clone(),
459                                                        propagate: propagate.clone(),
460                                                        default: default.clone(),
461                                                    });
462                                                }
463                                                Some(_) => unreachable!(),
464                                                _ => {}
465                                            }
466                                        }
467                                    }
468                                    EventType::File(data) => {
469                                        let event_handlers = element.events_handlers();
470                                        if let Some(event_handlers) = event_handlers {
471                                            match event_handlers.get(&event_name) {
472                                                Some(EventHandlerType::File(handler)) => {
473                                                    handler.call(Event {
474                                                        data: data.clone(),
475                                                        propagate: propagate.clone(),
476                                                        default: default.clone(),
477                                                    });
478                                                }
479                                                Some(_) => unreachable!(),
480                                                _ => {}
481                                            }
482                                        }
483                                    }
484                                    EventType::ImePreedit(data) => {
485                                        let event_handlers = element.events_handlers();
486                                        if let Some(event_handlers) = event_handlers {
487                                            match event_handlers.get(&event_name) {
488                                                Some(EventHandlerType::ImePreedit(handler)) => {
489                                                    handler.call(Event {
490                                                        data: data.clone(),
491                                                        propagate: propagate.clone(),
492                                                        default: default.clone(),
493                                                    });
494                                                }
495                                                Some(_) => unreachable!(),
496                                                _ => {}
497                                            }
498                                        }
499                                    }
500                                }
501
502                                // Bubble up if desired
503                                if *propagate.borrow() {
504                                    if path.len() > 1 {
505                                        // Change the target to this element parent (still in the same Scope)
506                                        current_target
507                                            .replace((path[..path.len() - 1].to_vec(), scope_id));
508                                    } else {
509                                        let mut parent_scope_id = scope.borrow().parent_id;
510                                        // Otherwise change the target to this element parent in the parent Scope
511                                        loop {
512                                            if let Some(parent_id) = parent_scope_id.take() {
513                                                let parent_scope =
514                                                    self.scopes.get(&parent_id).unwrap();
515                                                let path = parent_scope.borrow().nodes.find_path(
516                                                    |value| {
517                                                        value
518                                                            == Some(&PathNode {
519                                                                node_id: scope
520                                                                    .borrow()
521                                                                    .parent_node_id_in_parent,
522                                                                scope_id: None,
523                                                            })
524                                                    },
525                                                );
526                                                if let Some(path) = path {
527                                                    current_target.replace((path, parent_id));
528                                                    break;
529                                                } else {
530                                                    parent_scope_id =
531                                                        parent_scope.borrow().parent_id;
532                                                }
533                                            } else {
534                                                return;
535                                            }
536                                        }
537                                    }
538                                }
539                            },
540                        )
541                    }
542                }
543            });
544        }
545        *default.borrow()
546    }
547
548    #[cfg_attr(feature = "hotpath", hotpath::measure)]
549    pub async fn handle_events(&mut self) {
550        loop {
551            while let Ok(msg) = self.receiver.try_recv() {
552                match msg {
553                    Message::MarkScopeAsDirty(scope_id) => {
554                        self.dirty_scopes.insert(scope_id);
555                    }
556                    Message::PollTask(task_id) => {
557                        self.dirty_tasks.push_back(task_id);
558                    }
559                }
560            }
561
562            if !self.dirty_scopes.is_empty() {
563                return;
564            }
565
566            while let Some(task_id) = self.dirty_tasks.pop_front() {
567                let Some(task) = self.tasks.borrow().get(&task_id).cloned() else {
568                    continue;
569                };
570                let mut task = task.borrow_mut();
571                let waker = task.waker.clone();
572
573                let mut cx = std::task::Context::from_waker(&waker);
574
575                CurrentContext::run(
576                    {
577                        let Some(scope) = self.scopes.get(&task.scope_id) else {
578                            continue;
579                        };
580                        CurrentContext {
581                            scope_id: scope.borrow().id,
582                            scopes_storages: self.scopes_storages.clone(),
583                            tasks: self.tasks.clone(),
584                            task_id_counter: self.task_id_counter.clone(),
585                            sender: self.sender.clone(),
586                        }
587                    },
588                    || {
589                        let poll_result = task.future.poll(&mut cx);
590                        if poll_result.is_ready() {
591                            let _ = self.tasks.borrow_mut().remove(&task_id);
592                        }
593                    },
594                );
595            }
596
597            if !self.dirty_scopes.is_empty() {
598                return;
599            }
600
601            while let Some(msg) = self.receiver.next().await {
602                match msg {
603                    Message::MarkScopeAsDirty(scope_id) => {
604                        self.dirty_scopes.insert(scope_id);
605                    }
606                    Message::PollTask(task_id) => {
607                        self.dirty_tasks.push_back(task_id);
608                    }
609                }
610            }
611        }
612    }
613
614    /// Useful for freya-testing
615    #[cfg_attr(feature = "hotpath", hotpath::measure)]
616    pub fn handle_events_immediately(&mut self) {
617        while let Ok(msg) = self.receiver.try_recv() {
618            match msg {
619                Message::MarkScopeAsDirty(scope_id) => {
620                    self.dirty_scopes.insert(scope_id);
621                }
622                Message::PollTask(task_id) => {
623                    self.dirty_tasks.push_back(task_id);
624                }
625            }
626        }
627
628        if !self.dirty_scopes.is_empty() {
629            return;
630        }
631
632        // Poll here
633        while let Some(task_id) = self.dirty_tasks.pop_front() {
634            let Some(task) = self.tasks.borrow().get(&task_id).cloned() else {
635                continue;
636            };
637            let mut task = task.borrow_mut();
638            let waker = task.waker.clone();
639
640            let mut cx = std::task::Context::from_waker(&waker);
641
642            CurrentContext::run(
643                {
644                    let Some(scope) = self.scopes.get(&task.scope_id) else {
645                        continue;
646                    };
647                    CurrentContext {
648                        scope_id: scope.borrow().id,
649                        scopes_storages: self.scopes_storages.clone(),
650                        tasks: self.tasks.clone(),
651                        task_id_counter: self.task_id_counter.clone(),
652                        sender: self.sender.clone(),
653                    }
654                },
655                || {
656                    let poll_result = task.future.poll(&mut cx);
657                    if poll_result.is_ready() {
658                        let _ = self.tasks.borrow_mut().remove(&task_id);
659                    }
660                },
661            );
662        }
663    }
664
665    #[cfg_attr(feature = "hotpath", hotpath::measure)]
666    pub fn sync_and_update(&mut self) -> Mutations {
667        self.handle_events_immediately();
668        use itertools::Itertools;
669
670        #[cfg(all(debug_assertions, feature = "debug-integrity"))]
671        self.verify_scopes_integrity();
672
673        let mut mutations = Mutations::default();
674
675        let dirty_scopes = self
676            .dirty_scopes
677            .drain()
678            .filter_map(|id| self.scopes.get(&id).cloned())
679            .sorted_by_key(|s| s.borrow().height)
680            .map(|s| s.borrow().id)
681            .collect::<Box<[_]>>();
682
683        let mut visited_scopes = FxHashSet::default();
684
685        for scope_id in dirty_scopes {
686            // No need to run scopes more than once
687            if visited_scopes.contains(&scope_id) {
688                continue;
689            }
690
691            let Some(scope_rc) = self.scopes.get(&scope_id).cloned() else {
692                continue;
693            };
694
695            let scope_id = scope_rc.borrow().id;
696
697            let element = CurrentContext::run_with_reactive(
698                CurrentContext {
699                    scope_id,
700                    scopes_storages: self.scopes_storages.clone(),
701                    tasks: self.tasks.clone(),
702                    task_id_counter: self.task_id_counter.clone(),
703                    sender: self.sender.clone(),
704                },
705                || {
706                    let scope = scope_rc.borrow();
707                    (scope.comp)(scope.props.clone())
708                },
709            );
710
711            let path_element = PathElement::from_element(vec![0], element);
712            let mut diff = Diff::default();
713            path_element.diff(scope_rc.borrow().element.as_ref(), &mut diff);
714
715            self.apply_diff(&scope_rc, diff, &mut mutations, &path_element);
716
717            self.run_scope(
718                &scope_rc,
719                &path_element,
720                &mut mutations,
721                &mut visited_scopes,
722            );
723
724            let mut scopes_storages = self.scopes_storages.borrow_mut();
725            let scope_storage = scopes_storages.get_mut(&scope_rc.borrow().id).unwrap();
726            scope_storage.current_value = 0;
727            scope_storage.current_run += 1;
728
729            scope_rc.borrow_mut().element = Some(path_element);
730        }
731
732        mutations
733    }
734
735    pub fn run_in<T>(&self, run: impl FnOnce() -> T) -> T {
736        CurrentContext::run(
737            CurrentContext {
738                scope_id: ScopeId::ROOT,
739                scopes_storages: self.scopes_storages.clone(),
740                tasks: self.tasks.clone(),
741                task_id_counter: self.task_id_counter.clone(),
742                sender: self.sender.clone(),
743            },
744            run,
745        )
746    }
747
748    #[cfg_attr(feature = "hotpath", hotpath::measure)]
749    fn run_scope(
750        &mut self,
751        scope: &Rc<RefCell<Scope>>,
752        element: &PathElement,
753        mutations: &mut Mutations,
754        visited_scopes: &mut FxHashSet<ScopeId>,
755    ) {
756        visited_scopes.insert(scope.borrow().id);
757        match element {
758            PathElement::Component {
759                comp,
760                props,
761                key,
762                path,
763            } => {
764                // Safe to unwrap because this is a component
765                let assigned_scope_id = scope
766                    .borrow()
767                    .nodes
768                    .get(path)
769                    .and_then(|path_node| path_node.scope_id)
770                    .unwrap();
771
772                let parent_node_id = if path.as_ref() == [0] {
773                    scope.borrow().parent_node_id_in_parent
774                } else {
775                    scope
776                        .borrow()
777                        .nodes
778                        .get(&path[..path.len() - 1])
779                        .unwrap()
780                        .node_id
781                };
782
783                if let Some(Ok(mut existing_scope)) = self
784                    .scopes
785                    .get(&assigned_scope_id)
786                    .map(|s| s.try_borrow_mut())
787                {
788                    let key_changed = existing_scope.key != *key;
789                    if key_changed || existing_scope.props.changed(props.as_ref()) {
790                        self.dirty_scopes.insert(assigned_scope_id);
791                        existing_scope.props = props.clone();
792
793                        if key_changed {
794                            self.scopes_storages
795                                .borrow_mut()
796                                .get_mut(&assigned_scope_id)
797                                .unwrap()
798                                .reset();
799                        }
800                    }
801                } else {
802                    self.scopes.insert(
803                        assigned_scope_id,
804                        Rc::new(RefCell::new(Scope {
805                            parent_node_id_in_parent: parent_node_id,
806                            path_in_parent: path.clone(),
807                            height: scope.borrow().height + 1,
808                            parent_id: Some(scope.borrow().id),
809                            id: assigned_scope_id,
810                            key: key.clone(),
811                            comp: comp.clone(),
812                            props: props.clone(),
813                            element: None,
814                            nodes: PathGraph::default(),
815                        })),
816                    );
817                    self.scopes_storages.borrow_mut().insert(
818                        assigned_scope_id,
819                        ScopeStorage::new(Some(scope.borrow().id), |writer| {
820                            ReactiveContext::new_for_scope(
821                                self.sender.clone(),
822                                assigned_scope_id,
823                                writer,
824                            )
825                        }),
826                    );
827                    self.dirty_scopes.insert(assigned_scope_id);
828                }
829
830                let was_dirty = self.dirty_scopes.remove(&assigned_scope_id);
831
832                if !was_dirty {
833                    // No need to rerun scope if it is not dirty
834                    return;
835                }
836
837                let scope_rc = self.scopes.get(&assigned_scope_id).cloned().unwrap();
838
839                let element = hotpath::measure_block!("Scope Rendering", {
840                    CurrentContext::run_with_reactive(
841                        CurrentContext {
842                            scope_id: assigned_scope_id,
843                            scopes_storages: self.scopes_storages.clone(),
844                            tasks: self.tasks.clone(),
845                            task_id_counter: self.task_id_counter.clone(),
846                            sender: self.sender.clone(),
847                        },
848                        || {
849                            let scope = scope_rc.borrow();
850                            (scope.comp)(scope.props.clone())
851                        },
852                    )
853                });
854
855                let path_element = PathElement::from_element(vec![0], element);
856                let mut diff = Diff::default();
857                path_element.diff(scope_rc.borrow().element.as_ref(), &mut diff);
858
859                self.apply_diff(&scope_rc, diff, mutations, &path_element);
860
861                self.run_scope(&scope_rc, &path_element, mutations, visited_scopes);
862                let mut scopes_storages = self.scopes_storages.borrow_mut();
863                let scope_storage = scopes_storages.get_mut(&assigned_scope_id).unwrap();
864                scope_storage.current_value = 0;
865                scope_storage.current_run += 1;
866
867                scope_rc.borrow_mut().element = Some(path_element);
868            }
869            PathElement::Element { elements, .. } => {
870                for element in elements.iter() {
871                    self.run_scope(scope, element, mutations, visited_scopes);
872                }
873            }
874        }
875    }
876
877    /// Recursively traverse up in the scopes tree until a suitable (non-root) slot is found to put an element.
878    /// Returns a parent node id and a slot index pointing to one of its children.
879    fn find_scope_root_parent_info(
880        &self,
881        parent_id: Option<ScopeId>,
882        parent_node_id: NodeId,
883        scope_id: ScopeId,
884    ) -> (NodeId, u32) {
885        let mut index_inside_parent = 0;
886
887        if let Some(parent_id) = parent_id {
888            let mut buffer = Some((parent_id, parent_node_id, scope_id));
889            while let Some((parent_id, parent_node_id, scope_id)) = buffer.take() {
890                let parent_scope = self.scopes.get(&parent_id).unwrap();
891                let parent_scope = parent_scope.borrow();
892
893                let scope = self.scopes.get(&scope_id).unwrap();
894                let scope = scope.borrow();
895
896                let path_node_parent = parent_scope.nodes.find(|v| {
897                    if let Some(v) = v {
898                        v.node_id == parent_node_id
899                    } else {
900                        false
901                    }
902                });
903
904                if let Some(path_node_parent) = path_node_parent {
905                    if let Some(scope_id) = path_node_parent.scope_id {
906                        if let Some(parent_id) = parent_scope.parent_id {
907                            // The found element turns out to be a component so go to it to continue looking
908                            buffer.replace((
909                                parent_id,
910                                parent_scope.parent_node_id_in_parent,
911                                scope_id,
912                            ));
913                        } else {
914                            assert_eq!(scope_id, ScopeId::ROOT);
915                        }
916                    } else {
917                        // Found an Element parent so we get the index from the path
918                        index_inside_parent = *scope.path_in_parent.last().unwrap();
919                        return (parent_node_id, index_inside_parent);
920                    }
921                } else if let Some(new_parent_id) = parent_scope.parent_id {
922                    // If no element was found we go to the parent scope
923                    buffer.replace((
924                        new_parent_id,
925                        parent_scope.parent_node_id_in_parent,
926                        parent_id,
927                    ));
928                }
929            }
930        } else {
931            assert_eq!(scope_id, ScopeId::ROOT);
932        }
933
934        (parent_node_id, index_inside_parent)
935    }
936
937    /// Recursively finds the root element [NodeId] of a scope.
938    /// When a scope's first child is another component (scope), this follows
939    /// the chain until it finds the first actual element.
940    fn find_scope_root_node_id(&self, scope_id: ScopeId) -> NodeId {
941        let scope_rc = self.scopes.get(&scope_id).unwrap();
942        let scope = scope_rc.borrow();
943        let path_node = scope.nodes.get(&[0]).unwrap();
944        if let Some(child_scope_id) = path_node.scope_id {
945            self.find_scope_root_node_id(child_scope_id)
946        } else {
947            path_node.node_id
948        }
949    }
950
951    #[cfg_attr(feature = "hotpath", hotpath::measure)]
952    fn apply_diff(
953        &mut self,
954        scope: &Rc<RefCell<Scope>>,
955        diff: Diff,
956        mutations: &mut Mutations,
957        path_element: &PathElement,
958    ) {
959        let mut moved_nodes =
960            FxHashMap::<Box<[u32]>, (NodeId, FxHashMap<u32, PathNode>)>::default();
961        let mut parents_to_resync_scopes = FxHashSet::default();
962
963        // Store the moved nodes so that they can
964        // later be rearranged once the removals and additions have been done
965        for (parent, movements) in &diff.moved {
966            parents_to_resync_scopes.insert(parent.clone());
967            // `parent` is a new-tree path; if the parent itself was moved, its path in the
968            // old nodes tree will differ — resolve it before any lookup.
969            let old_parent = resolve_old_path(parent, &diff.moved);
970            let paths = moved_nodes.entry(parent.clone()).or_insert_with(|| {
971                let parent_node_id = scope.borrow().nodes.get(&old_parent).unwrap().node_id;
972                (parent_node_id, FxHashMap::default())
973            });
974
975            for (from, _to) in movements.iter() {
976                let mut old_child_path = old_parent.clone();
977                old_child_path.push(*from);
978
979                let path_node = scope.borrow().nodes.get(&old_child_path).cloned().unwrap();
980
981                paths.1.insert(*from, path_node);
982            }
983        }
984
985        // Collect a set of branches to remove in cascade
986        let mut selected_roots: HashMap<&[u32], HashSet<&[u32]>> = HashMap::default();
987        let mut scope_removal_buffer = vec![];
988
989        // Given some removals like:
990        // [
991        //     [0,2],
992        //     [0,1,0,1],
993        //     [0,1,0,2],
994        //     [0,3],
995        //     [0,1,5,8],
996        // ]
997        //
998        // We want them ordered like:
999        // [
1000        //     [0,3],
1001        //     [0,2],
1002        //     [0,1,5,8],
1003        //     [0,1,0,2],
1004        //     [0,1,0,1],
1005        // ]
1006        //
1007        // This way any removal does not move the next removals
1008        'remove: for removed in diff.removed.iter().sorted_by(|a, b| {
1009            for (x, y) in a.iter().zip(b.iter()) {
1010                match x.cmp(y) {
1011                    Ordering::Equal => continue,
1012                    non_eq => return non_eq.reverse(),
1013                }
1014            }
1015            b.len().cmp(&a.len())
1016        }) {
1017            parents_to_resync_scopes.insert(Box::from(&removed[..removed.len() - 1]));
1018
1019            let path_node = scope.borrow().nodes.get(removed).cloned();
1020            if let Some(PathNode { node_id, scope_id }) = path_node {
1021                if scope_id.is_none() {
1022                    let index_inside_parent = if removed.as_ref() == [0] {
1023                        let parent_id = scope.borrow().parent_id;
1024                        let scope_id = scope.borrow().id;
1025                        let parent_node_id = scope.borrow().parent_node_id_in_parent;
1026                        self.find_scope_root_parent_info(parent_id, parent_node_id, scope_id)
1027                            .1
1028                    } else {
1029                        // Only do it for non-scope-roots because the root is is always in the same position therefore it doesnt make sense to resync from its parent
1030                        removed[removed.len() - 1]
1031                    };
1032
1033                    // plain element removal
1034                    mutations.removed.push(MutationRemove::Element {
1035                        id: node_id,
1036                        index: index_inside_parent,
1037                    });
1038                }
1039
1040                // Skip if this removed path is already covered by a previously selected root
1041                for (root, inner) in &mut selected_roots {
1042                    if is_descendant(removed, root) {
1043                        inner.insert(removed);
1044                        continue 'remove;
1045                    }
1046                }
1047
1048                // Remove any previously selected roots that are descendants of this new (higher) removed path
1049                selected_roots.retain(|root, _| !is_descendant(root, removed));
1050
1051                selected_roots
1052                    .entry(&removed[..removed.len() - 1])
1053                    .or_default()
1054                    .insert(removed);
1055            } else {
1056                unreachable!()
1057            }
1058        }
1059
1060        // Traverse each chosen branch root and queue nested scopes
1061        for (root, removed) in selected_roots.iter().sorted_by(|(a, _), (b, _)| {
1062            for (x, y) in a.iter().zip(b.iter()) {
1063                match x.cmp(y) {
1064                    Ordering::Equal => continue,
1065                    non_eq => return non_eq.reverse(),
1066                }
1067            }
1068            b.len().cmp(&a.len())
1069        }) {
1070            scope.borrow_mut().nodes.retain(
1071                root,
1072                |p, _| !removed.contains(p),
1073                |_, &PathNode { scope_id, node_id }| {
1074                    if let Some(scope_id) = scope_id {
1075                        // Queue scope to be removed
1076                        scope_removal_buffer.push(self.scopes.get(&scope_id).cloned().unwrap());
1077                    } else {
1078                        self.node_to_scope.remove(&node_id).unwrap();
1079                    }
1080                },
1081            );
1082        }
1083
1084        let mut scope_removal_queue = VecDeque::new();
1085
1086        while let Some(scope_rc) = scope_removal_buffer.pop() {
1087            // Push the scopes to a queue that will remove
1088            // them starting from the deepest to the highest ones
1089            scope_removal_queue.push_front(scope_rc.clone());
1090
1091            let scope = scope_rc.borrow_mut();
1092
1093            let mut scope_root_node_id = None;
1094
1095            // Queue nested scopes to be removed
1096            scope
1097                .nodes
1098                .traverse(&[], |path, &PathNode { scope_id, node_id }| {
1099                    if let Some(scope_id) = scope_id {
1100                        scope_removal_buffer.push(self.scopes.get(&scope_id).cloned().unwrap());
1101                    } else {
1102                        self.node_to_scope.remove(&node_id).unwrap();
1103                    }
1104                    if path == [0] {
1105                        scope_root_node_id = Some(node_id);
1106                    }
1107                });
1108
1109            // Nodes that have a scope id are components, so no need to mark those as removed in the tree
1110            // Instead we get their root node id and remove it
1111            mutations.removed.push(MutationRemove::Scope {
1112                id: scope_root_node_id.unwrap(),
1113            });
1114        }
1115
1116        // Finally drops the scopes and their storage
1117        for scope_rc in scope_removal_queue {
1118            let scope = scope_rc.borrow_mut();
1119
1120            self.scopes.remove(&scope.id);
1121
1122            // Dropped hooks might e.g spawn forever tasks, so they need access to the context
1123            CurrentContext::run_with_reactive(
1124                CurrentContext {
1125                    scope_id: scope.id,
1126                    scopes_storages: self.scopes_storages.clone(),
1127                    tasks: self.tasks.clone(),
1128                    task_id_counter: self.task_id_counter.clone(),
1129                    sender: self.sender.clone(),
1130                },
1131                || {
1132                    // TODO: Scopes could also maintain its own registry of assigned tasks
1133                    let mut _removed_tasks = Vec::new();
1134
1135                    self.tasks.borrow_mut().retain(|task_id, task| {
1136                        if task.borrow().scope_id == scope.id {
1137                            _removed_tasks.push((*task_id, task.clone()));
1138                            false
1139                        } else {
1140                            true
1141                        }
1142                    });
1143                    drop(_removed_tasks);
1144                    // This is very important, the scope storage must be dropped after the borrow in `scopes_storages` has been released
1145                    let _scope = self.scopes_storages.borrow_mut().remove(&scope.id);
1146                },
1147            );
1148        }
1149
1150        // Given some additions like:
1151        // [
1152        //     [0,2],
1153        //     [0,1,0,1],
1154        //     [0,1,0,2],
1155        //     [0,3],
1156        //     [0,1,5,8],
1157        // ]
1158        //
1159        // We want them ordered like:
1160        // [
1161        //     [0,1,0,1],
1162        //     [0,1,0,2],
1163        //     [0,1,5,8],
1164        //     [0,2],
1165        //     [0,3],
1166        // ]
1167        //
1168        // This way, no addition offsets the next additions in line.
1169        for added in diff
1170            .added
1171            .iter()
1172            .sorted_by(|a, b| {
1173                for (x, y) in a.iter().zip(b.iter()) {
1174                    match x.cmp(y) {
1175                        Ordering::Equal => continue,
1176                        non_eq => return non_eq.reverse(),
1177                    }
1178                }
1179                b.len().cmp(&a.len())
1180            })
1181            .rev()
1182        {
1183            let (parent_node_id, index_inside_parent) = if added.as_ref() == [0] {
1184                let parent_id = scope.borrow().parent_id;
1185                let scope_id = scope.borrow().id;
1186                let parent_node_id = scope.borrow().parent_node_id_in_parent;
1187                self.find_scope_root_parent_info(parent_id, parent_node_id, scope_id)
1188            } else {
1189                // Only do it for non-scope-roots because the root is is always in the same position therefore it doesnt make sense to resync from its parent
1190                parents_to_resync_scopes.insert(Box::from(&added[..added.len() - 1]));
1191                (
1192                    scope
1193                        .borrow()
1194                        .nodes
1195                        .get(&added[..added.len() - 1])
1196                        .unwrap()
1197                        .node_id,
1198                    added[added.len() - 1],
1199                )
1200            };
1201
1202            self.node_id_counter += 1;
1203
1204            path_element.with_element(added, |element| match element {
1205                PathElement::Component { .. } => {
1206                    self.scope_id_counter += 1;
1207                    let scope_id = self.scope_id_counter;
1208
1209                    scope.borrow_mut().nodes.insert(
1210                        added,
1211                        PathNode {
1212                            node_id: self.node_id_counter,
1213                            scope_id: Some(scope_id),
1214                        },
1215                    );
1216                }
1217                PathElement::Element { element, .. } => {
1218                    mutations.added.push(MutationAdd {
1219                        node_id: self.node_id_counter,
1220                        parent_id: parent_node_id,
1221                        index: index_inside_parent,
1222                        element: element.clone(),
1223                    });
1224
1225                    self.node_to_scope
1226                        .insert(self.node_id_counter, scope.borrow().id);
1227                    scope.borrow_mut().nodes.insert(
1228                        added,
1229                        PathNode {
1230                            node_id: self.node_id_counter,
1231                            scope_id: None,
1232                        },
1233                    );
1234                }
1235            });
1236        }
1237
1238        for (parent, movements) in diff.moved.into_iter().sorted_by(|(a, _), (b, _)| {
1239            for (x, y) in a.iter().zip(b.iter()) {
1240                match x.cmp(y) {
1241                    Ordering::Equal => continue,
1242                    non_eq => return non_eq.reverse(),
1243                }
1244            }
1245            a.len().cmp(&b.len())
1246        }) {
1247            parents_to_resync_scopes.insert(parent.clone());
1248
1249            let (parent_node_id, paths) = moved_nodes.get_mut(&parent).unwrap();
1250
1251            for (from, to) in movements.into_iter().sorted_by_key(|e| e.0).rev() {
1252                let path_node = paths.remove(&from).unwrap();
1253
1254                let PathNode { node_id, scope_id } = path_node;
1255
1256                // Search for this moved node current position
1257                let from_path = scope
1258                    .borrow()
1259                    .nodes
1260                    .find_child_path(&parent, |v| v == Some(&path_node))
1261                    .unwrap();
1262
1263                let mut to_path = parent.to_vec();
1264                to_path.push(to);
1265
1266                if from_path == to_path {
1267                    continue;
1268                }
1269
1270                // Remove the node from the old position and add it to the new one
1271                let path_entry = scope.borrow_mut().nodes.remove(&from_path).unwrap();
1272                scope.borrow_mut().nodes.insert_entry(&to_path, path_entry);
1273
1274                if let Some(scope_id) = scope_id {
1275                    let scope_root_node_id = self.find_scope_root_node_id(scope_id);
1276                    let scope_rc = self.scopes.get(&scope_id).cloned().unwrap();
1277                    let scope = scope_rc.borrow();
1278
1279                    // Mark the scope root node id as moved
1280                    mutations
1281                        .moved
1282                        .entry(scope.parent_node_id_in_parent)
1283                        .or_default()
1284                        .push(MutationMove {
1285                            index: to,
1286                            node_id: scope_root_node_id,
1287                        });
1288                } else {
1289                    // Mark the element as moved
1290                    mutations
1291                        .moved
1292                        .entry(*parent_node_id)
1293                        .or_default()
1294                        .push(MutationMove { index: to, node_id });
1295                }
1296            }
1297        }
1298
1299        for (modified, flags) in diff.modified {
1300            path_element.with_element(&modified, |element| match element {
1301                PathElement::Component { .. } => {
1302                    // Components never change when being diffed
1303                    unreachable!()
1304                }
1305                PathElement::Element { element, .. } => {
1306                    let node_id = scope
1307                        .borrow()
1308                        .nodes
1309                        .get(&modified)
1310                        .map(|path_node| path_node.node_id)
1311                        .unwrap();
1312                    mutations.modified.push(MutationModified {
1313                        node_id,
1314                        element: element.clone(),
1315                        flags,
1316                    });
1317                }
1318            });
1319        }
1320
1321        // When a parent gets a new child, or a child is removed or moved we
1322        // resync its 1 level children scopes with their new path
1323        for parent in parents_to_resync_scopes {
1324            // But only if the parent already existed before otherwise its pointless
1325            // as Scopes will be created with the latest path already
1326            if diff.added.contains(&parent) {
1327                // TODO: Maybe do this check before inserting
1328                continue;
1329            }
1330
1331            // Update all the nested scopes in this Scope with their up to date paths
1332            scope
1333                .borrow_mut()
1334                .nodes
1335                .traverse_1_level(&parent, |p, path_node| {
1336                    if let Some(scope_id) = path_node.scope_id
1337                        && let Some(scope_rc) = self.scopes.get(&scope_id)
1338                    {
1339                        let mut scope = scope_rc.borrow_mut();
1340                        scope.path_in_parent = Box::from(p);
1341                    }
1342                });
1343        }
1344    }
1345}
1346
1347#[derive(Default, Debug)]
1348pub struct Diff {
1349    pub added: Vec<Box<[u32]>>,
1350
1351    pub modified: Vec<(Box<[u32]>, DiffModifies)>,
1352
1353    pub removed: Vec<Box<[u32]>>,
1354
1355    pub moved: HashMap<Box<[u32]>, Vec<(u32, u32)>>,
1356}
1357
1358/// Converts a new-tree path to its corresponding old-tree path by checking, for each
1359/// segment, whether that position was the destination of a movement in `moved`. If so,
1360/// the original (`from`) index is substituted so the result can be used to look up nodes
1361/// in the pre-diff nodes tree.
1362fn resolve_old_path(new_path: &[u32], moved: &HashMap<Box<[u32]>, Vec<(u32, u32)>>) -> Vec<u32> {
1363    let mut old_path = Vec::with_capacity(new_path.len());
1364    for i in 0..new_path.len() {
1365        let new_parent = &new_path[..i];
1366        let new_index = new_path[i];
1367        if let Some(movements) = moved.get(new_parent)
1368            && let Some(&(from, _)) = movements.iter().find(|(_, to)| *to == new_index)
1369        {
1370            old_path.push(from);
1371            continue;
1372        }
1373        old_path.push(new_index);
1374    }
1375    old_path
1376}
1377
1378fn is_descendant(candidate: &[u32], ancestor: &[u32]) -> bool {
1379    if ancestor.len() > candidate.len() {
1380        return false;
1381    }
1382    candidate[..ancestor.len()] == *ancestor
1383}