-
-
Notifications
You must be signed in to change notification settings - Fork 2
Overhaul Docs #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Overhaul Docs #30
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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). |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.