use crate::dom::event::DomEvent;
use crate::ComponentImports;
#[macro_export]
macro_rules! handle_events_using_callbacks {
() => {
#[export_name = "__afia__$handle_event"]
#[allow(missing_docs)]
pub extern "C" fn handle_event(event_handle: i64, callback: *mut Callback) {
let callback: &'static Callback = unsafe { &*callback };
let event_handle =
$crate::dom::event::DomEvent::_private_new(event_handle, callback.imports());
(callback.callback())(event_handle, callback.context());
}
};
}
pub struct Callback {
context: isize,
imports: &'static ComponentImports,
callback: fn(DomEvent, isize),
}
impl Callback {
pub fn temporary_way_to_call_using_integer_event_handle(&self, event_handle: i64) {
(self.callback)(
DomEvent::_private_new(event_handle, self.imports),
self.context,
)
}
pub fn new_with_context_pointer<T>(
context: *mut T,
imports: &'static ComponentImports,
callback: fn(DomEvent, *mut T),
) -> Self {
let callback: fn(DomEvent, isize) = unsafe { std::mem::transmute(callback) };
Self {
context: context as isize,
callback,
imports,
}
}
pub fn new_without_context(imports: &'static ComponentImports, callback: fn(DomEvent)) -> Self {
Self {
context: callback as isize,
imports,
callback: |event: DomEvent, context: isize| {
let callback: fn(DomEvent) = unsafe { std::mem::transmute(context) };
callback(event)
},
}
}
#[doc(hidden)]
pub fn callback(&self) -> fn(DomEvent, isize) {
self.callback
}
#[doc(hidden)]
pub fn context(&self) -> isize {
self.context
}
#[doc(hidden)]
pub fn imports(&self) -> &'static ComponentImports {
self.imports
}
}