DEV Community

azu
azu

Posted on

2 1

eventmit - Tiny EventEmitter

I've created eventmit as Universal EventEmitter library.

You can use eventmit on browser, Node.js, also Deno.

Feature

  • A single event object per an event
  • Tiny code base
  • Written by TypeScript

Its all code of eventmit.

var n = function () {var n = new Set();return { on: function (t) {n.add(t);}, off: function (t) {n.delete(t);}, offAll: function () {n.clear();}, emit: function (t) {n.forEach(function (n) {return n(t);});} };};export { n as eventmit };
Enter fullscreen mode Exit fullscreen mode

Usage

You can create eventmit object per an event.
In other words, eventmit is an single event object.

import { eventmit } from "eventmit";
const event = eventmit<{ key: string }>();
// Register handler
event.on((value) => {
    console.log(1, value);
});
event.on((value) => {
    console.log(2, value);
});
// Invoke handler
event.emit({
    key: "value"
});
// Unregister handler
event.offAll();
Enter fullscreen mode Exit fullscreen mode

eventmit has not support multiple event type like EventEmitter or developit/mitt.
It is simple and enforce strong type with TypeScript.

import { eventmit } from "eventmit";
const changeEvent = eventmit<void>();
const inputEvent = eventmit<string>();
// subscription event
changeEvent.on(() => console.log("CHANGE!"));
inputEvent.on((input) => console.log(input));
Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️