@omerdev/event-spark
v1.0.0
Published
Type-safe, ultra-lightweight micro event emitter with wildcards, async listeners, and priority (<1KB)
Maintainers
Readme
⚡ @omerdev/event-spark
Type-safe, ultra-lightweight micro event emitter with wildcards, async listeners, and priority (<1KB).
⚡ Features
- Tiny Footprint: Less than 1KB bundled!
- Strict TypeScript Types: Complete compile-time type checking with EventMap.
- Async Execution (
emitAsync): Await asynchronous handlers with clean promise coordination. - Wildcard Pattern Matching: Match events like
"user:*"or"order:created:*". - Listener Priority: Control which handlers execute first (
priority: 100). - Promise-Based
once(): Supports timeouts andAbortSignal. - Zero Dependencies: Pure modern JavaScript/TypeScript.
📦 Installation
npm install @omerdev/event-spark
# or
pnpm add @omerdev/event-spark
# or
bun add @omerdev/event-spark💡 Quick Start
import { createEmitter } from "@omerdev/event-spark";
interface AppEvents {
"user:login": { userId: string; ip: string };
"order:created": { orderId: string; total: number };
}
const emitter = createEmitter<AppEvents>();
// 1. Type-safe subscriber
emitter.on("user:login", ({ userId }) => {
console.log(`User ${userId} logged in`);
});
// 2. Wildcard listener
emitter.on("order:*", (data) => {
console.log("Order event received:", data);
});
// 3. Priority ordering
emitter.on("user:login", () => checkSecurity(), { priority: 100 });
// 4. Async emission
await emitter.emitAsync("order:created", { orderId: "123", total: 99 });
// 5. Promise once with timeout
const event = await emitter.once("user:login", undefined, { timeoutMs: 5000 });👤 Author
omerdev
📄 License
MIT
