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

use-pubsub-js

v1.0.7

Published

A service and hooks for React to publish or subscribe

Downloads

2,347

Readme

use-pubsub-js

A service and hooks for React to publish or subscribe (wrapper of pubsub-js)

Table of Contents

Install

npm i use-pubsub-js
yarn add use-pubsub-js

Usage

You can import the hooks or a service to use where you want

import { PubSub, usePublish, useSubscribe } from 'use-pubsub-js'

useSubscribe

import { PubSub, useSubscribe } from 'use-pubsub-js'

setTimeout(() => PubSub.publish('token', 'message'), 5000)

const ExampleUseSubscribe = () => {
  const handler = (token, message) => {
    console.log(`Message ${message} - Token ${token}`)
  }

  const { unsubscribe, resubscribe } = useSubscribe({ token: 'token', handler })

  return (
    <div>
      <button type="button" onClick={unsubscribe}>
        Unsubscribe
      </button>
      <button type="button" onClick={resubscribe}>
        Resubscribe
      </button>
    </div>
  )
}

The useSubscribe is a hook to listen publications that are made using the same token in publish and subscription. The hook returns two functions, one to unsubscribe the token off your handler and one to resubscribe your function to token.

You can only invoke the hook and dynamically unsubscribe and subscribe pass the isUnsubscribe prop to hook.

usePublish

import { PubSub, usePublish } from 'use-pubsub-js'

const handler = (token, message) => {
  console.log(`Message ${message} - Token ${token}`)
}

PubSub.subscribe('token_two', handler)

const ExampleUsePublish = () => {
  const { publish } = usePublish({ token: 'token_two', message: 'message' })

  return (
    <div>
      <button type="button" onClick={publish}>
        Publish
      </button>
    </div>
  )
}

The usePublish hook have more than one way to use, the above is a simple wrapper to declare your publish function using a React approach with hooks.

import { PubSub, usePublish } from 'use-pubsub-js'

const handler = (token, message) => {
  console.log(`Message ${message} - Token ${token}`)
}

PubSub.subscribe('token_three', handler)

const ExampleUsePublish = () => {
  const { lastPublish } = usePublish({
    token: 'token_three',
    message: 'message',
    isAutomatic: true,
  })

  return (
    <div>
      <p>{lastPublish ? Publishing success : Publication failure}</p>
    </div>
  )
}

The other way to use usePublish is with automatic publishing, always message change is called a new publish with a new message, by default have a debounce with 300ms, you can increase, decrease ou run immediately pass a specific prop.

The returned lastPublish value is true if have some subscribe to receive a message and false if they don't referring to the last publication.

To see more information for PubSub service check the official documentation

Examples

Checkout the simple examples on Example folder

or

More real examples:

Edit use-pubsub-js

API Documentation

useSubscribe

  • Arguments of useSubscribe

| key | description | type | default/required | | ------------- | -------------------------------------------------------------------------- | ----------------------------------------------- | ---------------- | | token | Token is used to subscribe listen a specific publisher | string | Symbol | required | | handler | Function that is going to be executed when a publication occurs | (token: string | Symbol, message: any) => void | required | | isUnsubscribe | Is the way to dynamically unsubscribe and subscribe based on some variable | boolean | false |

  • Returns of useSubscribe

| key | description | type | | ----------- | ------------------------------------------------------------------------------------------------------------------- | ---------- | | unsubscribe | A function to manual unsubscribe the token off your handler | () => void | | resubscribe | A function to manual resubscribe the token in your handler, only have effects if the handler is not linked in token | () => void |

usePublish

  • Arguments of usePublish

| key | description | type | default/required | | ---------------- | ----------------------------------------------------------------------- | ---------------- | ---------------- | | token | Token is used to subscribe listen a specific publisher | string | Symbol | required | | message | The value that will be send to subscriber | any | required | | isAutomatic | Whether the publication should be automatic | boolean | false | | isInitialPublish | Whether to make a publication in the first render | boolean | false | | isImmediate | To disable debounce and publish without delay any change in the message | boolean | false | | debounceMs | The delay value | number | string | 300 |

  • Returns of usePublish

| key | description | type | | ----------- | ------------------------------------------------------------------------------------- | ---------- | | lastPublish | The value is true if you have a subscriber on last publication and false if you don't | boolean | | publish | A function to manual publish a message | () => void |

License

MIT © Reactivando


This hook is created using create-react-hook.