nodejs / nodejs/node-addon-api
Wrapping Event in a C++ class so that every where you pass an event it works.
未关闭
还没有人认领这个 Issue。
never-stale
- 主要语言
- C++
- 星标
- 2.4k
- 派生
- 499
- 平均合并
- 2 天 11 小时
- 30 天内合并 PR
- 2
描述
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.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
评估
这个 Issue 还没有评估数据。