nodejs / nodejs/node-addon-api

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

オープン
#1,661 コメント 8 件 リアクション 0 件 担当者 1 名 GitHub で見る

まだ誰も着手していません。

never-stale
主要言語
C++
スター
2.4k
フォーク
499
平均マージ
2日 11時間
マージ済み PR(30日)
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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。