Coder Social home page Coder Social logo

lil-emit's Introduction

lil-emit

a typed 253 byte event emitter

Yes, another tiny event emitter library.

Install

npm  install --save  lil-emit    // npm
pnpm install --save  lil-emit    // pnpm
yarn add             lil-emit    // yarn

And then use!

import { emitter } from "lil-emit";

interface MyEvents {
	click: [number, number];
	update: Record<string, unknown>;
}

// Create our event handler
const ee = emitter<MyEvents>();

const handler = (data) => console.log(data);

// Listen for our events to be fired
// (It's best to avoid using lambda functions for the callback)
ee.on("click", handler);

ee.on("update", handler);

// Dispatch our 'click' event
ee.dispatch("click", [32, 425]);

// Stop listening to the 'click' event
ee.off("click", handler);
// ...or all events
ee.off("*");

That's all that's needed!

Guide

The following guide will reference this event handler function at different points:

function handler(a, b, c) {
	console.log(a, b, c);
}

.on(name, callback)

Registers an event handler for a given name.

Callback

The callback has the type (...data) => void, where (...data) is the data being emitted. For example:

ee.on("test", handler);
// or the leaky way
ee.on("test", (a, b, c) => console.log(a, b, c));

.dispatch(name, ...data)

Invokes all listeners for the given event name.

To demonstrate, here's how you would dispatch to the example above:

ee.dispatch("test", "One", "Two", "Three!");

.off(name, callback)

Unregisters an event handler from an event.

// This won't work.
ee.off("test", (a, b, c) => console.log(a, b, c));

// But this will!
ee.off("test", handler);

You can also do .off("*") to remove all event listeners, from all events.

lil-emit's People

Contributors

snuffydev avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.