Skip to content

Delegated events throw when the handler is an EventListenerObject #3206

Description

@nickshiro

Describe the bug

addEvent() accepts EventListenerObject handlers, but delegated event dispatch assumes every handler is a function and calls handler.call(...).

When an object with handleEvent() is used for a delegated event such as click, dispatch throws:

TypeError: handler.call is not a function

The same handler shape works for non-delegated events because it is passed directly to the native addEventListener().

Steps to Reproduce the Bug or Issue

import {
    addEvent,
    delegateEvents,
    render
  } from "@solidjs/web";

  const container = document.createElement("div");
  const button = document.createElement("button");
  document.body.append(container);

  const calls: Event[] = [];

  const listener = {
    handleEvent(event: Event) {
      calls.push(event);
    }
  };

  const dispose = render(() => button, container);

  addEvent(button, "click", listener, true);
  delegateEvents(["click"]);

  button.click();

  // Expected: 1
  // Actual: 0, and dispatch throws:
  // TypeError: handler.call is not a function
  console.log(calls.length);

  dispose();
  container.remove();

Expected behavior

The delegated event dispatcher should invoke:

  listener.handleEvent(event);

This would match the native EventTarget behavior and the EventListenerObject type accepted by addEvent().

Additional context

Type check in the delegated dispatcher makes the regression test pass:

  if (typeof handler === "function") {
    handler.call(node, event);
  } else {
    handler.handleEvent(event);
  }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions