Move some details implementation from EventTarget to the parser

Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
Francis Bouvier
2024-01-17 15:29:48 +01:00
parent ae6a714573
commit 86cc3d25dc
2 changed files with 21 additions and 18 deletions

View File

@@ -17,18 +17,6 @@ pub const EventTarget = struct {
// JS funcs
// --------
const js_handler = struct {
fn handle(event: ?*parser.Event, data: ?*anyopaque) callconv(.C) void {
const ptr: *align(@alignOf(*Callback)) anyopaque = @alignCast(data.?);
const func = @as(*Callback, @ptrCast(ptr));
func.call(.{event}) catch unreachable;
// NOTE: we can not call func.deinit here
// b/c the handler can be called several times
// as the event goes through the ancestors
// TODO: check the event phase to call func.deinit and free func
}
}.handle;
pub fn _addEventListener(
self: *parser.EventTarget,
alloc: std.mem.Allocator,
@@ -38,8 +26,7 @@ pub const EventTarget = struct {
// TODO: when can we free this allocation?
const cbk_ptr = try alloc.create(Callback);
cbk_ptr.* = cbk;
const func = @as(*anyopaque, @ptrCast(cbk_ptr));
try parser.eventTargetAddEventListener(self, eventType, func, js_handler);
try parser.eventTargetAddEventListener(self, eventType, cbk_ptr);
}
pub fn _dispatchEvent(self: *parser.EventTarget, event: *parser.Event) !bool {