simple-event-handler
v1.1.0
Published
Lightweight, framework-agnostic event handler library for both browser and Node.js environments
Downloads
36
Maintainers
Readme
simple-event-handler
A lightweight, framework-agnostic event handler library for both front-end and back-end JavaScript applications. Implement the pub/sub (publisher-subscriber) pattern to decouple your code and enable event-driven architecture. Works seamlessly in browsers, Node.js, and modern JavaScript frameworks like React, Vue, and Angular.
Perfect for managing application-wide events, component communication, real-time updates, and building loosely coupled, maintainable codebases.
Features
- 🚀 Lightweight - Less than 2KB minified
- 🔧 Zero Dependencies - No external requirements
- 💪 TypeScript - Full type definitions included
- 🌐 Universal - Works in browser and Node.js
- ⚡ Fast - Optimized for performance
- 🔗 Method Chaining - Fluent API support
- 📦 Multiple Formats - UMD, CommonJS, ES Module
- ✅ 100% Test Coverage - Thoroughly tested
Table of Contents
Installation
npm
npm install simple-event-handleryarn
yarn add simple-event-handlerpnpm
pnpm add simple-event-handlerCDN
<script src="https://unpkg.com/simple-event-handler/dist/simple-event-handler.min.js"></script>Quick Start
import eventHandler from 'simple-event-handler';
// Subscribe to an event
eventHandler.subscribe('user:login', (user) => {
console.log('User logged in:', user.name);
});
// Fire the event
eventHandler.fire('user:login', { name: 'John Doe' });Examples
For comprehensive usage examples including React, Vue, TypeScript, and more, see EXAMPLES.md.
Quick examples:
// Node.js / CommonJS
const eventHandler = require('simple-event-handler');
eventHandler.subscribe('event', (data) => console.log(data));
eventHandler.fire('event', { message: 'Hello!' });
// ES Modules / TypeScript
import eventHandler from 'simple-event-handler';
eventHandler.on('notification', (msg) => console.log(msg));
eventHandler.emit('notification', 'Hello from ESM!');
// Method chaining
eventHandler
.subscribe('update', (data) => console.log(data))
.fire('update', { value: 42 })
.unsubscribeAll('update');API Documentation
For complete API reference, see API.md.
Quick Reference:
subscribe(events, handler, $scope?)/on()- Register event handler(s)once(events, handler, $scope?)- Register one-time handlerfire(name, args?)/emit()- Trigger eventunsubscribe(name, handler)/off()- Remove specific handlerunsubscribeAll(name)/offAll()- Remove all handlers
License
MIT - See LICENSE file for details.
