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

@lemoncode/publisher-subscriber-provider

v0.0.1

Published

This package is an implementation of the publisher-subscriber pattern, using react hooks and context. It provides the necessary tools to publish events and subscribe to them. Control that no re-renders are performed on components that are not affected by

Downloads

1

Readme

publisher-subscriber-provider

This package is an implementation of the publisher-subscriber pattern, using react hooks and context. It provides the necessary tools to publish events and subscribe to them. Control that no re-renders are performed on components that are not affected by published events.

Features

  • Publish-subscriber pattern.
  • Persistence of subscriptions in context.
  • Do not re-render on components not affected by the publication of an event.
  • Ability to automatically unsubscribe via useSubscribe hook.
  • Javascript & Typescript.

Usage

You can check the examples folder.

Add the provider

First, you need to add the Publisher Subscriber Provider by wrapping your components that need to publish / subscribe to events:

import React from "react";
import { PublisherSubscriberProvider } from "publisher-subscriber-provider";
import { Content } from "./components/content.component";
import { Header } from "./components/header.component";
import { Other } from "./components/other.component";

export const App = () => {
  return (
    <PublisherSubscriberProvider>
      <Header />
      <Content />
      <Other />
    </PublisherSubscriberProvider>
  );
};

Now you can publish events from any child component:

import React from "react";
import { usePublisherSubscriber } from "publisher-subscriber-provider";

export const Header = () => {
  const [value, setValue] = React.useState("");

  const { publish } = usePublisherSubscriber();

  React.useEffect(() => {
    publish("onChangeText", value);
  }, [value]);

  return (
    <div>
      <span>Header</span>
      <input type="text" value={value} onChange={e => setValue(e.target.value)} />
    </div>
  );
};

And subscribe/unsubscribe to those events from other components:

export const Content = () => {
  const [text, setText] = React.useState("");
  const { subscribe } = usePublisherSubscriber();

  React.useEffect(() => {
    const unsubscribe = subscribe("onChangeText", setText);

    // Return cleanup function
    return () => unsubscribe();
  }, []);

  return (
    <div>
      <h3>Content</h3>
      <h5>The value has changed: {text}</h5>
    </div>
  );
};

You can auto-unsubscribe events using useSubscribe hook:

export const ContentAutoUnsubscribe = () => {
  const [text, setText] = React.useState("");
  const { useSubscribe } = usePublisherSubscriber();

  useSubscribe("onChangeText", setText);

  return (
    <div>
      <h3>Content</h3>
      <h5>The value has changed: {text}</h5>
    </div>
  );
};

Installing

Using npm:

$ npm install publisher-subscriber-provider

Using yarn:

$ yarn install publisher-subscriber-provider

Peer dependencies

publisher-subscriber-provider needs the following peer dependencies:

"react": ">=16.8.6",
"react-dom": ">=16.8.6"

Typescript

publisher-subscriber-provider includes TypeScript definitions.

Example

You can check the examples folder.

Credits

Link to author github