mfe-broadcast
v1.0.0
Published
A TypeScript library for managing Broadcast Channels
Downloads
4
Maintainers
Readme
Sure, here's a basic README.md file example for your microfrontend broadcast functionality:
Microfrontend Broadcast
This module provides classes to manage and interact with broadcast channels in a microfrontend environment.
Installation
To install the module, use npm or yarn:
npm install mfe-broadcast
# or
yarn add mfe-broadcast
Usage
BroadcastChannelInstanceManager
The BroadcastChannelInstanceManager
class manages multiple BroadcastService
instances.
Methods
getAllInstance(): BroadcastService[]
- Retrieves all active
BroadcastService
instances.
- Retrieves all active
createChannels(...channels: string[]): void
- Creates new broadcast channels with the specified names.
dropChannels(): void
- Closes all active broadcast channels.
dropByName(channelName: string): void
- Closes the broadcast channel with the specified name.
getChannelByName(channelName: string): BroadcastService | null
- Retrieves a specific broadcast channel instance by name.
BroadcastService
The BroadcastService
class manages communication within a single broadcast channel.
Constructor
- new BroadcastService(channelName: string)
- Initializes a new broadcast channel with the given name.
Methods
getUUID(): string
- Retrieves the unique identifier (UUID) of the broadcast channel.
static getInstance(channelName?: string): BroadcastService
- Retrieves a singleton instance of
BroadcastService
for the specified channel name.
- Retrieves a singleton instance of
initChannel(channelName: string): void
- Initializes the broadcast channel with the specified name.
sendMessage(message: any): void
- Sends a message through the broadcast channel.
onMessage(callback: (arg: any) => void): void
- Registers a callback function to handle incoming messages.
close(): void
- Closes the broadcast channel.
Example Usage
import { BroadcastChannelInstanceManager } from 'mfe-broadcast';
// Create an instance of BroadcastChannelInstanceManager
const manager = new BroadcastChannelInstanceManager();
// Create new broadcast channels
manager.createChannels('channel1', 'channel2');
// Retrieve and use a specific channel
const channel1 = manager.getChannelByName('channel1');
channel1.initChannel("channel1")
if (channel1) {
channel1.sendMessage('Hello, world!');
channel1.onMessage((message) => {
console.log('Received message:', message);
});
}
// Close all channels
manager.dropChannels();
License
This project is licensed under the MIT License - see the LICENSE file for details.
Adjust the sections and content based on your specific requirements and any additional features or configurations your module may have.