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

dead-simple

v1.1.0

Published

Dead simple PubSub and EventEmitter in JavaScript

Downloads

83

Readme

:skull::bulb: Dead simple PubSub and EventEmitter

Small, readable, almost-tweetable modules utilising classic patterns in modern language.

GitHub Workflow Status license git3moji code style: prettier Managed by Yarn

Features

  • Small: PubSub: 127 bytes, EventEmitter: 138 bytes, together: 191 bytes (all gzipped)
  • Best practices: Subscribe returns unsubscribe. So you can use anonymous functions.
  • Readable: There is nothing magical in the code; except the simplicity.
  • Modern: Uses new, yet well-supported features.
  • Efficient: No memory leaks, no duplicate calls, no extra looping.
  • Clean API: sub and pub (or on and emit), need no more, can't go less.
  • Modular: Use only what you need.
  • Typed TypeScript definitions are bundled

Install

Install using yarn or npm:

yarn add dead-simple
# or via npm
npm install --save dead-simple

Usage

import pubsub from "dead-simple/pubsub";
import eventEmitter from "dead-simple/eventEmitter";
// Alternatively:
// import { pubsub, eventEmitter } from 'dead-simple'

// === PubSub ======
const clicks = pubsub();

const unSub = clicks.sub((target) => console.log(`Clicked on ${target}!`));

clicks.pub("button");
// -> Clicked on button!

unSub();

clicks.pub("link");
// nothing

// === eventEmitter ===
// eventEmitter = named PubSub
const events = eventEmitter();

events.on("click", (target) => console.log(`Clicked on ${target}!`));

const unSubChange = events.on("change", (newValue) =>
  console.log(`Value is now ${newValue}!`)
);

events.emit("change", 1968);
// -> Value is now 1968!

unSubChange();

events.emit("change", 1968);
// nothing

events.emit("click", "button");
// -> Clicked on button!

Browser compatibility

Used ES6: const, arrow functions, Map, Set, object shorthand

| Chrome* | Edge | FF | IE | Opera | Safari | iOS | Node | | -------- | ---- | --- | --- | ----- | ------ | --- | ---- | | 38 | 12 | 13 | -* | 25 | 7.1 | 8 | 4 |

Notes:

  • Chrome includes mobile Chrome (Android 4+).
  • IE 11 does not support only syntax feature, arrow functions and object shorthand.
  • The module needs to be bundled, of course.

Super small versions

This project started a pair of gists which included a hand minified version, too.

We were able to get down to 91B for PubSub:

export default (s = new Set()) => ({
  pub: (d) => s.forEach((f) => f(d)),
  sub: (f) => s.add(f).delete.bind(s, f),
});

And 139B for EventEmitter (p is PubSub):

export default (e) => (
  (e = new Map()),
  Object.freeze({
    on: (n, f) => (e.has(name) || e.set(n, p()), e.get(n).sub(f)),
    emit: (n, d) => e.has(n) && e.get(n).pub(d),
  })
);

These versions are for fun, more like a proof of concept and may not work in some browsers.