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

2.3.0 • Public • Published

lite-emit

NPM version

A simple, lightweight, and fast event emitter.

Usage

import { LiteEmit } from "lite-emit";

interface Events {
	foo: [string];
	bar: ["bar", number, symbol];
	baz: [42];
}

const emitter = new LiteEmit<Events>();

function fooListener1(str: string) {
	console.log(str);
}

function fooListener2(str: string) {
	console.log(str);
}

function fooListener3(str: string) {
	console.log(str);
}

// Add listeners
// Chainable
emitter.on("foo", fooListener1);
emitter.on("foo", fooListener2);
emitter.on("foo", fooListener3).emit("foo", "hello");
emitter
	.on("baz", (num) => {
		console.log(num);
	})
	.emit("baz", "42");
// 42

// Remove a specified listener for a specified event
emitter.off("foo", fooListener1).emit("foo", "hello");
// Output:
// hello
// hello

// Remove all listeners for a specified event
emitter.off("foo");
// Output:
// <NONE>

emitter.emit("baz", 42);
// Output:
// 42

// Remove all wildcard listeners
emitter.off("*");

// Remove all listeners for all events
emitter.off();

License

MIT License © 2022 Ray

Package Sidebar

Install

npm i lite-emit

Weekly Downloads

228

Version

2.3.0

License

MIT

Unpacked Size

15.8 kB

Total Files

7

Last publish

Collaborators

  • so1ve