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

pusu

v1.2.2

Published

Simple Publish-Subscribe implementation APIs for Javascript Apps

Downloads

122

Readme

pusu

Simple type-safe pub-sub implementation APIs for Javascript/TypeScript Apps

Create Publication

type TCreateConfiguration = {
  name?: string;
  enableLogging?: string;
};

type TPublication<T> = {
  config?: TCreateConfiguration,
  subscribers: ((data: T) => void)[],
}

declare const createPublication: <T>(config?: TCreateConfiguration | undefined) => TPublication<T>;

Parameters:

  • config: (Optional) TCreateConfiguration - Configuration options.

Return value: Object - New publication.

Creates & returns a unique new publication object.

Configuration

  • name?: (Optional) string - Used in logging. Default: "Unknown".
  • enableLogging?: (Optional) boolean - Enable/disable console logging. Useful in development and/or test environments. If enabled then each action "create" | "publish" | "subscribe" | "unsubscribe" | "notify" gets logged on console with relevent data.

Example

import { createPublication } from 'pusu';

export default createPublication<{ asOfDate: Date }>({ name: 'load-data' });

Unique publication every time

Creation of a publication makes sure that each publication is unique in itself and removes the need of maintaining a unique key for each publication.

Even if multiple publications created with same name, then each publication will be treated as a separate publication without any conflicts.

Below code creates two separate unique publications publication1 & publication2 even though the publication names are same. Name is just for the sake of naming the publication so that its useful during debugging any issues.

Example

import { createPublication } from 'pusu';

const publication1 = createPublication<{ asOfDate: Date }>({ name: 'load-data' });
const publication2 = createPublication<{ asOfDate: Date }>({ name: 'load-data' });

console.log(publication1 === publication2); //false

Publish

declare const publish: <T>(publication: TPublication<T>, data: T) => void;

Parameters:

  • publication: (Required) Object - Publication object created using the api createPublication()
  • data: (Optional) - This argument is passed to the subscribers listening to the publication. Its a way of passing data to the subscribers.

publish method calls all the subscribers subscribed to the publication (provided as a first argument). It calls the subscribers with the data.

Example

import { publish } from 'pusu';
import loadDataPublication from './publications/load-data-publication';

<button
  id="headerRefreshAction"
  onClick={() => {
    // Publish the data
    publish(loadDataPublication, { asOfDate: new Date() });
  }}
>
  Refresh
</button>

Subscribe

declare const subscribe: <T>(publication: TPublication<T>, subscriber: TSubscriber<T>) => (() => void);

Parameters:

  • publication: (Required) Object - Publication object created using the api createPublication
  • subscriber: (Required) Function - A subscriber function which will be called by the publisher. This function will receive the data published by the publisher.

Return value: Function - A function to unsubscribe, when called then the subscriber is unsubscribed and no longer called by the publisher.

Example

import { subscribe } from 'pusu';
import loadDataPublication from './publications/load-data-publication';

let unsubscribe;

// Subscribe to the publication
unsubscribe = subscribe(loadDataPublication, ({ asOfDate }) => {
  // load the data from API
});

// Unsubscribe from the publication before removal of the component
if (unsubscribe) {
  unsubscribe();
}

Logging

If logging is enabled while creating the pubcalition then each action gets logged.

Log Information

type TLog<T> = {
  publication: string;
  action: TLogAction;
  data?: T;
  meta?: any;
};
  • publication: Name of the publication. Default = "Unknown".
  • action: "create" | "publish" | "subscribe" | "unsubscribe" | "notify".
  • data: Data published and sent to subscribers.
  • meta: Can be any metadata. E.g. when a subscriber is notified then the sibscriber function's name is added in the metadata.

Actions

  • create: When publication is created.
  • publish: When publciation is published with data.
  • subscribe: When a subscriber function is subscribed.
  • unsubscribe: When a subscriber function is unsubscribed.
  • notify: When a subscriber function is called with the data.

Example

createPublication<{ asOfDate: Date }>({ name: 'load-data', enableLogging: process.env.NODE_ENV === 'development' });

Log

'pusu', { publication: 'load-data', action: 'create' }
'pusu', { publication: 'load-data', action: 'subscribe', meta: { subscriber: '[functionName]' } }
'pusu', { publication: 'load-data', action: 'publish', data: { asOfDate: 1706513208749 } }
'pusu', { publication: 'load-data', action: 'notify', data: { asOfDate: 1706513208749 }, meta: { subscriber: '[functionName]' } }

License

MIT