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

pub-sub-es6

v1.5.1

Published

simple publish and subscribe library for comunicate js objects, react componetes, DOM events.

Downloads

51

Readme

PubSub es6

Simple lib for subscribe and dispatch actions.

In our application we perform many more complex actions, and depending on our project structure sometimes it is difficult to handle all the events that happen. We can simply think of giving a name to that complex action and send any data we need.

You can send your subscribers many arguments and of any kind; anyone who has subscribed to this event can execute an action

install

npm install pub-sub-es6 --save

React

When you need to comunicate componentes maybe you find many difficulties. You can resolve it sending a message to the other component.

The function onAddItem will be called when dispatched the action "ADD_ITEM".

import {dispatch, receive, on}  from 'pub-sub-es6'
//ShoppingCard.jsx
class ShoppingCard extends React.Component {

  state = { items: [] }

  @on("ADD_ITEM")
  onAddIem(item, language){
    this.setState({ items: [item, ...this.state.items] })
  }

  render(){
    //....
  }

}

//Item.jsx
class Item extends React.Component {

  onClickHandler(item){
    const { item, language } = this.props
    dispatch("ADD_ITEM", item, language)
  }

  render(){
    //....
  }

}

dispatch

  dispatch("USER.CLICK_IN_AD", adData)

receive

  receive("OPEN_SIGN_IN", (type) => type == 'modal' ? openModal() : redirectToSignIn() )

unsubscribe

  var fnc = (data) => {}
  receive("MESSAGE", fnc, "custom-uid")
  unsubscribe("MESSAGE", "custom-uid") // anyone can unsubscribe
  // or
  var fnc = () => {}
  const uid = receive("MESSAGE", fnc)
  unsubscribe("MESSAGE", uid)

React comunication with plain javascript

//ShoppingCard.jsx
class ShoppingCard extends React.Component {

  state = { items: [] }

  @on("ADD_ITEM")
  onAddIem(item, language){
    this.setState({ items: [item, ...this.state.items] })
  }

}
// item.html
<li class="js-item-action-add" data-item='{"name":"My Item"}'>My Item</li>
//my_controller.js
$(".js-item-action-add").on("click", function(event) {
  PubSubEs6.dispatch("ADD_ITEM", $(this).data("item"))
})

Actions named

  • The actions names it's a global name, it's recomended create a file with the actions names to avoid duplicate a action name.
//site_actions.js
const actionsSite = {
  item:{
    add: "ADD_ITEM",
  }
}

//Item.jsx
import action from './site_actions'

class Item extends React.Component {

  onClickHandler(item){
    const {item, language} = this.props
    dispatch(action.item.add, item, language)
  }

  render(){
    //....
  }

}

Devtools

Devtools it's a simple console logger for trace your actions.

allActions

  PubSubEs6.allActions()
  //[ { actionName, subscriptions: [ {fnc, uid} ] ]

status

  PubSubEs6.status()
  //All subscribers for the Action (ADD_ITEM) = `, `[ ShoppingCard -> onAddIem ]

findSubscriptions


  PubSubEs6.findSubscriptions("MESSAGE")
  // [ fnc, "uid-token" ]

logger

If your module bundle set process.env.NODE_ENV == 'production' this logger will be off

The default options it's:

  config = {
    trace: {
      dispatch: true,
      receive: false,
      unsubscribe: false,
      not_found_subscriber: true,
    },
  }

You can disable the logger for a specific action.

import PubSubEs6 from 'pub-sub-es6'
PubSubEs6.config.trace.dispatch = { exept: ["MY_LOOP_ACTION"] }

Decorator configuration

The decorator @on only works with react components

this decorator only subscribe in the componentDidMount and unsubscribe in the componentWillUnmount functions

to enable decorators see decorators