@jcbhmr/node-45981 v1.0.5
Make globalThis an EventTarget
ā° Make the global object an EventTarget
š Implements the idea from nodejs/node#45981 \ š“ Will be obsolete if nodejs/node#45993 is merged \ ā Doesn't add any event listeners or emitters \ š Only works in Node.js
Installation
You can install this package using npm, Yarn, pnpm, or any other Node.js package manager that supports npm packages:
npm install @jcbhmr/node-45981š This package only works in Node.js! Other export conditions will result in a non-existent file error:
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '***/node_modules/@jcbhmr/node-45981/DNE'
at ... {
code: 'ERR_MODULE_NOT_FOUND'
}Usage
All you need to do is import this polyfill in your app, and you're good to go!
š You can also use the --import=module CLI flag if you want to keep it out
of you main app code and apply it manually at runtime.
ā ļø Be warned that this is an unconditional polyfill that will always
reset the prototype of globalThis to EventTarget, even if it already is!
// Make sure you gate it behind a conditional!
if (!(globalThis instanceof EventTarget) && typeof process !== "undefined") {
await import("@jcbhmr/node-45981");
}
globalThis.addEventListener("hello", (event) => {
console.log(event.detail);
});
const event = new CustomEvent("hello", { detail: "world" });
globalThis.dispatchEvent(event);
//=> 'world'Development
ā¤ļø Future developers: Make sure to deprecate this package if nodejs/node#45993 is ever merged!
This package uses plain JavaScript with a .d.ts file. We don't even typecheck!
šØ Why would we do such a thing? Because this package is so simplistic, we can
get away with it. You can get started by cloning this repo and running
npm install. From there, just run npm start or npm test and you're on your
way! š
npm startWhat's in the name? This is an as-close-as-possible implementation of
nodejs/node#45981, so it made sense to follow the ${spec}-${section} style
naming convention, just with "node" being a spec now, and "45981" being the
section that we are implementing. š