1.0.5 ā€¢ Published 12 months ago

@jcbhmr/node-45981 v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

Make globalThis an EventTarget

ā° Make the global object an EventTarget

npm.io

šŸ™Œ 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

npm Yarn pnpm

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

Node.js

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

JavaScript Node.js

ā¤ļø 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 start

What'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. šŸ˜

1.0.5

12 months ago

1.0.4

12 months ago