about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-07-21 14:42:50 +0200
committerbptato <nincsnevem662@gmail.com>2024-07-21 14:43:14 +0200
commit6216aa6733b72fa631546fc6afa591305e45fcf4 (patch)
tree0c4e1fc11c706464dd62058ecf51a47fab6a565b
parent1369135f9719ebe7a58c75fd73ecbfec92ef7e24 (diff)
downloadchawan-6216aa6733b72fa631546fc6afa591305e45fcf4.tar.gz
buffer: replace dispatchEvent procs with that in dom
-rw-r--r--src/html/dom.nim2
-rw-r--r--src/html/event.nim5
-rw-r--r--src/server/buffer.nim37
3 files changed, 7 insertions, 37 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim
index 2cee89ac..d2682efc 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -2460,6 +2460,8 @@ proc dispatchEvent0(window: Window; event: Event; currentTarget: EventTarget;
   event.currentTarget = currentTarget
   var els = currentTarget.eventListeners # copy intentionally
   for el in els:
+    if JS_IsUndefined(el.callback):
+      continue # removed, presumably by a previous handler
     if el.ctype == event.ctype:
       let e = ctx.invoke(el, event)
       called = true
diff --git a/src/html/event.nim b/src/html/event.nim
index 3d2a3e3a..d396f3ba 100644
--- a/src/html/event.nim
+++ b/src/html/event.nim
@@ -52,14 +52,12 @@ type
 
   EventListener* = ref object
     ctype*: CAtom
+    # if callback is undefined, the listener has been removed
     callback*: EventListenerCallback
     capture: bool
     passive: Option[bool]
     once: bool
     #TODO AbortSignal
-    #TODO do we really need `removed'? maybe we could just check if
-    # callback is undefined.
-    removed*: bool
 
 jsDestructor(Event)
 jsDestructor(CustomEvent)
@@ -233,7 +231,6 @@ proc addAnEventListener(ctx: JSContext; target: EventTarget;
 
 proc removeAnEventListener(eventTarget: EventTarget; ctx: JSContext; i: int) =
   let listener = eventTarget.eventListeners[i]
-  listener.removed = true
   JS_FreeValue(ctx, listener.callback)
   listener.callback = JS_UNDEFINED
   eventTarget.eventListeners.delete(i)
diff --git a/src/server/buffer.nim b/src/server/buffer.nim
index 153ebbb6..3864d9c6 100644
--- a/src/server/buffer.nim
+++ b/src/server/buffer.nim
@@ -989,44 +989,15 @@ proc clone*(buffer: Buffer; newurl: URL): int {.proxy.} =
 
 proc dispatchDOMContentLoadedEvent(buffer: Buffer) =
   let window = buffer.window
-  let ctx = window.jsctx
-  let document = buffer.document
-  let adcl = window.toAtom(satDOMContentLoaded)
-  let event = newEvent(adcl, document)
-  var called = false
-  var els = document.eventListeners
-  for el in els:
-    if el.removed:
-      continue
-    if el.ctype == adcl:
-      let e = ctx.invoke(el, event)
-      if JS_IsException(e):
-        ctx.writeException(buffer.estream)
-      JS_FreeValue(ctx, e)
-      called = true
-      if efStopImmediatePropagation in event.flags:
-        break
+  let event = newEvent(window.toAtom(satDOMContentLoaded), buffer.document)
+  let (called, _) = window.dispatchEvent(event, buffer.document)
   if called:
     buffer.do_reshape()
 
 proc dispatchLoadEvent(buffer: Buffer) =
   let window = buffer.window
-  let ctx = window.jsctx
-  let aload = window.toAtom(satLoad)
-  let event = newEvent(aload, window)
-  var called = false
-  var els = window.eventListeners
-  for el in els:
-    if el.removed:
-      continue
-    if el.ctype == aload:
-      let e = ctx.invoke(el, event)
-      if JS_IsException(e):
-        ctx.writeException(buffer.estream)
-      JS_FreeValue(ctx, e)
-      called = true
-      if efStopImmediatePropagation in event.flags:
-        break
+  let event = newEvent(window.toAtom(satLoad), window)
+  let (called, _) = window.dispatchEvent(event, window)
   if called:
     buffer.do_reshape()