alevent v1.0.3
alevent - A Lightweight Event Bus for JavaScript & TypeScript
alevent
is a lightweight and flexible event bus library for handling communication between different parts of your application. It supports two modes of event-driven communication:
- AppEventBus: For applications that exist on the same domain and are bundled together at build time (e.g., widgets, micro-frontends within a single app).
- GlobalEventBus: For applications that exist on different domains (e.g., iframes, cross-origin communication).
š Installation
npm install alevent
š¹ Usage
1ļøā£ Using AppEventBus
for Same-Domain Communication
Use AppEventBus
when components, widgets, or micro-frontends exist within the same domain and can communicate directly in memory.
š Example: Communication Between Widgets
import { AppEventBus } from 'alevent';
// Create an instance of AppEventBus
const appBus = new AppEventBus<string>();
// Widget A: Subscribe to an event
appBus.on('userLoggedIn', (username) => {
console.log(`User logged in: ${username}`);
});
// Widget B: Emit an event
appBus.emit('userLoggedIn', 'Alice');
ā Fast and efficient ā Uses in-memory communication. ā Ideal for single-page applications (SPAs), widgets, and modules.
2ļøā£ Using GlobalEventBus
for Cross-Domain Communication
Use GlobalEventBus
when applications reside on different domains (e.g., a parent app communicating with an iframe).
š Example: Communication Between a Parent App and an Iframe
import { GlobalEventBus } from 'alevent';
// Parent App: Emit an event
const globalBus = new GlobalEventBus<string>();
globalBus.emit('iframeMessage', 'Hello from Parent');
// Iframe App: Listen for the event
const iframeBus = new GlobalEventBus<string>();
iframeBus.on('iframeMessage', (message) => {
console.log(`Received in iframe: ${message}`);
});
ā
Allows cross-origin communication (e.g., between an iframe and its parent page).
ā
Uses window.dispatchEvent
for event propagation.
š¹ Propagation Control and Ordered Handlers
Both AppEventBus
and GlobalEventBus
support ordered handlers and stopping propagation. A listener can prevent further handlers from executing by returning false
.
š Example: Ordered Handlers and Stopping Propagation
import { AppEventBus } from 'alevent';
const appBus = new AppEventBus<string>();
appBus.on('message', (msg) => {
console.log(`[Handler 1] Received: ${msg}`);
return true; // Continue propagation
}, 0);
appBus.on('message', (msg) => {
console.log(`[Handler 2] Processing: ${msg}`);
return false; // Stop propagation
}, 1);
appBus.on('message', (msg) => {
console.log(`[Handler 3] Should not run`);
}, 2);
appBus.emit('message', 'Hello!');
ā
Handlers execute in order (by index).
ā
Propagation stops when a handler returns false
.
š Features
- š Supports Ordered Handlers ā Event handlers execute in the order they were added.
- š Propagation Control ā Listeners can stop event propagation by returning
false
. - š Lightweight & Fast ā Optimized for minimal performance overhead.
- š Supports Dynamic Subscription & Removal ā Easily add or remove event listeners at runtime.
ā API Reference
1. AppEventBus (For Same-Domain Communication)
š¹ on(event: string, listener: (data: T) => boolean | void, index?: number): void
Registers an event listener with an optional order index.
š¹ off(event: string, listener: (data: T) => boolean | void): void
Removes an event listener.
š¹ emit(event: string, data: T): void
Emits an event, triggering all subscribed listeners.
2. GlobalEventBus (For Cross-Domain Communication)
š¹ on(event: string, listener: (data: T) => boolean | void, index?: number): void
Registers an event listener on the window
object.
š¹ off(event: string, listener: (data: T) => boolean | void): void
Removes an event listener from the window
object.
š¹ emit(event: string, data: T): void
Emits an event using window.dispatchEvent
.
š§ Development & Testing
Run tests to ensure functionality:
npm test
Build the package:
npm run build