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

@catsums/signal-event-bus

v1.0.2

Published

A package for adding signal events to your front end code, which can be used in your components or vanilla JS code as a signal handler.

Downloads

6

Readme

SignalEventBus

A package for adding signal events to your front end code, which can be used in your components or vanilla JS code as a signal handler.

It contains a class called SignalEventBus which can be used to subscribe functions to it as handlers and emits data to them if they are listening to the specified event.

It also contains a React hook called useSignalEvent() and a component called <SignalEventListener/> which can be placed into components to listen to events and handle them. For these components, the events can be listened to from other components and other client based files.

If you have a server component with a SignalEventBus trying to subscribe a function from a client component, this will not work unless you import one component into the other.

How to use

Here's how to create a signalbus and use it in a normal function:


//// signal.ts
//create signalbus and export it
export let trigger = new SignalEventBus();

function onTrigger(){
  console.log("triggered signal");
}
function onShoutEvent(count){
  console.log(`SHOUT! (count: ${count})`);
}

//subscribe listener to trigger (which is a nameless event)
trigger.on(onTrigger);

//subscribe listener to event called "shout"
trigger.on("shout", onShoutEvent);

//// main.ts

//import the trigger signalbus from te file it was created
import { trigger } from "./signal"

let shoutCount = 0;
setInterval(()=>{
  //emit nameless event
  trigger.emit();
  
  shoutCount++;
  if(shoutCount > 10){
   //emit "shout" event with a passed parameter
   trigger.emit("shout", shoutCount);
  }
}, 1000)

Here's how to use it in a React component, where you can trigger the signal from the child component to close the parent component:

import {useState} from "react"
import { SignalEventBus, useSignalEvent } from "@catsums/signal-event-bus"

let closeSignal = new SignalEventBus();

function ChildComponent(){
 function onClick(){
  closeSignal.emit("close");
 }
  return (
   <button onClick={onClick}>Click to close</button>
  )
}

function ParentComponent(){
  let [open, setOpen] = useState(true);

  function onCloseEvent(){
   setOpen(false);
  }

//this allows button in child component to trigger the event to close parent!
  useSignalEvent("close", onCloseEvent, closeSignal);
//you could also do this instead:
 useEffect(()=>{
  closeSignal.on("close", onCloseEvent);
  return () => {
   closeSignal.off("close", onCloseEvent);
  }
 })

 return (
   <div>
    <div>Alert! You have a notification</div>
    <ChildComponent/>
   </div>
  )
}