1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//! Specialized async Executor built on top of winit event loop for running [`AsyncComponent`]

pub mod signal;
#[cfg(target_arch = "wasm32")]
mod wasm;

use std::{
    sync::atomic::Ordering,
    task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
};

use async_component_core::{
    context::{ComponentStream, StateContext},
    AsyncComponent,
};
use futures::StreamExt;
use winit::{
    event::Event,
    event_loop::{ControlFlow, EventLoop},
};

use crate::WinitComponent;

use ref_extended::ref_extended;

use self::signal::WinitSignal;

/// Reserved zero sized user event struct used for waking winit eventloop
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct ExecutorPollEvent;

/// Executor implemented on top of winit eventloop using user event.
///
/// See [`WinitSignal`] for more detail how it utilize winit user event.
#[derive(Debug)]
pub struct WinitExecutor {
    event_loop: Option<EventLoop<ExecutorPollEvent>>,

    state_signal: WinitSignal,
}

impl WinitExecutor {
    /// Create new [`WinitExecutor`]
    pub fn new(event_loop: EventLoop<ExecutorPollEvent>) -> Self {
        let state_signal = WinitSignal::new(event_loop.create_proxy());

        Self {
            event_loop: Some(event_loop),

            state_signal,
        }
    }

    fn poll_component(
        &'static self,
        component_stream: &mut ComponentStream<impl AsyncComponent>,
    ) -> Poll<()> {
        if self
            .state_signal
            .scheduled
            .compare_exchange(true, false, Ordering::AcqRel, Ordering::Acquire)
            .is_ok()
        {
            if let Poll::Ready(Some(_)) =
                component_stream.poll_next_unpin(&mut Context::from_waker(&unsafe {
                    Waker::from_raw(create_raw_waker(&self.state_signal))
                }))
            {
                self.state_signal.scheduled.store(true, Ordering::Release);
            }

            Poll::Ready(())
        } else {
            Poll::Pending
        }
    }

    /// Initializes the winit event loop and run component.
    ///
    /// See [`EventLoop`] for more detail about winit event loop
    pub fn run<C: AsyncComponent + WinitComponent + 'static>(
        mut self,
        func: impl FnOnce(&StateContext) -> C,
    ) -> ! {
        let event_loop = self.event_loop.take().unwrap();

        let mut stream = ComponentStream::new(func);

        let executor = self;
        ref_extended!(|&executor| event_loop.run(move |event, _, control_flow| {
            match event {
                Event::MainEventsCleared => {
                    stream
                        .component_mut()
                        .on_event(&mut Event::MainEventsCleared, control_flow);

                    if let ControlFlow::ExitWithCode(_) = control_flow {
                        return;
                    }

                    match executor.poll_component(&mut stream) {
                        Poll::Ready(_) => {
                            control_flow.set_poll();
                        }
                        Poll::Pending => {
                            control_flow.set_wait();
                        }
                    }
                }

                // Handled in Event::MainEventsCleared
                Event::UserEvent(_) => {}

                _ => {
                    stream
                        .component_mut()
                        .on_event(&mut event.map_nonuser_event().unwrap(), control_flow);
                }
            }
        }))
    }
}

fn create_raw_waker(signal: &'static WinitSignal) -> RawWaker {
    unsafe fn waker_clone(this: *const ()) -> RawWaker {
        create_raw_waker(&*(this as *const WinitSignal))
    }

    unsafe fn waker_wake(this: *const ()) {
        let this = &*(this as *const WinitSignal);
        this.wake_by_ref();
    }

    unsafe fn waker_wake_by_ref(this: *const ()) {
        let this = &*(this as *const WinitSignal);
        this.wake_by_ref();
    }

    unsafe fn waker_drop(_: *const ()) {}

    RawWaker::new(
        signal as *const _ as *const (),
        &RawWakerVTable::new(waker_clone, waker_wake, waker_wake_by_ref, waker_drop),
    )
}