PubSub.js is a lightweight JavaScript utility that facilitates event-driven programming by allowing you to subscribe to, publish, and manage events. It supports subscribing multiple observers to events and triggering callbacks when those events are published. It also includes advanced features for handling inter-window communication using postMessage
.
- Subscribe/Unsubscribe: Add or remove event listeners with ease.
- Multiple Subscriptions: Subscribe to multiple events at once.
- Event Publishing: Trigger event callbacks with optional contextual data.
- Observer Management: Remove specific observers or all associated event listeners.
- PostMessage Support: Publish and subscribe to events across windows or iframes using
postMessage
.
import pubsub from './pubsub.js';
pubsub.subscribe("default", "uniqueIdentifier", someFunction);
eventName
: Name of the event to subscribe to (e.g., "default").observer
: A unique identifier for the subscriber.callback
: The function to call when the event is published.
pubsub.subscribeMultiple(["signin", "signout"], "uniqueIdentifier", someFunction);
eventNames
: Array of event names to subscribe to.observer
: A unique identifier for the subscriber.callback
: The function to call for all subscribed events.
pubsub.unsubscribe("default", "uniqueIdentifier");
eventName
: The event name from which to unsubscribe.observer
: The identifier of the observer to remove.
pubsub.unsubscribeObserver("uniqueIdentifier");
pubsub.publish("default", { key: "value" });
eventName
: The event to publish.data
: Optional data to pass to the event callback.
pubsub.initPostMessaging();
Sets up a global listener to handle cross-window messaging via postMessage
.
pubsub.isSubscribed("default");
Returns true
if there are any subscribers to the event.
// Define a callback function
function onEvent(data) {
console.log("Event received:", data);
}
// Subscribe to an event
pubsub.subscribe("userLogin", "myComponent", onEvent);
// Publish the event with some data
pubsub.publish("userLogin", { username: "john_doe" });
// Unsubscribe the observer
pubsub.unsubscribe("userLogin", "myComponent");
This project is licensed under the MIT License.
Created by [email protected]. Version 0.1.0 (2022/01/04).