A single event object per an event.
- A single event object per an event
- Tiny code base - less 1kb
- Written by TypeScript
Install with npm:
npm install eventmit
Requirement: ECMAScript 2015 because this library use Set
.
Create an eventmit object and register handler and invoke handlers.
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();
export declare type EventmitHandler<T> = (value: T) => any;
export declare type Eventmitter<T> = {
/**
* Register an event handler
*/
on: (handler: EventmitHandler<T>) => void;
/**
* Remove an event handler
*/
off: (handler: EventmitHandler<T>) => void;
/**
* Remove all event handlers
*/
offAll: () => void;
/**
* Invoke all handlers
*/
emit: (value: T) => void;
};
export declare const eventmit: <T>() => Eventmitter<T>;
You can import eventmit
as ES Modules.
import { eventmit } from "https://unpkg.com/eventmit?module";
const event = eventmit();
// Register handler
event.on((value) => {
console.log(value);
});
// Invoke handler
event.emit("value");
It means that eventmit work on Browser and Deno.
If you are using Deno, import eventmit
from a URL.
For example, using a CDN:
import { eventmit } from "https://cdn.skypack.dev/eventmit?dts";
See Releases page.
Install devDependencies and Run npm test
:
npm test
Pull requests and stars are always welcome.
For bugs and feature requests, please create an issue.
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
MIT © azu
- developit/mitt: 🥊 Tiny 200 byte functional event emitter / pubsub.
- Support multiple event type