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

@beardedtim/pubsub

v0.3.0

Published

> Us suffer bad. Want justice. We want Thunderdome!

Downloads

11

Readme

PubSub

Us suffer bad. Want justice. We want Thunderdome!

Build Status codecov

API

PubSub

Demo Usage:

const { PubSub } = require('@beardedtim/pubsub);

// emit is how we can observe the result of some action
// in a uniformed way throughout the system.
//
// Think of `emit` as the `resolve` of a promise,
// the callback of a, well, callback function
//
// This is how we emit changes inside of the PubSub
// system into the outer scope
const emit = (...args) => {
  console.log(...args);
}

// Each PubSub instance MUST have an emit 
// function passed to its constructor.
const pubsub = new PubSub(emit);

// We want to subscribe to some event in the future
// called 'EVENT_NAME', with default scope applied,
// and when that even happens, I want to emit whatever
// was passed with that event.
pubsub.subscribe('EVENT_NAME', ({ emit }, ...data) => emit(...data));

// At some point in the future we
// publish that event into our system
// and eventually our `emit` function
// gets called.
pubsub.publish('EVENT_NAME', 1, 2, 3);
// 1, 2, 3

API:

  • constructor: (emit: fn [, id: str, scoped: bool]) => void

    • Emit: Function to emit from the system
    • ID: The ID of this instance. Falls back to a guid
    • Scoped: If we are going to scope our event names
  • getEventName: (event: str, scoped: bool) => str

    • Returns an EventName based on scoped
    • Example:
        const unScopedName = pubsub.getEventName('event', false) // 'event'
        const scopedName = pubsub.getEventName('event', true) // '${pubsub._id}:event'
  • subscribe: (event: str, fn: function, scoped: bool) => fn

    • Adds an event listener to passed event

      • Example:
        const handleInput = ({ emit, publish, subscribe }, input) => {
          const massagedInput = sanitizeInput(input);
          publish('SANATIZED_INPUT', massagedInput);
        }
      
        const respondToUser = ({ emit }, payload) =>
          emit({
            type: 'RETURN_TO_USER',
            payload
          });
      
        const handleSanatizedInput = ({ emit }, input) =>
          emit({
            type: 'UPDATE_FORM',
            payload: input,
          });
            
        pubsub.subscribe('USER_NAME_UPDATE', handleInput);
        pubsub.subscribe('SANATIZED_INPUT', handleSanatizedInput);
  • publish: (event: str [, ...args: [*]]) => void

    • Publishes an event to the system
    • Example:
      pubsub.subscribe('GET_DB', handleDB);
        
      const asyncPublish = action => payload =>
        pubsub.publish(action, payload);
        
      findDB()
        .then(asyncPublish('GET_DB'));
  • __id: str

    • The ID of the instance
  • __emit: fn

    • The emit function passed to the constructor
  • __isScoped: bool

    • The scoped value passed to the constructor

GUID

This is a straight copy/paste from [stackOverflow](// https://stackoverflow.com/a/105074/3902127)

  // guid: () -> String
  guid() // some random string

Running Local Example

$ git clone [repo]

$ cd [repo]

$ yarn

$ yarn example

This will start a WebSocket server at ./.env's SOCKET_PORT= value or default to port 5001. You can send it the message { "type": "GET" } and get a response eventually. I use wscat for testing.

You may also find a basic example here