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 Element { id: NodeId, index: u32 },
61 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 (Element { index: a, .. }, Element { index: b, .. }) => b.cmp(a),
77 (Element { .. }, Scope { .. }) => Ordering::Less,
79 (Scope { .. }, Element { .. }) => Ordering::Greater,
80 (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 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 if *propagate.borrow() {
504 if path.len() > 1 {
505 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 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 #[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 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 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 #[cfg(feature = "hotreload")]
708 {
709 subsecond::call(|| (scope.comp)(scope.props.clone()))
710 }
711 #[cfg(not(feature = "hotreload"))]
712 {
713 (scope.comp)(scope.props.clone())
714 }
715 },
716 );
717
718 let path_element = PathElement::from_element(vec![0], element);
719 let mut diff = Diff::default();
720 path_element.diff(scope_rc.borrow().element.as_ref(), &mut diff);
721
722 self.apply_diff(&scope_rc, diff, &mut mutations, &path_element);
723
724 self.run_scope(
725 &scope_rc,
726 &path_element,
727 &mut mutations,
728 &mut visited_scopes,
729 );
730
731 let mut scopes_storages = self.scopes_storages.borrow_mut();
732 let scope_storage = scopes_storages.get_mut(&scope_rc.borrow().id).unwrap();
733 scope_storage.current_value = 0;
734 scope_storage.current_run += 1;
735
736 scope_rc.borrow_mut().element = Some(path_element);
737 }
738
739 mutations
740 }
741
742 pub fn run_in<T>(&self, run: impl FnOnce() -> T) -> T {
743 CurrentContext::run(
744 CurrentContext {
745 scope_id: ScopeId::ROOT,
746 scopes_storages: self.scopes_storages.clone(),
747 tasks: self.tasks.clone(),
748 task_id_counter: self.task_id_counter.clone(),
749 sender: self.sender.clone(),
750 },
751 run,
752 )
753 }
754
755 #[cfg_attr(feature = "hotpath", hotpath::measure)]
756 fn run_scope(
757 &mut self,
758 scope: &Rc<RefCell<Scope>>,
759 element: &PathElement,
760 mutations: &mut Mutations,
761 visited_scopes: &mut FxHashSet<ScopeId>,
762 ) {
763 visited_scopes.insert(scope.borrow().id);
764 match element {
765 PathElement::Component {
766 comp,
767 props,
768 key,
769 path,
770 } => {
771 let assigned_scope_id = scope
773 .borrow()
774 .nodes
775 .get(path)
776 .and_then(|path_node| path_node.scope_id)
777 .unwrap();
778
779 let parent_node_id = if path.as_ref() == [0] {
780 scope.borrow().parent_node_id_in_parent
781 } else {
782 scope
783 .borrow()
784 .nodes
785 .get(&path[..path.len() - 1])
786 .unwrap()
787 .node_id
788 };
789
790 if let Some(Ok(mut existing_scope)) = self
791 .scopes
792 .get(&assigned_scope_id)
793 .map(|s| s.try_borrow_mut())
794 {
795 let key_changed = existing_scope.key != *key;
796 if key_changed || existing_scope.props.changed(props.as_ref()) {
797 self.dirty_scopes.insert(assigned_scope_id);
798 existing_scope.props = props.clone();
799
800 if key_changed {
801 self.scopes_storages
802 .borrow_mut()
803 .get_mut(&assigned_scope_id)
804 .unwrap()
805 .reset();
806 }
807 }
808 } else {
809 self.scopes.insert(
810 assigned_scope_id,
811 Rc::new(RefCell::new(Scope {
812 parent_node_id_in_parent: parent_node_id,
813 path_in_parent: path.clone(),
814 height: scope.borrow().height + 1,
815 parent_id: Some(scope.borrow().id),
816 id: assigned_scope_id,
817 key: key.clone(),
818 comp: comp.clone(),
819 props: props.clone(),
820 element: None,
821 nodes: PathGraph::default(),
822 })),
823 );
824 self.scopes_storages.borrow_mut().insert(
825 assigned_scope_id,
826 ScopeStorage::new(Some(scope.borrow().id), |writer| {
827 ReactiveContext::new_for_scope(
828 self.sender.clone(),
829 assigned_scope_id,
830 writer,
831 )
832 }),
833 );
834 self.dirty_scopes.insert(assigned_scope_id);
835 }
836
837 let was_dirty = self.dirty_scopes.remove(&assigned_scope_id);
838
839 if !was_dirty {
840 return;
842 }
843
844 let scope_rc = self.scopes.get(&assigned_scope_id).cloned().unwrap();
845
846 let element = hotpath::measure_block!("Scope Rendering", {
847 CurrentContext::run_with_reactive(
848 CurrentContext {
849 scope_id: assigned_scope_id,
850 scopes_storages: self.scopes_storages.clone(),
851 tasks: self.tasks.clone(),
852 task_id_counter: self.task_id_counter.clone(),
853 sender: self.sender.clone(),
854 },
855 || {
856 let scope = scope_rc.borrow();
857 #[cfg(feature = "hotreload")]
858 {
859 subsecond::call(|| (scope.comp)(scope.props.clone()))
860 }
861 #[cfg(not(feature = "hotreload"))]
862 {
863 (scope.comp)(scope.props.clone())
864 }
865 },
866 )
867 });
868
869 let path_element = PathElement::from_element(vec![0], element);
870 let mut diff = Diff::default();
871 path_element.diff(scope_rc.borrow().element.as_ref(), &mut diff);
872
873 self.apply_diff(&scope_rc, diff, mutations, &path_element);
874
875 self.run_scope(&scope_rc, &path_element, mutations, visited_scopes);
876 let mut scopes_storages = self.scopes_storages.borrow_mut();
877 let scope_storage = scopes_storages.get_mut(&assigned_scope_id).unwrap();
878 scope_storage.current_value = 0;
879 scope_storage.current_run += 1;
880
881 scope_rc.borrow_mut().element = Some(path_element);
882 }
883 PathElement::Element { elements, .. } => {
884 for element in elements.iter() {
885 self.run_scope(scope, element, mutations, visited_scopes);
886 }
887 }
888 }
889 }
890
891 fn find_scope_root_parent_info(
894 &self,
895 parent_id: Option<ScopeId>,
896 parent_node_id: NodeId,
897 scope_id: ScopeId,
898 ) -> (NodeId, u32) {
899 let mut index_inside_parent = 0;
900
901 if let Some(parent_id) = parent_id {
902 let mut buffer = Some((parent_id, parent_node_id, scope_id));
903 while let Some((parent_id, parent_node_id, scope_id)) = buffer.take() {
904 let parent_scope = self.scopes.get(&parent_id).unwrap();
905 let parent_scope = parent_scope.borrow();
906
907 let scope = self.scopes.get(&scope_id).unwrap();
908 let scope = scope.borrow();
909
910 let path_node_parent = parent_scope.nodes.find(|v| {
911 if let Some(v) = v {
912 v.node_id == parent_node_id
913 } else {
914 false
915 }
916 });
917
918 if let Some(path_node_parent) = path_node_parent {
919 if let Some(scope_id) = path_node_parent.scope_id {
920 if let Some(parent_id) = parent_scope.parent_id {
921 buffer.replace((
923 parent_id,
924 parent_scope.parent_node_id_in_parent,
925 scope_id,
926 ));
927 } else {
928 assert_eq!(scope_id, ScopeId::ROOT);
929 }
930 } else {
931 index_inside_parent = *scope.path_in_parent.last().unwrap();
933 return (parent_node_id, index_inside_parent);
934 }
935 } else if let Some(new_parent_id) = parent_scope.parent_id {
936 buffer.replace((
938 new_parent_id,
939 parent_scope.parent_node_id_in_parent,
940 parent_id,
941 ));
942 }
943 }
944 } else {
945 assert_eq!(scope_id, ScopeId::ROOT);
946 }
947
948 (parent_node_id, index_inside_parent)
949 }
950
951 fn find_scope_root_node_id(&self, scope_id: ScopeId) -> NodeId {
955 let scope_rc = self.scopes.get(&scope_id).unwrap();
956 let scope = scope_rc.borrow();
957 let path_node = scope.nodes.get(&[0]).unwrap();
958 if let Some(child_scope_id) = path_node.scope_id {
959 self.find_scope_root_node_id(child_scope_id)
960 } else {
961 path_node.node_id
962 }
963 }
964
965 fn process_addition(
966 &mut self,
967 scope: &Rc<RefCell<Scope>>,
968 added: &[u32],
969 path_element: &PathElement,
970 mutations: &mut Mutations,
971 parents_to_resync_scopes: &mut FxHashSet<Box<[u32]>>,
972 ) {
973 let (parent_node_id, index_inside_parent) = if added == [0] {
974 let parent_id = scope.borrow().parent_id;
975 let scope_id = scope.borrow().id;
976 let parent_node_id = scope.borrow().parent_node_id_in_parent;
977 self.find_scope_root_parent_info(parent_id, parent_node_id, scope_id)
978 } else {
979 parents_to_resync_scopes.insert(Box::from(&added[..added.len() - 1]));
980 (
981 scope
982 .borrow()
983 .nodes
984 .get(&added[..added.len() - 1])
985 .unwrap()
986 .node_id,
987 added[added.len() - 1],
988 )
989 };
990
991 self.node_id_counter += 1;
992
993 path_element.with_element(added, |element| match element {
994 PathElement::Component { .. } => {
995 self.scope_id_counter += 1;
996 let scope_id = self.scope_id_counter;
997
998 scope.borrow_mut().nodes.insert(
999 added,
1000 PathNode {
1001 node_id: self.node_id_counter,
1002 scope_id: Some(scope_id),
1003 },
1004 );
1005 }
1006 PathElement::Element { element, .. } => {
1007 mutations.added.push(MutationAdd {
1008 node_id: self.node_id_counter,
1009 parent_id: parent_node_id,
1010 index: index_inside_parent,
1011 element: element.clone(),
1012 });
1013
1014 self.node_to_scope
1015 .insert(self.node_id_counter, scope.borrow().id);
1016 scope.borrow_mut().nodes.insert(
1017 added,
1018 PathNode {
1019 node_id: self.node_id_counter,
1020 scope_id: None,
1021 },
1022 );
1023 }
1024 });
1025 }
1026
1027 #[cfg_attr(feature = "hotpath", hotpath::measure)]
1028 fn apply_diff(
1029 &mut self,
1030 scope: &Rc<RefCell<Scope>>,
1031 diff: Diff,
1032 mutations: &mut Mutations,
1033 path_element: &PathElement,
1034 ) {
1035 let mut moved_nodes =
1036 FxHashMap::<Box<[u32]>, (NodeId, FxHashMap<u32, PathNode>)>::default();
1037 let mut parents_to_resync_scopes = FxHashSet::default();
1038
1039 for (parent, movements) in &diff.moved {
1042 parents_to_resync_scopes.insert(parent.clone());
1043 let old_parent = resolve_old_path(parent, &diff.moved);
1046 let paths = moved_nodes.entry(parent.clone()).or_insert_with(|| {
1047 let parent_node_id = scope.borrow().nodes.get(&old_parent).unwrap().node_id;
1048 (parent_node_id, FxHashMap::default())
1049 });
1050
1051 for (from, _to) in movements.iter() {
1052 let mut old_child_path = old_parent.clone();
1053 old_child_path.push(*from);
1054
1055 let path_node = scope.borrow().nodes.get(&old_child_path).cloned().unwrap();
1056
1057 paths.1.insert(*from, path_node);
1058 }
1059 }
1060
1061 let mut selected_roots: HashMap<&[u32], HashSet<&[u32]>> = HashMap::default();
1063 let mut scope_removal_buffer = vec![];
1064
1065 'remove: for removed in diff.removed.iter().sorted_by(|a, b| {
1085 for (x, y) in a.iter().zip(b.iter()) {
1086 match x.cmp(y) {
1087 Ordering::Equal => continue,
1088 non_eq => return non_eq.reverse(),
1089 }
1090 }
1091 b.len().cmp(&a.len())
1092 }) {
1093 parents_to_resync_scopes.insert(Box::from(&removed[..removed.len() - 1]));
1094
1095 let path_node = scope.borrow().nodes.get(removed).cloned();
1096 if let Some(PathNode { node_id, scope_id }) = path_node {
1097 if scope_id.is_none() {
1098 let index_inside_parent = if removed.as_ref() == [0] {
1099 let parent_id = scope.borrow().parent_id;
1100 let scope_id = scope.borrow().id;
1101 let parent_node_id = scope.borrow().parent_node_id_in_parent;
1102 self.find_scope_root_parent_info(parent_id, parent_node_id, scope_id)
1103 .1
1104 } else {
1105 removed[removed.len() - 1]
1107 };
1108
1109 mutations.removed.push(MutationRemove::Element {
1111 id: node_id,
1112 index: index_inside_parent,
1113 });
1114 }
1115
1116 for (root, inner) in &mut selected_roots {
1118 if is_descendant(removed, root) {
1119 inner.insert(removed);
1120 continue 'remove;
1121 }
1122 }
1123
1124 selected_roots.retain(|root, _| !is_descendant(root, removed));
1126
1127 selected_roots
1128 .entry(&removed[..removed.len() - 1])
1129 .or_default()
1130 .insert(removed);
1131 } else {
1132 unreachable!()
1133 }
1134 }
1135
1136 for (root, removed) in selected_roots.iter().sorted_by(|(a, _), (b, _)| {
1138 for (x, y) in a.iter().zip(b.iter()) {
1139 match x.cmp(y) {
1140 Ordering::Equal => continue,
1141 non_eq => return non_eq.reverse(),
1142 }
1143 }
1144 b.len().cmp(&a.len())
1145 }) {
1146 scope.borrow_mut().nodes.retain(
1147 root,
1148 |p, _| !removed.contains(p),
1149 |_, &PathNode { scope_id, node_id }| {
1150 if let Some(scope_id) = scope_id {
1151 scope_removal_buffer.push(self.scopes.get(&scope_id).cloned().unwrap());
1153 } else {
1154 self.node_to_scope.remove(&node_id).unwrap();
1155 }
1156 },
1157 );
1158 }
1159
1160 let mut scope_removal_queue = VecDeque::new();
1161
1162 while let Some(scope_rc) = scope_removal_buffer.pop() {
1163 scope_removal_queue.push_front(scope_rc.clone());
1166
1167 let scope = scope_rc.borrow_mut();
1168
1169 let mut scope_root_node_id = None;
1170
1171 scope
1173 .nodes
1174 .traverse(&[], |path, &PathNode { scope_id, node_id }| {
1175 if let Some(scope_id) = scope_id {
1176 scope_removal_buffer.push(self.scopes.get(&scope_id).cloned().unwrap());
1177 } else {
1178 self.node_to_scope.remove(&node_id).unwrap();
1179 }
1180 if path == [0] {
1181 scope_root_node_id = Some(node_id);
1182 }
1183 });
1184
1185 mutations.removed.push(MutationRemove::Scope {
1188 id: scope_root_node_id.unwrap(),
1189 });
1190 }
1191
1192 for scope_rc in scope_removal_queue {
1194 let scope = scope_rc.borrow_mut();
1195
1196 self.scopes.remove(&scope.id);
1197
1198 CurrentContext::run_with_reactive(
1200 CurrentContext {
1201 scope_id: scope.id,
1202 scopes_storages: self.scopes_storages.clone(),
1203 tasks: self.tasks.clone(),
1204 task_id_counter: self.task_id_counter.clone(),
1205 sender: self.sender.clone(),
1206 },
1207 || {
1208 let mut _removed_tasks = Vec::new();
1210
1211 self.tasks.borrow_mut().retain(|task_id, task| {
1212 if task.borrow().scope_id == scope.id {
1213 _removed_tasks.push((*task_id, task.clone()));
1214 false
1215 } else {
1216 true
1217 }
1218 });
1219 drop(_removed_tasks);
1220 let _scope = self.scopes_storages.borrow_mut().remove(&scope.id);
1222 },
1223 );
1224 }
1225
1226 let mut deferred_adds = Vec::new();
1249
1250 for added in diff
1251 .added
1252 .iter()
1253 .sorted_by(|a, b| {
1254 for (x, y) in a.iter().zip(b.iter()) {
1255 match x.cmp(y) {
1256 Ordering::Equal => continue,
1257 non_eq => return non_eq.reverse(),
1258 }
1259 }
1260 b.len().cmp(&a.len())
1261 })
1262 .rev()
1263 {
1264 let parent = &added[..added.len() - 1];
1265 let has_moved_ancestor = resolve_old_path(parent, &diff.moved) != *parent;
1266 if has_moved_ancestor {
1267 deferred_adds.push(added.clone());
1268 continue;
1269 }
1270
1271 self.process_addition(
1272 scope,
1273 added,
1274 path_element,
1275 mutations,
1276 &mut parents_to_resync_scopes,
1277 );
1278 }
1279
1280 for (parent, movements) in diff.moved.into_iter().sorted_by(|(a, _), (b, _)| {
1281 for (x, y) in a.iter().zip(b.iter()) {
1282 match x.cmp(y) {
1283 Ordering::Equal => continue,
1284 non_eq => return non_eq.reverse(),
1285 }
1286 }
1287 a.len().cmp(&b.len())
1288 }) {
1289 parents_to_resync_scopes.insert(parent.clone());
1290
1291 let (parent_node_id, paths) = moved_nodes.get_mut(&parent).unwrap();
1292
1293 for (from, to) in movements.into_iter().sorted_by_key(|e| e.0).rev() {
1294 let path_node = paths.remove(&from).unwrap();
1295
1296 let PathNode { node_id, scope_id } = path_node;
1297
1298 let from_path = scope
1300 .borrow()
1301 .nodes
1302 .find_child_path(&parent, |v| v == Some(&path_node))
1303 .unwrap();
1304
1305 let mut to_path = parent.to_vec();
1306 to_path.push(to);
1307
1308 if from_path == to_path {
1309 continue;
1310 }
1311
1312 let path_entry = scope.borrow_mut().nodes.remove(&from_path).unwrap();
1314 scope.borrow_mut().nodes.insert_entry(&to_path, path_entry);
1315
1316 if let Some(scope_id) = scope_id {
1317 let scope_root_node_id = self.find_scope_root_node_id(scope_id);
1318 let scope_rc = self.scopes.get(&scope_id).cloned().unwrap();
1319 let scope = scope_rc.borrow();
1320
1321 mutations
1323 .moved
1324 .entry(scope.parent_node_id_in_parent)
1325 .or_default()
1326 .push(MutationMove {
1327 index: to,
1328 node_id: scope_root_node_id,
1329 });
1330 } else {
1331 mutations
1333 .moved
1334 .entry(*parent_node_id)
1335 .or_default()
1336 .push(MutationMove { index: to, node_id });
1337 }
1338 }
1339 }
1340
1341 for added in &deferred_adds {
1343 self.process_addition(
1344 scope,
1345 added,
1346 path_element,
1347 mutations,
1348 &mut parents_to_resync_scopes,
1349 );
1350 }
1351
1352 for (modified, flags) in diff.modified {
1353 path_element.with_element(&modified, |element| match element {
1354 PathElement::Component { .. } => {
1355 unreachable!()
1357 }
1358 PathElement::Element { element, .. } => {
1359 let node_id = scope
1360 .borrow()
1361 .nodes
1362 .get(&modified)
1363 .map(|path_node| path_node.node_id)
1364 .unwrap();
1365 mutations.modified.push(MutationModified {
1366 node_id,
1367 element: element.clone(),
1368 flags,
1369 });
1370 }
1371 });
1372 }
1373
1374 for parent in parents_to_resync_scopes {
1377 if diff.added.contains(&parent) {
1380 continue;
1382 }
1383
1384 scope
1386 .borrow_mut()
1387 .nodes
1388 .traverse_1_level(&parent, |p, path_node| {
1389 if let Some(scope_id) = path_node.scope_id
1390 && let Some(scope_rc) = self.scopes.get(&scope_id)
1391 {
1392 let mut scope = scope_rc.borrow_mut();
1393 scope.path_in_parent = Box::from(p);
1394 }
1395 });
1396 }
1397 }
1398
1399 pub fn mark_all_scopes_dirty(&mut self) {
1400 self.dirty_scopes.extend(self.scopes.keys());
1401 let _ = self
1402 .sender
1403 .unbounded_send(Message::MarkScopeAsDirty(ScopeId::ROOT));
1404 }
1405
1406 pub fn clear_all_scopes_storages(&mut self) {
1411 let mut scopes_storages = self.scopes_storages.borrow_mut();
1412 let scopes = self
1413 .scopes
1414 .iter()
1415 .sorted_by_key(|(_, s)| s.borrow().height)
1416 .map(|(_, s)| s.borrow().id)
1417 .collect::<Vec<_>>();
1418
1419 for scope_id in scopes {
1420 CurrentContext::run(
1421 CurrentContext {
1422 scope_id,
1423 scopes_storages: self.scopes_storages.clone(),
1424 tasks: self.tasks.clone(),
1425 task_id_counter: self.task_id_counter.clone(),
1426 sender: self.sender.clone(),
1427 },
1428 || {
1429 if let Some(storage) = scopes_storages.get_mut(&scope_id) {
1430 storage.reset_hooks();
1431 }
1432 },
1433 );
1434 }
1435 }
1436
1437 pub fn clear_all_tasks(&mut self) {
1442 self.tasks.borrow_mut().clear();
1443 self.dirty_tasks.clear();
1444 while self.receiver.try_recv().is_ok() {}
1445 }
1446}
1447
1448#[derive(Default, Debug)]
1449pub struct Diff {
1450 pub added: Vec<Box<[u32]>>,
1451
1452 pub modified: Vec<(Box<[u32]>, DiffModifies)>,
1453
1454 pub removed: Vec<Box<[u32]>>,
1455
1456 pub moved: HashMap<Box<[u32]>, Vec<(u32, u32)>>,
1457}
1458
1459fn resolve_old_path(new_path: &[u32], moved: &HashMap<Box<[u32]>, Vec<(u32, u32)>>) -> Vec<u32> {
1464 let mut old_path = Vec::with_capacity(new_path.len());
1465 for i in 0..new_path.len() {
1466 let new_parent = &new_path[..i];
1467 let new_index = new_path[i];
1468 if let Some(movements) = moved.get(new_parent)
1469 && let Some(&(from, _)) = movements.iter().find(|(_, to)| *to == new_index)
1470 {
1471 old_path.push(from);
1472 continue;
1473 }
1474 old_path.push(new_index);
1475 }
1476 old_path
1477}
1478
1479fn is_descendant(candidate: &[u32], ancestor: &[u32]) -> bool {
1480 if ancestor.len() > candidate.len() {
1481 return false;
1482 }
1483 candidate[..ancestor.len()] == *ancestor
1484}