@ahoo-wang/fetcher-eventbus 2.9.1 → 2.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +53 -4
- package/README.zh-CN.md +53 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,12 +15,14 @@ execution, and cross-tab broadcasting.
|
|
|
15
15
|
|
|
16
16
|
- **🔄 Serial Execution**: Execute event handlers in order with priority support
|
|
17
17
|
- **⚡ Parallel Execution**: Run event handlers concurrently for better performance
|
|
18
|
-
- **🌐 Cross-Tab Broadcasting**: Broadcast events across browser tabs using BroadcastChannel API
|
|
18
|
+
- **🌐 Cross-Tab Broadcasting**: Broadcast events across browser tabs using BroadcastChannel API or localStorage fallback
|
|
19
|
+
- **💾 Storage Messenger**: Direct cross-tab messaging with TTL and cleanup
|
|
19
20
|
- **📦 Generic Event Bus**: Manage multiple event types with lazy loading
|
|
20
21
|
- **🔧 Type-Safe**: Full TypeScript support with strict typing
|
|
21
22
|
- **🧵 Async Support**: Handle both synchronous and asynchronous event handlers
|
|
22
23
|
- **🔄 Once Handlers**: Support for one-time event handlers
|
|
23
24
|
- **🛡️ Error Handling**: Robust error handling with logging
|
|
25
|
+
- **🔌 Auto Fallback**: Automatically selects best available cross-tab communication method
|
|
24
26
|
|
|
25
27
|
## 🚀 Quick Start
|
|
26
28
|
|
|
@@ -92,7 +94,7 @@ import {
|
|
|
92
94
|
} from '@ahoo-wang/fetcher-eventbus';
|
|
93
95
|
|
|
94
96
|
const delegate = new SerialTypedEventBus<string>('shared-events');
|
|
95
|
-
const bus = new BroadcastTypedEventBus(delegate);
|
|
97
|
+
const bus = new BroadcastTypedEventBus({ delegate });
|
|
96
98
|
|
|
97
99
|
bus.on({
|
|
98
100
|
name: 'cross-tab-handler',
|
|
@@ -103,6 +105,29 @@ bus.on({
|
|
|
103
105
|
await bus.emit('broadcast-message'); // Emits locally and broadcasts to other tabs
|
|
104
106
|
```
|
|
105
107
|
|
|
108
|
+
**Note**: If BroadcastChannel API is not supported, the library automatically falls back to using localStorage for cross-tab communication.
|
|
109
|
+
|
|
110
|
+
### Storage Messenger (Direct API)
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
import { StorageMessenger } from '@ahoo-wang/fetcher-eventbus';
|
|
114
|
+
|
|
115
|
+
const messenger = new StorageMessenger({
|
|
116
|
+
channelName: 'my-channel',
|
|
117
|
+
ttl: 5000, // 5 seconds TTL for messages
|
|
118
|
+
cleanupInterval: 1000, // Clean expired messages every 1 second
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
messenger.onmessage = message => {
|
|
122
|
+
console.log('Received:', message);
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
messenger.postMessage('Hello from another tab!');
|
|
126
|
+
|
|
127
|
+
// Clean up when done
|
|
128
|
+
messenger.close();
|
|
129
|
+
```
|
|
130
|
+
|
|
106
131
|
### Generic Event Bus
|
|
107
132
|
|
|
108
133
|
```typescript
|
|
@@ -144,6 +169,22 @@ interface EventHandler<EVENT> {
|
|
|
144
169
|
}
|
|
145
170
|
```
|
|
146
171
|
|
|
172
|
+
### Cross-Tab Messengers
|
|
173
|
+
|
|
174
|
+
- **BroadcastChannelMessenger**: Uses BroadcastChannel API for efficient cross-tab communication
|
|
175
|
+
- **StorageMessenger**: Uses localStorage events as fallback when BroadcastChannel is unavailable
|
|
176
|
+
- **createCrossTabMessenger**: Automatically selects the best available messenger
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
import { createCrossTabMessenger } from '@ahoo-wang/fetcher-eventbus';
|
|
180
|
+
|
|
181
|
+
const messenger = createCrossTabMessenger('my-channel');
|
|
182
|
+
if (messenger) {
|
|
183
|
+
messenger.onmessage = msg => console.log(msg);
|
|
184
|
+
messenger.postMessage('Hello!');
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
147
188
|
### EventBus<Events>
|
|
148
189
|
|
|
149
190
|
- `on<Key>(type: Key, handler: EventHandler<Events[Key]>): boolean`
|
|
@@ -168,8 +209,16 @@ the [contributing guide](https://github.com/Ahoo-Wang/fetcher/blob/main/CONTRIBU
|
|
|
168
209
|
|
|
169
210
|
## Browser Support
|
|
170
211
|
|
|
171
|
-
- **BroadcastTypedEventBus
|
|
172
|
-
-
|
|
212
|
+
- **BroadcastTypedEventBus**: Requires BroadcastChannel API (Chrome 54+, Firefox 38+, Safari 15.4+)
|
|
213
|
+
- **StorageMessenger**: Works in all browsers supporting localStorage and StorageEvent
|
|
214
|
+
- **Other implementations**: Compatible with ES2020+ environments (Node.js, browsers)
|
|
215
|
+
|
|
216
|
+
## Performance
|
|
217
|
+
|
|
218
|
+
- **Serial Event Bus**: Minimal overhead, predictable execution order
|
|
219
|
+
- **Parallel Event Bus**: Optimized for concurrent handler execution
|
|
220
|
+
- **Broadcast Event Bus**: Efficient cross-tab communication with automatic fallback
|
|
221
|
+
- **Memory Management**: Automatic cleanup of expired messages and event handlers
|
|
173
222
|
|
|
174
223
|
## 📄 License
|
|
175
224
|
|
package/README.zh-CN.md
CHANGED
|
@@ -14,12 +14,14 @@
|
|
|
14
14
|
|
|
15
15
|
- **🔄 串行执行**:按优先级顺序执行事件处理器
|
|
16
16
|
- **⚡ 并行执行**:并发运行事件处理器以提升性能
|
|
17
|
-
- **🌐 跨标签页广播**:使用 BroadcastChannel API
|
|
17
|
+
- **🌐 跨标签页广播**:使用 BroadcastChannel API 或 localStorage 回退在浏览器标签页间广播事件
|
|
18
|
+
- **💾 存储消息器**:直接跨标签页消息传递,支持 TTL 和清理
|
|
18
19
|
- **📦 通用事件总线**:使用懒加载管理多种事件类型
|
|
19
20
|
- **🔧 类型安全**:完整的 TypeScript 支持和严格类型检查
|
|
20
21
|
- **🧵 异步支持**:处理同步和异步事件处理器
|
|
21
22
|
- **🔄 一次性处理器**:支持一次性事件处理器
|
|
22
23
|
- **🛡️ 错误处理**:强大的错误处理和日志记录
|
|
24
|
+
- **🔌 自动回退**:自动选择最佳可用的跨标签页通信方式
|
|
23
25
|
|
|
24
26
|
## 🚀 快速开始
|
|
25
27
|
|
|
@@ -91,7 +93,7 @@ import {
|
|
|
91
93
|
} from '@ahoo-wang/fetcher-eventbus';
|
|
92
94
|
|
|
93
95
|
const delegate = new SerialTypedEventBus<string>('shared-events');
|
|
94
|
-
const bus = new BroadcastTypedEventBus(delegate);
|
|
96
|
+
const bus = new BroadcastTypedEventBus({ delegate });
|
|
95
97
|
|
|
96
98
|
bus.on({
|
|
97
99
|
name: 'cross-tab-handler',
|
|
@@ -102,6 +104,29 @@ bus.on({
|
|
|
102
104
|
await bus.emit('broadcast-message'); // 本地执行并广播到其他标签页
|
|
103
105
|
```
|
|
104
106
|
|
|
107
|
+
**注意**:如果不支持 BroadcastChannel API,库会自动回退到使用 localStorage 进行跨标签页通信。
|
|
108
|
+
|
|
109
|
+
### 存储消息器(直接 API)
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
import { StorageMessenger } from '@ahoo-wang/fetcher-eventbus';
|
|
113
|
+
|
|
114
|
+
const messenger = new StorageMessenger({
|
|
115
|
+
channelName: 'my-channel',
|
|
116
|
+
ttl: 5000, // 消息 TTL 5 秒
|
|
117
|
+
cleanupInterval: 1000, // 每 1 秒清理过期消息
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
messenger.onmessage = message => {
|
|
121
|
+
console.log('收到:', message);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
messenger.postMessage('来自其他标签页的问候!');
|
|
125
|
+
|
|
126
|
+
// 完成后清理
|
|
127
|
+
messenger.close();
|
|
128
|
+
```
|
|
129
|
+
|
|
105
130
|
### 通用事件总线
|
|
106
131
|
|
|
107
132
|
```typescript
|
|
@@ -156,6 +181,22 @@ interface EventHandler<EVENT> {
|
|
|
156
181
|
}
|
|
157
182
|
```
|
|
158
183
|
|
|
184
|
+
### 跨标签页消息器
|
|
185
|
+
|
|
186
|
+
- **BroadcastChannelMessenger**:使用 BroadcastChannel API 进行高效跨标签页通信
|
|
187
|
+
- **StorageMessenger**:当 BroadcastChannel 不可用时,使用 localStorage 事件作为回退
|
|
188
|
+
- **createCrossTabMessenger**:自动选择最佳可用的消息器
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
import { createCrossTabMessenger } from '@ahoo-wang/fetcher-eventbus';
|
|
192
|
+
|
|
193
|
+
const messenger = createCrossTabMessenger('my-channel');
|
|
194
|
+
if (messenger) {
|
|
195
|
+
messenger.onmessage = msg => console.log(msg);
|
|
196
|
+
messenger.postMessage('你好!');
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
159
200
|
### EventBus<Events>
|
|
160
201
|
|
|
161
202
|
- `on<Key>(type: Key, handler: EventHandler<Events[Key]>): boolean`
|
|
@@ -180,8 +221,16 @@ pnpm test --coverage
|
|
|
180
221
|
|
|
181
222
|
## 🌐 浏览器支持
|
|
182
223
|
|
|
183
|
-
- **BroadcastTypedEventBus
|
|
184
|
-
-
|
|
224
|
+
- **BroadcastTypedEventBus**:需要 BroadcastChannel API 支持(Chrome 54+、Firefox 38+、Safari 15.4+)
|
|
225
|
+
- **StorageMessenger**:适用于支持 localStorage 和 StorageEvent 的所有浏览器
|
|
226
|
+
- **其他实现**:兼容 ES2020+ 环境(Node.js、浏览器)
|
|
227
|
+
|
|
228
|
+
## 性能
|
|
229
|
+
|
|
230
|
+
- **串行事件总线**:最小开销,可预测执行顺序
|
|
231
|
+
- **并行事件总线**:优化并发处理器执行
|
|
232
|
+
- **广播事件总线**:高效跨标签页通信,自动回退
|
|
233
|
+
- **内存管理**:自动清理过期消息和事件处理器
|
|
185
234
|
|
|
186
235
|
## 📄 许可证
|
|
187
236
|
|