Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

azu/eventmit

Open more actions menu

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

About

Simple EventEmitter. A single event object per an event.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  
Morty Proxy This is a proxified and sanitized view of the page, visit original site.