Skip to content
/ eventmit Public

Simple EventEmitter. A single event object per an event.

License

Notifications You must be signed in to change notification settings

azu/eventmit

Repository files navigation

eventmit Actions Status

A single event object per an event.

Feature

  • A single event object per an event
  • Tiny code base - less 1kb
  • Written by TypeScript

Install

Install with npm:

npm install eventmit

Requirement: ECMAScript 2015 because this library use Set.

Usage

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();

API

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>;

ECMAScript Modules

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.

Import at 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";

Changelog

See Releases page.

Running tests

Install devDependencies and Run npm test:

npm test

Contributing

Pull requests and stars are always welcome.

For bugs and feature requests, please create an issue.

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

License

MIT © azu

Related