@@ -23,38 +23,57 @@ pub fn dispatchEvent(self: *EventTarget, event: *Event, page: *Page) !bool {
2323 return ! event ._cancelable or ! event ._prevent_default ;
2424}
2525
26- const addEventListenerOptions = union (enum ) {
26+ const AddEventListenerOptions = union (enum ) {
2727 capture : bool ,
2828 options : RegisterOptions ,
2929};
30- pub fn addEventListener (self : * EventTarget , typ : []const u8 , callback : js.Function , opts_ : ? addEventListenerOptions , page : * Page ) ! void {
30+
31+ pub const EventListenerCallback = union (enum ) {
32+ function : js.Function ,
33+ object : js.Object ,
34+ };
35+ pub fn addEventListener (self : * EventTarget , typ : []const u8 , callback_ : ? EventListenerCallback , opts_ : ? AddEventListenerOptions , page : * Page ) ! void {
36+ const callback = callback_ orelse return ;
37+
38+ const actual_callback = switch (callback ) {
39+ .function = > | func | func ,
40+ .object = > | obj | (try obj .getFunction ("handleEvent" )) orelse return ,
41+ };
42+
3143 const options = blk : {
3244 const o = opts_ orelse break :blk RegisterOptions {};
3345 break :blk switch (o ) {
3446 .options = > | opts | opts ,
3547 .capture = > | capture | RegisterOptions { .capture = capture },
3648 };
3749 };
38- return page ._event_manager .register (self , typ , callback , options );
50+ return page ._event_manager .register (self , typ , actual_callback , options );
3951}
4052
41- const removeEventListenerOptions = union (enum ) {
53+ const RemoveEventListenerOptions = union (enum ) {
4254 capture : bool ,
4355 options : Options ,
4456
4557 const Options = struct {
4658 useCapture : bool = false ,
4759 };
4860};
49- pub fn removeEventListener (self : * EventTarget , typ : []const u8 , callback : js.Function , opts_ : ? removeEventListenerOptions , page : * Page ) ! void {
61+ pub fn removeEventListener (self : * EventTarget , typ : []const u8 , callback_ : ? EventListenerCallback , opts_ : ? RemoveEventListenerOptions , page : * Page ) ! void {
62+ const callback = callback_ orelse return ;
63+
64+ const actual_callback = switch (callback ) {
65+ .function = > | func | func ,
66+ .object = > | obj | (try obj .getFunction ("handleEvent" )) orelse return ,
67+ };
68+
5069 const use_capture = blk : {
5170 const o = opts_ orelse break :blk false ;
5271 break :blk switch (o ) {
5372 .capture = > | capture | capture ,
5473 .options = > | opts | opts .useCapture ,
5574 };
5675 };
57- return page ._event_manager .remove (self , typ , callback , use_capture );
76+ return page ._event_manager .remove (self , typ , actual_callback , use_capture );
5877}
5978
6079pub fn format (self : * EventTarget , writer : * std.Io.Writer ) ! void {
0 commit comments