https://github.com/princecodes247/kamui
A powerful, type-safe event bus system for Event driven TypeScript applications
https://github.com/princecodes247/kamui
Last synced: 7 days ago
JSON representation
A powerful, type-safe event bus system for Event driven TypeScript applications
- Host: GitHub
- URL: https://github.com/princecodes247/kamui
- Owner: princecodes247
- Created: 2025-04-03T23:31:48.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-13T23:00:14.000Z (about 1 year ago)
- Last Synced: 2025-10-30T09:45:28.525Z (8 months ago)
- Language: TypeScript
- Homepage:
- Size: 89.8 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Kamui
A powerful, type-safe event bus system for TypeScript applications with advanced event handling capabilities.
## Features
- 🔒 **Type-safe Event Handling**: Fully typed event system that catches type errors at compile time
- 🚀 **High Performance**: Efficient event dispatching with Set-based listener management
- 🎯 **Event Metadata**: Automatic tracking of event IDs, timestamps, and names
- 🔄 **Flexible Architecture**: Support for both EventBus and EventEmitter patterns
- 🛠**Extensible Design**: Easy to extend and adapt to different use cases
## Installation
```bash
npm install kamui
# or
yarn add kamui
# or
pnpm add kamui
```
## Usage
### Basic Event Bus Usage
```typescript
import { createEventBus, ExtendedEventListener } from 'kamui';
// Define your event types
type UserEvents = {
'user:created': ExtendedEventListener<{ id: string; name: string }>[];
'user:updated': ExtendedEventListener<{ id: string; changes: Record }>[];
};
// Create event bus instance
const eventBus = createEventBus({
'user:created': [],
'user:updated': []
});
// Emit an event
eventBus.emit('user:created', {
id: '123',
name: 'John Doe'
});
```
### Using Event Creator
```typescript
import { createEvent } from 'kamui';
type UserCreatedPayload = { id: string; name: string };
const userCreatedListeners = createEvent(
(payload, metadata) => {
console.log(`User created: ${payload.name} at ${metadata?.timestamp}`);
}
);
```
### Event Emitter Adapter
```typescript
import { createEventEmitter } from 'kamui';
const events = [
{
name: 'userCreated',
listeners: [(metadata, payload) => console.log(payload)]
}
];
const emitter = createEventEmitter(events);
emitter.emit('userCreated', { id: '123', name: 'John Doe' });
```
## API Reference
### EventBus
- `createEventBus(events)`: Creates a new event bus instance
- `emit(eventName, payload)`: Emits an event with the given name and payload
- `getListenerCount(eventName)`: Returns the number of listeners for an event
### EventCreator
- `createEvent(...listeners)`: Creates a new event with the given listeners
- `addListener(listener)`: Adds a new listener to an event
- `getListeners()`: Returns all listeners for an event
### Event Types
- `EventMetadata`: Contains event metadata (id, timestamp, name)
- `EventListener`: Basic event listener type
- `ExtendedEventListener`: Event listener with metadata support
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
## License
MIT