npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mando75/luister

v1.2.1

Published

A simple event emitter library written in typescript

Downloads

3

Readme

Luister

npm version coverage report pipeline status

A simple event emitter library written in Typescript

Installation

yarn add @mando75/luister
# OR
npm install @mando75/luister

Source Code

Usage

Checkout the documentation for a full API reference.

You can create an event bus by invoking the named export Luister. The return value of this function is an object with emit, subscribe, and unsubscribe methods as properties.

Use subscribe to listen to a particular event. It will be called and receive a payload when the event is emitted. It also returns a function that you can invoke to unsubscribe the consumer from the event for easy cleanup.

Internally, the consumers for a given event are stored in a Set. This means that if the same consumer is registered multiple times, it will only be called once when the event is emitted.

Use emit to trigger an event and call any subscribers with a given payload.

Use unsubscribe to remove a consumer from a given event.

import {Luister} from "@mando75/luister";

// Define an interface mapping your event keys to their payload types
// You can use either symbols or strings as event keys
// It is highly recommended to provide an event mapping so that your
// consumers and emits remain typesafe. If you do not provide an event mapping,
// all payloads will have type of unknown.
const fooEvent = Symbol("fooEvent");

interface MyEvents {
  [fooEvent]: { message: string };
  barEvent: string;
}

// Create a new bus
const luister = Luister<MyEvents>();

// To subscribe to an event, provide the event key
// and a consumer to process the event data

// inline consumer
const unsubFromFoo = luister.subscribe(fooEvent, (payload) => { /* handle payload */
});

// named consumer
const barConsumer = (message: string) => { /* print message */
};
luister.subscribe("barEvent", barConsumer);

// Emit a new event. Emitting the events will call the consumers
// above with the provided payloads
luister.emit(fooEvent, {message: "Hello foo!"});
luister.emit("barEvent", "Hello bar!");

// We can unsubcribe from an event by using the helper returned by subscribe
unsubFromFoo();
// Or we can pass the event key and a named consumer to the unsubscribe method
luister.unsubscribe("barEvent", barConsumer);

Features

  • [x] Each event can trigger multiple subscribers.

  • [x] Unsubscribe from events

  • [x] Subscribe to multiple events with the same consumer

Roadmap

  • [ ] Subscribe to all events

Contributing

Development takes place on the GitLab Repository. The GitHub Repository (Mirror) is just a mirror for discoverability. Issues opened in GitHub will be addressed, but any development contributions need to happen on GitLab. Any PRs opened in GitHub will be closed with a message to linking to the GitLab repository.

Code of Conduct

Treat others how you would like to be treated.

Trolling, harassment, or discrimination will not be tolerated.