Skip to main content

ragnarok/
executor.rs

1use alloc::{
2    collections::VecDeque,
3    vec::Vec,
4};
5
6use crate::{
7    EmmitableEvent,
8    NameOfEvent,
9    NodeKey,
10    NodesState,
11    NodesStatesUpdate,
12    PotentialEvent,
13    SourceEvent,
14};
15
16#[derive(Clone, Debug, PartialEq)]
17pub struct ProcessedEvents<
18    Key: NodeKey,
19    Name: NameOfEvent,
20    Emmitable: EmmitableEvent,
21    Source: SourceEvent,
22> {
23    pub emmitable_events: Vec<Emmitable>,
24    pub flattened_potential_events: Vec<PotentialEvent<Key, Name, Source>>,
25    pub nodes_states_update: NodesStatesUpdate<Key>,
26}
27
28impl<Key: NodeKey, Name: NameOfEvent, Emmitable: EmmitableEvent, Source: SourceEvent> Default
29    for ProcessedEvents<Key, Name, Emmitable, Source>
30{
31    fn default() -> Self {
32        Self {
33            emmitable_events: Vec::default(),
34            flattened_potential_events: Vec::default(),
35            nodes_states_update: NodesStatesUpdate::default(),
36        }
37    }
38}
39
40pub trait EventsExecutor
41where
42    Self: core::marker::Sized,
43{
44    type Name: NameOfEvent;
45    type Key: NodeKey;
46    type Emmitable: EmmitableEvent<Key = Self::Key, Name = Self::Name>;
47    type Source: SourceEvent;
48
49    /// Call the event handler of the given [Self::Emmitable].
50    fn emit_event(&mut self, event: Self::Emmitable) -> bool;
51
52    // All events have been emitted
53    fn emitted_events(&mut self) {}
54}
55
56impl<T: EventsExecutor> private::Sealed for T {}
57
58impl<T: EventsExecutor + private::Sealed> EventsExecutorRunner for T {
59    type Name = T::Name;
60    type Key = T::Key;
61    type Emmitable = T::Emmitable;
62    type Source = T::Source;
63    fn run(
64        mut self,
65        nodes_state: &mut NodesState<Self::Key>,
66        ProcessedEvents {
67            emmitable_events,
68            flattened_potential_events,
69            mut nodes_states_update,
70        }: ProcessedEvents<Self::Key, Self::Name, Self::Emmitable, Self::Source>,
71    ) {
72        let mut emmitable_events: VecDeque<Self::Emmitable> = emmitable_events.into();
73        let mut processed_events = Vec::<Self::Emmitable>::new();
74
75        #[cfg(debug_assertions)]
76        tracing::info!("Processing {} Tree events", emmitable_events.len());
77
78        while let Some(emmitable_event) = emmitable_events.pop_front() {
79            let default_action_enabled = self.emit_event(emmitable_event.clone());
80
81            if !default_action_enabled {
82                // Get the events that this event can cancel
83                let cancellable_events = emmitable_event.name().get_cancellable_events();
84
85                // Remove the rest of emmitable events that are cancellable
86                emmitable_events.retain(|event| !cancellable_events.contains(&event.name()));
87
88                // Discard the potential events that dont find a matching emmitable event
89                // So for instance, a cancelled potential mousemove event wont be discarded if a emmitable mousenter was processed before
90                // At the same time, a cancelled potential mousemove event that actually gets discarded will only discard the node state
91                // made in this run, but will not change what was already before this run
92                // So if the affected node was already being hovered from the last events run, it will continue to be as so
93                for potential_event in &flattened_potential_events {
94                    let is_cancellable = cancellable_events.contains(&potential_event.name);
95                    if is_cancellable {
96                        let processed_event = processed_events.iter().find(|event| {
97                            potential_event.name == event.source()
98                                && potential_event.node_key == event.key()
99                        });
100                        if processed_event.is_none() {
101                            nodes_states_update
102                                .discard(&potential_event.name, &potential_event.node_key);
103                        }
104                    }
105                }
106            }
107
108            processed_events.push(emmitable_event);
109        }
110
111        self.emitted_events();
112
113        nodes_state.apply_update(nodes_states_update);
114    }
115}
116
117pub trait EventsExecutorRunner: private::Sealed
118where
119    Self: core::marker::Sized,
120{
121    type Name: NameOfEvent;
122    type Key: NodeKey;
123    type Emmitable: EmmitableEvent<Key = Self::Key, Name = Self::Name>;
124    type Source: SourceEvent;
125
126    fn run(
127        self,
128        nodes_state: &mut NodesState<Self::Key>,
129        processed_events: ProcessedEvents<Self::Key, Self::Name, Self::Emmitable, Self::Source>,
130    );
131}
132
133#[doc(hidden)]
134mod private {
135    pub trait Sealed {}
136}