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

create-pubsub

v1.6.3

Published

A tiny Event Emitter and Observable Store.

Downloads

767

Readme

Create PubSub

NPM Version Size Known Vulnerabilities Types License

A tiny Event Emitter and Observable Store for JavaScript apps.

Supported environments: Browser, Node and Deno.

It's a Vanilla JavaScript library, so it's framework-agnostic. But if you're using React, check out the built-in support for it in the examples. And if you're planning to store immutable data, check also the built-in support for Immer.

Install

npm install create-pubsub

Import

// Import as an ES Module.
import { createPubSub } from "create-pubsub";
// Or require as a CommonJS Module.
const { createPubSub } = require("create-pubsub");
// Or import it from URL.
import { createPubSub } from "https://esm.sh/create-pubsub";
<!-- Or use it directly in the browser. -->
<script src="https://unpkg.com/create-pubsub"></script>
<script>
  const { createPubSub } = window["create-pubsub"];
</script>

Usage

As Emitter/Listener

For each event you want to track, create a new PubSub.

const [pub, sub] = createPubSub<Type>();

As Store

To store a value, pass an initial value and set a third element while destructuring it.

const [pub, sub, get] = createPubSub(initialValue);

Examples

Example: Getting Started

const [pub, sub] = createPubSub<string>();

sub((data) => console.log(`Hello ${data}!`));

pub("World"); // Prints "Hello World!".

Example: Naming Functions

Name the 'pub' and 'sub' functions as you wish. The idea is to avoid relying on strings representing the events names. The following snippet shows different ways to name an event which indicates the game started:

const [publishGameStarted, subscribeToGameStarted] = createPubSub();

const [gameStarted, onGameStarted] = createPubSub();

const [dispatchGameStarted, listenGameStartedEvent] = createPubSub();

Example: Signalling

You can also publish events with no data, just for signalling:

const [emitPageIsReady, whenPageIsReady] = createPubSub();

whenPageIsReady(() => {
  // Do something with the page, which is now ready.
});

emitPageIsReady();

Example: Unsubscribing

And you can unsubscribe at any moment, by invoking the function returned when you subscribe:

const [publish, subscribe] = createPubSub();

const unsubscribe = subscribe((numberReceived) => {
  console.log(numberReceived);

  if (numberReceived === 2) unsubscribe();
});

publish(1); // Prints 1.
publish(2); // Prints 2 and unsubscribe.
publish(3); // Nothing is printed.

Example: Chaining Events

const [emitAssetsLoaded, onAssetsLoaded] = createPubSub();
const [emitGameStarted, onGameStarted] = createPubSub();

onGameStarted(() => {
  // Setup world, characters, etc. And possibly chain more events.
});

onAssetsLoaded(() => {
  // Initializes the game, load last saved session, etc.
  emitGameStarted();
});

emitAssetsLoaded();

Example: Storing Data

const [set, sub, get] = createPubSub("red");

console.log(get()); // Prints "red".

set("blue"); // Sets the store to "blue", but nothing is printed.

sub((state) => console.log(state)); // Subscribe to next store updates.

set("green"); // Sets the store to "green" and prints it.

console.log(get()); // Prints "green".

Example: Action & Reaction

You also receive the value to the previous value stored there, so you can check if the value has changed or not since last time it was set, for example:

const [updatePlayer, onPlayerChanged, getPlayer] = createPubSub({
  name: "Player1",
  level: 5,
  hp: 33,
  mana: 92,
});

onPlayerChanged((playerState, previousPlayerState) => {
  if (playerState.level > previousPlayerState.level) {
    // Player leveled up! Let's display the level-up dialog.
  }
});

updatePlayer({ ...getPlayer(), level: 6, hp: 40, mana: 100 });

Example: State Management

Using it as a store and reacting to other events:

const [setValue, watchValue, getValue] = createPubSub(0);

const [dispatchIncremented, onIncremented] = createPubSub();

const [dispatchDecremented, onDecremented] = createPubSub();

onIncremented(() => setValue(getValue() + 1));

onDecremented(() => setValue(getValue() - 1));

watchValue((state) => console.log(state));

dispatchIncremented(); // Prints 1.
dispatchIncremented(); // Prints 2.
dispatchDecremented(); // Prints 1.

Example: Working with Immutable Data

For creating a PubSub instance that makes use of Immer, import the createImmerPubSub function from create-pubsub/immer and the Publish function will then provide the draft which you can mutate to generate the new state.

import { createImmerPubSub } from "create-pubsub/immer";

const [updateColorsList, onColorsListUpdated, getColorsList] =
  createImmerPubSub([
    { name: "White", code: { r: 255, g: 255, b: 255 } },
    { name: "Gray", code: { r: 128, g: 128, b: 128 } },
  ]);

onColorsListUpdated((currentColorsList, previousColorsList) => {
  console.log(currentColorsList);
  // Prints:
  // [
  //   { name: "White", code: { r: 255, g: 255, b: 255 } },
  //   { name: "Green", code: { r: 0, g: 128, b: 0 } },
  // ]

  console.log(previousColorsList);
  // Prints:
  // [
  //   { name: "White", code: { r: 255, g: 255, b: 255 } },
  //   { name: "Gray", code: { r: 128, g: 128, b: 128 } },
  // ]
});

updateColorsList((colorsList) => {
  const color = colorsList.find((color) => color.name === "Gray");
  if (color) {
    color.name = "Green";
    color.code.r = 0;
    color.code.b = 0;
  }
});

console.log(getColorsList());
// Prints:
// [
//   { name: "White", code: { r: 255, g: 255, b: 255 } },
//   { name: "Green", code: { r: 0, g: 128, b: 0 } },
// ]

Example: React Hook

For linking a PubSub instance with a React element, import the usePubSub hook from create-pubsub/react and use it inside the component, similar to React's useState.

import { createPubSub } from "create-pubsub";
import { usePubSub } from "create-pubsub/react";

const counterPubSub = createPubSub(0);

const ReactButton = () => {
  const [count, setCount] = usePubSub(counterPubSub);

  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
};