@unchainedshop/events
Version:
Event emitter abstraction layer for the Unchained Engine
86 lines (60 loc) • 2.21 kB
Markdown
[](https://npmjs.com/package/@unchainedshop/events)
[](https://opensource.org/licenses/EUPL-1.2)
Event emitter abstraction layer for the Unchained Engine. Provides a pluggable event system for emitting and subscribing to business events.
```bash
npm install @unchainedshop/events
```
```typescript
import { emit, subscribe, registerEvents } from '@unchainedshop/events';
// Register custom events
registerEvents(['ORDER_CREATED', 'ORDER_PAID']);
// Subscribe to events
subscribe('ORDER_CREATED', ({ payload }) => {
console.log('Order created:', payload);
});
// Emit events
await emit('ORDER_CREATED', { orderId: '123', total: 99.99 });
```
| Export | Description |
|--------|-------------|
| `emit` | Emit an event with a payload |
| `subscribe` | Subscribe to an event with a callback handler |
| `registerEvents` | Register new event types |
| `getRegisteredEvents` | Get list of all registered event types |
| Export | Description |
|--------|-------------|
| `getEmitAdapter` | Get the current emit adapter |
| `setEmitAdapter` | Set a custom emit adapter |
| `getEmitHistoryAdapter` | Get the current emit history adapter |
| `setEmitHistoryAdapter` | Set a custom emit history adapter |
| Export | Description |
|--------|-------------|
| `EmitAdapter` | Interface for custom emit adapters |
| `RawPayloadType` | Type for event payload data |
| Event | Description |
|-------|-------------|
| `PAGE_VIEW` | Registered by default for page view tracking |
You can implement custom emit adapters for different backends (Redis, Kafka, etc.):
```typescript
import { setEmitAdapter, type EmitAdapter } from '@unchainedshop/events';
const customAdapter: EmitAdapter = {
publish: async (eventName, payload) => {
// Custom publish logic
},
subscribe: (eventName, callback) => {
// Custom subscribe logic
},
};
setEmitAdapter(customAdapter);
```
EUPL-1.2