豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Wrapping Event in a C++ class so that every where you pass an event it works. #1661

@greggman

Description

@greggman

in JavaScript I can do this

class MyEvent extends Event {
  constructor(type, msg) {
    super(type);
    this.message = msg;
  }
}
const target = new EventTarget();
const e = await new Promise(resolve => {
  target.addEventListener('custom', resolve);
  target.dispatchEvent(new MyEvent('custom', 'hello'));
});
assert.ok(e.message === 'hello');

Is it possible to have MyEvent be a C++ class?

In my current attempt, when I call target.dispatchEvent I get

node:internal/event_target:220
      throw new ERR_INVALID_THIS('Event');
            ^

TypeError [ERR_INVALID_THIS]: Value of "this" must be of type Event
    at get type [as type] (node:internal/event_target:220:13)
    at EventTarget.dispatchEvent (node:internal/event_target:755:40)

I tried setting the prototype chain in C++ and this passes

assert.ok(new MyEvent('custom', 'hello') instanceof Event);

but of course it's not actually an Event, it's a non-event who's prototype chain contains Event which is what node is complaining about.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Has PR

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions