eventmit
TypeScript icon, indicating that this package has built-in type declarations

2.0.4 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i eventmit

Weekly Downloads

483

Version

2.0.4

License

MIT

Unpacked Size

12.3 kB

Total Files

12

Last publish

Collaborators

  • azu