123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081openBaseopenJs_of_ocamlincludeEvent_intftypet=..typet+=|Ignore|Viewport_changed|Stop_propagation|Prevent_default|Manyoftlist(* We use this table for dispatching to the appropriate handler in an efficient way. *)lethandlers:(t->unit)Hashtbl.M(Int).t=Hashtbl.create(moduleInt)~size:8(* All visibility handlers see all events, so a simple list is enough. *)letvisibility_handlers:(unit->unit)listref=ref[]moduleObj=structmoduleExtension_constructor=struct[@@@ocaml.warning"-3"]letid=Caml.Obj.extension_idletof_val=Caml.Obj.extension_constructorendendmoduleDefine(Handler:Handler):Swithtypeaction:=Handler.Action.tandtypet:=t=structtypet+=C:Handler.Action.t->tletkey=Obj.Extension_constructor.id[%extension_constructorC]let()=Hashtbl.add_exnhandlers~key~data:(funinp->matchinpwith|Cvalue->Handler.handlevalue|_->raise_s[%message"Unrecognized variant"]);;letinjectv=CvendmoduleDefine_visibility(VH:Visibility_handler)=structlet()=visibility_handlers:=VH.handle::!visibility_handlersendletget_keyt=Obj.Extension_constructor.id(Obj.Extension_constructor.of_valt)lethandle_registered_eventt=Hashtbl.find_exnhandlers(get_keyt)tmoduleExpert=structlethandleevt=letrechandlet=matchtwith|Ignore->()|Manyl->List.iter~f:handlel|Viewport_changed->List.iter!visibility_handlers~f:(funf->f())|Stop_propagation->(* Safe to do because [stopPropagation] is defined equivalently to
[preventDefault] *)Dom_html.stopPropagationevt|Prevent_default->Dom.preventDefaultevt|t->handle_registered_eventtinhandle;;letrechandle_non_dom_event_exnt=matchtwith|Ignore->()|Manyl->List.iter~f:handle_non_dom_event_exnl|Viewport_changed->List.iter!visibility_handlers~f:(funf->f())|Stop_propagation->failwith"[handle_non_dom_event_exn] called with [Stop_propagation] which requires a dom \
event"|Prevent_default->failwith"[handle_non_dom_event_exn] called with [Prevent_default] which requires a dom \
event"|t->handle_registered_eventt;;end