镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Support CustomEvent #40678

Description

@alshdavid

Is your feature request related to a problem? Please describe.
When trying to mimic browser APIs surrounding EventTarget, we frequently use CustomEvent which is currently unavailable in Node.

It's useful in that it allows us to dispatch events through an EventTarget and include a custom data payload.

In a some way, this is the closest we have to a standardised Observable API (specifically Subject) and it's very useful.

Describe the solution you'd like
CustomEvent exists

Describe alternatives you've considered
Writing a polyfill

class CustomEvent extends Event { 
  constructor(message, data) {
    super(message, data)
    this.detail = data.detail
  }
}

const et = new EventTarget()
et.addEventListener('message', ev => console.log(ev.detail))
et.dispatchEvent(new CustomEvent('message', { detail: 'foo' }))

Activity

  1. added
    eventtargetIssues and PRs related to the EventTarget implementation.
    on Oct 31, 2021
  2. moved this to Pending triage in Node.js feature requestson Jan 16, 2022
  3. github-actions commented on May 1, 2022

    @github-actions
    Contributor

    There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

    For more information on how the project manages feature requests, please consult the feature request management document.

  4. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on May 1, 2022
  5. moved this from Pending Triage to Stale in Node.js feature requestson May 1, 2022
  6. regseb commented on May 15, 2022

    @regseb
    Contributor

    Yet another polyfill proposal that:

    • handles no options;
    • uses null for the default value of detail;
    • define detail readonly.
    class CustomEvent extends Event {
        #detail;
    
        constructor(type, options) {
            super(type, options);
            this.#detail = options?.detail ?? null;
        }
    
        get detail() {
            return this.#detail;
        }
    }
  7. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on May 16, 2022
  8. bnoordhuis commented on May 17, 2022

    @bnoordhuis
    Member

    For anyone wanting to work on this, the steps to take are:

    1. Enable the dom/events WPT tests in test/wpt
    2. Make them pass for regular Events/EventTargets
    3. Then start working on CustomEvent

    W.r.t. (1):

    diff --git a/test/wpt/status/dom/events.json b/test/wpt/status/dom/events.json
    new file mode 100644
    index 00000000000..0967ef424bc
    --- /dev/null
    +++ b/test/wpt/status/dom/events.json
    @@ -0,0 +1 @@
    +{}
    diff --git a/test/wpt/test-events.js b/test/wpt/test-events.js
    new file mode 100644
    index 00000000000..5040d56d6a2
    --- /dev/null
    +++ b/test/wpt/test-events.js
    @@ -0,0 +1,7 @@
    +'use strict';
    +require('../common');
    +const { WPTRunner } = require('../common/wpt');
    +
    +const runner = new WPTRunner('dom/events');
    +
    +runner.runJsTests();

    Where dom/events is https://github.com/web-platform-tests/wpt/tree/master/dom/events

  9. added
    help wantedIssues that need assistance from volunteers or PRs that need help to proceed.
    good first issueIssues that are suitable for first-time contributors.
    on May 17, 2022
  10. 10 remaining items

  11. added a commit that references this issue on Jul 31, 2022
  12. moved this from Stale to Done in Node.js feature requestson Apr 4, 2023
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

    eventtargetIssues and PRs related to the EventTarget implementation.feature requestIssues requesting new Node.js features.good first issueIssues that are suitable for first-time contributors.help wantedIssues that need assistance from volunteers or PRs that need help to proceed.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions