1.0.2 • Published 2 years ago

@sgty/m-it v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

m-it

m-it logo

A Minimal javascript event emitter, simple with no bloat

Motivation: sometimes you just gotta emit an event, without a complex named event system, and without it costing you 40kB

Installation

npm:

npm install @sgty/m-it

yarn:

yarn add @sgty/m-it

pnpm:

pnpm add @sgty/m-it

Usage

usage is very simple, and meant to be used for very simple events. there are no event names, the semantics of the event are tied to its variable name.

import EventEmitter from '@sgty/m-it';
// also accessible via destructure (complies better with vscode autocomplete)
import { EventEmitter } from '@sgty/m-it';

const obj = { onClick: new EventEmitter() };

obj.onClick.sub(() => {
	/*...*/
});

obj.onClick.emit();

Subscribing:

obj.onClick.subscribe(() => {
	/*...*/
});
// or the alias
obj.onClick.sub(() => {
	/*...*/
});

Unsubscribing:

obj.onClick.unsubscribe(handler);
// or the alias
obj.onClick.unsub(handler);

// unsubscribe all
obj.onClick.clear();

you can also utilize the cleanup function returned via the subscription functions

const cleanup = obj.onClick.sub(() => {
	/*...*/
});

// unsubscribe
cleanup();