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

@mfe-utils/pubsubmfe

v1.0.1

Published

PubSubMFE (Publish Subscribe Micro Frontend) is a flexible and efficient publish-subscribe pattern implementation in JavaScript. It is useful for decoupling components in a Micro Frontend Architecture and other systems where loose coupling between compone

Downloads

4

Readme

PubSubMFE

PubSubMFE (Publish Subscribe Micro Frontend) is a flexible and efficient publish-subscribe pattern implementation in JavaScript. It is useful for decoupling components in a Micro Frontend Architecture and other systems where loose coupling between components is desired.

Installation

npm i @mfe-utils/pubsubmfe

Usage

First, import PubSubMFE:

import { PubSubMFE } from '@mfe-utils/pubsubmfe';

Create a new instance of PubSubMFE. You can use the default options, or provide an options object:

const pubSub = new PubSubMFE();

// or with options
const pubSubWithOptions = new PubSubMFE({
  storeMap: new Map(),
  ObservableConfig: { /* DataObservable configuration here */ }
}); 

The optional parameters for the PubSubMFE object are:

  • storeMap: A Map object to use as the store instead of the build in static class.

  • ObservableConfig: An object containing configuration options for the DataObservables created by the instance. The actual options depend on the DataObservable implementation. See the notes on the ObservableConfig below

Basic Publish and Subscribe

A basic publish and subscribe flow looks like this:

// create a subscriber
pubSub.get('channel1').subscribe((data) => console.log('Received:', data));

// publish some data
pubSub.get('channel1').publish('Hello, world!');`

Filtering

You can provide a filter function as part of the options object when subscribing. The filter function is called with each new value that is published. If the function returns true, the value is passed to the subscriber function. If the function returns false, the value is not passed to the subscriber function.

pubSub.get('user-age').subscribe(
  (data) => console.log('Received:', data),
  {
    filter: (data) => data.age < 30,
  }
);

pubSub.get('user-age').publish({ name: 'Alice', age: 25 }); // logs: Received: { name: 'Alice', age: 25 }
pubSub.get('user-age').publish({ name: 'Bob', age: 35 }); // does not log anything 

Pausing and Resuming Subscriptions

You can pause and resume subscriptions:

const subscription = pubSub.get('channel1').subscribe((data) => console.log('Received:', data));

// pause the subscription
subscription.pause();

// publish some data
pubSub.get('channel1').publish('Hello, world!'); // nothing is logged

// resume the subscription
subscription.resume();

// publish some more data
pubSub.get('channel1').publish('Hello again, world!'); // logs: Received: Hello again, world! 

Observable Config

When creating an instance of PubSubMFE, you can provide replayPolicy and bufferSize as part of the ObservableConfig parameter. This will affect the behavior of the DataObservables that the instance creates.

The replayPolicy determines how many of the most recent published values to keep and send to new subscribers. The bufferSize determines the size of the buffer that holds the published values.

Here is an example of how to set these options:

javascript

const pubSub = new PubSubMFE({
  ObservableConfig: {
    replayPolicy: 'all', // 'all' or 'last'
    bufferSize: 100, // any positive integer
  }
}); 

In the example above, the replayPolicy is set to 'all', which means that all published values are kept (up to the bufferSize). When a new subscriber subscribes, they will receive all of these values immediately.

The bufferSize is set to 100, which means that the buffer can hold up to 100 values. If more than 100 values are published, the oldest values will be discarded to make room for the new values.

Now, when you subscribe and publish data, the settings will be respected:

// create a subscriber
pubSub.get('channel1').subscribe((data) => console.log('Received:', data));

// publish more than 100 values
for (let i = 0; i < 200; i++) {
  pubSub.get('channel1').publish(i);
}

// create a new subscriber
pubSub.get('channel1').subscribe((data) => console.log('Received by new subscriber:', data));

// the new subscriber receives the 100 most recent values (100-199) 

To set the replayPolicy to 'last', change the ObservableConfig as follows:

const pubSub = new PubSubMFE({
  ObservableConfig: {
    replayPolicy: 'last', // 'all' or 'last'
    bufferSize: 100, // any positive integer
  }
});

Now, when a new subscriber subscribes, they will only receive the most recent value, even if more values have been published.

Contributing

Contributions are welcome! Please submit a pull request or create an issue if you have any improvements or find any bugs.

License

PubSubMFE is licensed under the BSD-3-Clause license. See the LICENSE file for more information.