@sgty/m-it
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i @sgty/m-it

Weekly Downloads

3

Version

1.0.2

License

ISC

Unpacked Size

102 kB

Total Files

20

Last publish

Collaborators

  • leddit