DEV Community

azu
azu

Posted on

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

Top comments (0)