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

@entando/mfecommunication

v1.0.12

Published

mfe communications manager used in entando ecosystem

Downloads

21

Readme

Entando Microfrontend Communication Library

A powerful and lightweight JavaScript library that facilitates seamless communication between microfrontends through a centralized event-based communication mechanism.

npm version

Table of Contents

Installation

Install @entando/mfecommunication via npm:

npm install @entando/mfecommunication --save

Usage

The @entando/mfecommunication mediator is a singleton and should be used to facilitate communication between different microfrontends on the same page. Always remember to unsubscribe when you're done to avoid any memory leaks. @entando/mfecommunication mediator can be used in all JS libraries, frameworks and environments.

There are 3 different ways to use the @entando/mfecommunication mediator functionality.

  1. Here is an example of how to use the mediator by importing the instance from the library:
import { mediatorInstance } from '@entando/mfecommunication';
  1. entando-mfecommunication library exposes the mediatorInstance to the window object. As soon as you import the @entando/mfecommunication build file into html, then window.entando.globals.mediator will be available for any JS code. Here is an example:
    <!-- Include the bundled script -->
    <script src="https://unpkg.com/@entando/mfecommunication/dist/entando-mfecommunication.umd.cjs"></script>

    <script>
        // The library is available as a global variable
        var mediator = window.entando.globals.mediator;

        // Example usage:
        mediator.subscribe("eventName", {
            callerId: "subscriberA",
            callback: (data) => console.log('Subscriber A received:', data),
        });

        mediator.publish("eventName", {message: "Hello World!"});
    </script>
  1. Since using singleton pattern might not be suitable for all the usecases @entando/mfecommunication exports the Mediator class itself and the developer can create instance of it whenever and however they seem to be appropriate. This is how it can be used:
import { Mediator } from '@entando/mfecommunication';

const myMediatorInstance = new Mediator();

Once you have an access to the mediatorInstance here are simple examples how it can be used:

// Subscriber A
mediatorInstance.subscribe("eventName", {
  callerId: "subscriberA",
  callback: (data) => console.log('Subscriber A received:', data),
});

// Subscriber B
mediatorInstance.subscribe("eventName", {
  callerId: "subscriberB",
  callback: (data) => console.log('Subscriber B received:', data),
});

// Publisher
mediatorInstance.publish("eventName", {message: "Hello World!"});

API

Types

Callback

A type alias for a function that takes an optional data parameter of an unknown type and does not return anything.

type Callback = (data?: unknown) => void;

EventCallback

An interface for the callback object required for subscribing to an event. It contains a callerId of type string, and a callback of type Callback.

interface EventCallback {
  callerId: string;
  callback: Callback;
}

MediatorType

A type alias for the instance of the Mediator class.

type MediatorType = typeof mediatorInstance;

Methods

subscribe(eventType: string, eventCallback: EventCallback): void

Subscribes to a specific event. It requires an event type and an event callback object which includes a callerId and a callback function.

unsubscribe(eventType: string, callerId: string): void

Unsubscribes a specific subscriber from a specific event type.

unsubscribeAllEventsOfSubscriber(callerId: string): void

Unsubscribes a specific subscriber from all event types.

unsubscribeAllSubscribersOfEvent(eventType: string): void

Unsubscribes all subscribers from a specific event type.

unsubscribeAll(): void

Unsubscribes all subscribers from all event types.

publish(eventType: string, data?: unknown): void

Publishes an event of a specific type with optional data. All subscribers of this event type will receive the data.

publishToSubscribers(eventType: string, callerIds: string[], data?: unknown): void

Publishes an event of a specific type with optional data to a specific subscribers.

publishExceptToSubscribers(eventType: string, callerIds: string[], data?: unknown): void

Publishes an event of a specific type with optional data to all subscribers except for the specified ones.

listSubscribers(): Array<{eventType: string, subscriber: string}>

Lists all subscribers for all events.

listSubscribersForEvent(eventType: string): string[]

Lists all subscribers for a specific event.

listEvents(): string[]

Lists all event types.

listEventsForSubscriber(subscriber: string): string[]

Lists all event types a specific subscriber is subscribed to.