Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ export default defineConfig({
link: '/getting-started'
},
{
text: 'Additional Details',
link: '/additional-details'
text: 'Methods',
link: '/methods'
},
{
text: 'Broadcasting Events',
link: '/broadcasting-events'
}
]
},
Expand Down
49 changes: 49 additions & 0 deletions docs/README.md
Comment thread
pleek91 marked this conversation as resolved.
Outdated
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Kitbag Events Documentation

Welcome to the Kitbag Events documentation. This package provides a simple, lightweight event bus written in TypeScript with cross-tab broadcasting support.

## Documentation Structure

### Getting Started
- **Getting Started** (`/getting-started`) - Complete introduction with examples
- **Methods** (`/methods`) - Comprehensive API reference
- **Broadcasting Events** (`/broadcasting-events`) - Cross-tab communication guide

### API Reference
- **Functions** - `createEmitter` function documentation
- **Types** - All TypeScript type definitions
- **Errors** - Error classes and handling

## Quick Start

```ts
import { createEmitter } from '@kitbag/events'

type Events = {
userLogin: { userId: string }
messageReceived: { content: string }
}

const emitter = createEmitter<Events>()

emitter.on('userLogin', ({ userId }) => {
console.log(`User ${userId} logged in`)
})

emitter.emit('userLogin', { userId: '123' })
```

## Key Features

- **Type Safe**: Full TypeScript support with typed events
- **Cross-Tab Support**: Built-in broadcasting across browser tabs
- **Rich API**: on, off, once, next, emit, count, clear methods
- **Abort Support**: Automatic cleanup with AbortSignal
- **Promise Based**: Async event waiting with timeout support
- **Zero Dependencies**: Lightweight and fast

## Need Help?

- [GitHub Issues](https://github.com/kitbagjs/events/issues)
- [Discord Community](https://discord.gg/zw7dpcc5HV)
- [NPM Package](https://www.npmjs.com/package/@kitbag/events)
116 changes: 0 additions & 116 deletions docs/additional-details.md

This file was deleted.

34 changes: 34 additions & 0 deletions docs/api/functions/createEmitter.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,40 @@ clear: () => void;

`void`

### count()

```ts
count: <E>() => number<E>(event, options?) => number;
```

#### Type Parameters

| Type Parameter |
| ------ |
| `E` *extends* `string` \| `number` \| `symbol` |

#### Returns

`number`

#### Type Parameters

| Type Parameter |
| ------ |
| `E` *extends* `string` \| `number` \| `symbol` |

#### Parameters

| Parameter | Type |
| ------ | ------ |
| `event` | `E` |
| `options`? | \{ `global`: `boolean`; \} |
| `options.global`? | `boolean` |

#### Returns

`number`

### emit()

```ts
Expand Down
65 changes: 65 additions & 0 deletions docs/broadcasting-events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Broadcasting Events

Kitbag Events supports broadcasting events across multiple browser tabs/windows using the [`BroadcastChannel`](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API) API.

## Setup

Pass a channel name when creating your emitter:

```ts
import { createEmitter } from '@kitbag/events'

type Events = {
userLogin: { userId: string }
messageReceived: { content: string }
}

const emitter = createEmitter<Events>({
broadcastChannel: 'my-app-events'
})
```

## How It Works

When you specify a `broadcastChannel`:

1. **Local emission**: Events are processed by handlers in the current tab
2. **Cross-tab broadcast**: Events are automatically sent to all other tabs using the same channel name
3. **Automatic reception**: Other tabs automatically receive and process broadcasted events

## Example: Multi-tab Chat

```ts
// Tab 1: Send message
emitter.emit('messageReceived', { content: 'Hello from Tab 1!' })

// Tab 2: Automatically receives the message
emitter.on('messageReceived', ({ content }) => {
console.log('Message received:', content) // "Hello from Tab 1!"
})
```

## Channel Naming

Use descriptive, unique channel names to avoid conflicts:

```ts
// Good - specific to your app
broadcastChannel: 'my-chat-app-v1'

// Good - includes user context
broadcastChannel: `user-${userId}-events`

// Avoid - too generic
broadcastChannel: 'events'
```

## Use Cases

- **Real-time updates**: Sync user actions across tabs
- **Session management**: Notify all tabs when user logs out
- **Data synchronization**: Keep multiple tabs in sync
- **Notifications**: Broadcast system-wide alerts

## Structured Clone
Data sent is serialized using the [structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm).
Loading