@ahoo-wang/fetcher-eventbus 2.8.5

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/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [2025] [ahoo wang <ahoowang@qq.com>]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,182 @@
1
+ # @ahoo-wang/fetcher-eventbus
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@ahoo-wang/fetcher-eventbus.svg)](https://www.npmjs.com/package/@ahoo-wang/fetcher-eventbus)
4
+ [![Build Status](https://github.com/Ahoo-Wang/fetcher/actions/workflows/ci.yml/badge.svg)](https://github.com/Ahoo-Wang/fetcher/actions)
5
+ [![codecov](https://codecov.io/gh/Ahoo-Wang/fetcher/graph/badge.svg?token=JGiWZ52CvJ)](https://codecov.io/gh/Ahoo-Wang/fetcher)
6
+ [![License](https://img.shields.io/npm/l/@ahoo-wang/fetcher-eventbus.svg)](https://github.com/Ahoo-Wang/fetcher/blob/main/LICENSE)
7
+ [![npm downloads](https://img.shields.io/npm/dm/@ahoo-wang/fetcher-eventbus.svg)](https://www.npmjs.com/package/@ahoo-wang/fetcher-eventbus)
8
+ [![npm bundle size](https://img.shields.io/bundlephobia/minzip/%40ahoo-wang%2Ffetcher-eventbus)](https://www.npmjs.com/package/@ahoo-wang/fetcher-eventbus)
9
+ [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/Ahoo-Wang/fetcher)
10
+
11
+ A TypeScript event bus library providing multiple implementations for handling events: serial execution, parallel
12
+ execution, and cross-tab broadcasting.
13
+
14
+ ## 🌟 Features
15
+
16
+ - **🔄 Serial Execution**: Execute event handlers in order with priority support
17
+ - **⚡ Parallel Execution**: Run event handlers concurrently for better performance
18
+ - **🌐 Cross-Tab Broadcasting**: Broadcast events across browser tabs using BroadcastChannel API
19
+ - **📦 Generic Event Bus**: Manage multiple event types with lazy loading
20
+ - **🔧 Type-Safe**: Full TypeScript support with strict typing
21
+ - **🧵 Async Support**: Handle both synchronous and asynchronous event handlers
22
+ - **🔄 Once Handlers**: Support for one-time event handlers
23
+ - **🛡️ Error Handling**: Robust error handling with logging
24
+
25
+ ## 🚀 Quick Start
26
+
27
+ ### Installation
28
+
29
+ ```bash
30
+ # Using npm
31
+ npm install @ahoo-wang/fetcher-eventbus
32
+
33
+ # Using pnpm
34
+ pnpm add @ahoo-wang/fetcher-eventbus
35
+
36
+ # Using yarn
37
+ yarn add @ahoo-wang/fetcher-eventbus
38
+ ```
39
+
40
+ ### Basic Usage
41
+
42
+ ### Serial Event Bus
43
+
44
+ ```typescript
45
+ import { SerialTypedEventBus } from '@ahoo-wang/fetcher-eventbus';
46
+
47
+ const bus = new SerialTypedEventBus<string>('my-events');
48
+
49
+ bus.on({
50
+ name: 'logger',
51
+ order: 1,
52
+ handle: event => console.log('Event:', event),
53
+ });
54
+
55
+ bus.on({
56
+ name: 'processor',
57
+ order: 2,
58
+ handle: event => console.log('Processing:', event),
59
+ });
60
+
61
+ await bus.emit('hello'); // Handlers execute serially in order
62
+ ```
63
+
64
+ ### Parallel Event Bus
65
+
66
+ ```typescript
67
+ import { ParallelTypedEventBus } from '@ahoo-wang/fetcher-eventbus';
68
+
69
+ const bus = new ParallelTypedEventBus<string>('my-events');
70
+
71
+ bus.on({
72
+ name: 'handler1',
73
+ order: 1,
74
+ handle: async event => console.log('Handler 1:', event),
75
+ });
76
+
77
+ bus.on({
78
+ name: 'handler2',
79
+ order: 2,
80
+ handle: async event => console.log('Handler 2:', event),
81
+ });
82
+
83
+ await bus.emit('hello'); // Both handlers execute in parallel
84
+ ```
85
+
86
+ ### Broadcast Event Bus
87
+
88
+ ```typescript
89
+ import {
90
+ BroadcastTypedEventBus,
91
+ SerialTypedEventBus,
92
+ } from '@ahoo-wang/fetcher-eventbus';
93
+
94
+ const delegate = new SerialTypedEventBus<string>('shared-events');
95
+ const bus = new BroadcastTypedEventBus(delegate);
96
+
97
+ bus.on({
98
+ name: 'cross-tab-handler',
99
+ order: 1,
100
+ handle: event => console.log('Received from other tab:', event),
101
+ });
102
+
103
+ await bus.emit('broadcast-message'); // Emits locally and broadcasts to other tabs
104
+ ```
105
+
106
+ ### Generic Event Bus
107
+
108
+ ```typescript
109
+ import { EventBus, SerialTypedEventBus } from '@ahoo-wang/fetcher-eventbus';
110
+
111
+ const supplier = (type: string) => new SerialTypedEventBus(type);
112
+ const bus = new EventBus<{ 'user-login': string; 'order-update': number }>(
113
+ supplier,
114
+ );
115
+
116
+ bus.on('user-login', {
117
+ name: 'welcome',
118
+ order: 1,
119
+ handle: username => console.log(`Welcome ${username}!`),
120
+ });
121
+
122
+ await bus.emit('user-login', 'john-doe');
123
+ ```
124
+
125
+ ## API
126
+
127
+ ### TypedEventBus<EVENT>
128
+
129
+ - `type: EventType` - The event type identifier
130
+ - `handlers: EventHandler<EVENT>[]` - Array of registered handlers
131
+ - `on(handler: EventHandler<EVENT>): boolean` - Add an event handler
132
+ - `off(name: string): boolean` - Remove an event handler by name
133
+ - `emit(event: EVENT): Promise<void>` - Emit an event
134
+ - `destroy(): void` - Clean up resources
135
+
136
+ ### EventHandler<EVENT>
137
+
138
+ ```typescript
139
+ interface EventHandler<EVENT> {
140
+ name: string;
141
+ order: number;
142
+ handle: (event: EVENT) => void | Promise<void>;
143
+ once?: boolean; // Optional: remove after first execution
144
+ }
145
+ ```
146
+
147
+ ### EventBus<Events>
148
+
149
+ - `on<Key>(type: Key, handler: EventHandler<Events[Key]>): boolean`
150
+ - `off<Key>(type: Key, handler: EventHandler<Events[Key]>): boolean`
151
+ - `emit<Key>(type: Key, event: Events[Key]): void | Promise<void>`
152
+ - `destroy(): void`
153
+
154
+ ## 🧪 Testing
155
+
156
+ ```bash
157
+ # Run tests
158
+ pnpm test
159
+
160
+ # Run tests with coverage
161
+ pnpm test --coverage
162
+ ```
163
+
164
+ ## 🤝 Contributing
165
+
166
+ Contributions are welcome! Please see
167
+ the [contributing guide](https://github.com/Ahoo-Wang/fetcher/blob/main/CONTRIBUTING.md) for more details.
168
+
169
+ ## Browser Support
170
+
171
+ - **BroadcastTypedEventBus** requires BroadcastChannel API support (modern browsers)
172
+ - Other implementations work in all environments supporting ES2020+
173
+
174
+ ## 📄 License
175
+
176
+ Apache-2.0
177
+
178
+ ---
179
+
180
+ <p align="center">
181
+ Part of the <a href="https://github.com/Ahoo-Wang/fetcher">Fetcher</a> ecosystem
182
+ </p>
@@ -0,0 +1,194 @@
1
+ # @ahoo-wang/fetcher-eventbus
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@ahoo-wang/fetcher-eventbus.svg)](https://www.npmjs.com/package/@ahoo-wang/fetcher-eventbus)
4
+ [![Build Status](https://github.com/Ahoo-Wang/fetcher/actions/workflows/ci.yml/badge.svg)](https://github.com/Ahoo-Wang/fetcher/actions)
5
+ [![codecov](https://codecov.io/gh/Ahoo-Wang/fetcher/graph/badge.svg?token=JGiWZ52CvJ)](https://codecov.io/gh/Ahoo-Wang/fetcher)
6
+ [![License](https://img.shields.io/npm/l/@ahoo-wang/fetcher-eventbus.svg)](https://github.com/Ahoo-Wang/fetcher/blob/main/LICENSE)
7
+ [![npm downloads](https://img.shields.io/npm/dm/@ahoo-wang/fetcher-eventbus.svg)](https://www.npmjs.com/package/@ahoo-wang/fetcher-eventbus)
8
+ [![npm bundle size](https://img.shields.io/bundlephobia/minzip/%40ahoo-wang%2Ffetcher-eventbus)](https://www.npmjs.com/package/@ahoo-wang/fetcher-eventbus)
9
+ [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/Ahoo-Wang/fetcher)
10
+
11
+ 一个 TypeScript 事件总线库,提供多种实现来处理事件:串行执行、并行执行和跨标签页广播。
12
+
13
+ ## 🌟 功能特性
14
+
15
+ - **🔄 串行执行**:按优先级顺序执行事件处理器
16
+ - **⚡ 并行执行**:并发运行事件处理器以提升性能
17
+ - **🌐 跨标签页广播**:使用 BroadcastChannel API 在浏览器标签页间广播事件
18
+ - **📦 通用事件总线**:使用懒加载管理多种事件类型
19
+ - **🔧 类型安全**:完整的 TypeScript 支持和严格类型检查
20
+ - **🧵 异步支持**:处理同步和异步事件处理器
21
+ - **🔄 一次性处理器**:支持一次性事件处理器
22
+ - **🛡️ 错误处理**:强大的错误处理和日志记录
23
+
24
+ ## 🚀 快速开始
25
+
26
+ ### 安装
27
+
28
+ ```bash
29
+ # 使用 npm
30
+ npm install @ahoo-wang/fetcher-eventbus
31
+
32
+ # 使用 pnpm
33
+ pnpm add @ahoo-wang/fetcher-eventbus
34
+
35
+ # 使用 yarn
36
+ yarn add @ahoo-wang/fetcher-eventbus
37
+ ```
38
+
39
+ ### 基本用法
40
+
41
+ ## 串行事件总线
42
+
43
+ ```typescript
44
+ import { SerialTypedEventBus } from '@ahoo-wang/fetcher-eventbus';
45
+
46
+ const bus = new SerialTypedEventBus<string>('my-events');
47
+
48
+ bus.on({
49
+ name: 'logger',
50
+ order: 1,
51
+ handle: event => console.log('事件:', event),
52
+ });
53
+
54
+ bus.on({
55
+ name: 'processor',
56
+ order: 2,
57
+ handle: event => console.log('处理:', event),
58
+ });
59
+
60
+ await bus.emit('hello'); // 处理器按顺序串行执行
61
+ ```
62
+
63
+ ### 并行事件总线
64
+
65
+ ```typescript
66
+ import { ParallelTypedEventBus } from '@ahoo-wang/fetcher-eventbus';
67
+
68
+ const bus = new ParallelTypedEventBus<string>('my-events');
69
+
70
+ bus.on({
71
+ name: 'handler1',
72
+ order: 1,
73
+ handle: async event => console.log('处理器 1:', event),
74
+ });
75
+
76
+ bus.on({
77
+ name: 'handler2',
78
+ order: 2,
79
+ handle: async event => console.log('处理器 2:', event),
80
+ });
81
+
82
+ await bus.emit('hello'); // 两个处理器并行执行
83
+ ```
84
+
85
+ ### 广播事件总线
86
+
87
+ ```typescript
88
+ import {
89
+ BroadcastTypedEventBus,
90
+ SerialTypedEventBus,
91
+ } from '@ahoo-wang/fetcher-eventbus';
92
+
93
+ const delegate = new SerialTypedEventBus<string>('shared-events');
94
+ const bus = new BroadcastTypedEventBus(delegate);
95
+
96
+ bus.on({
97
+ name: 'cross-tab-handler',
98
+ order: 1,
99
+ handle: event => console.log('来自其他标签页:', event),
100
+ });
101
+
102
+ await bus.emit('broadcast-message'); // 本地执行并广播到其他标签页
103
+ ```
104
+
105
+ ### 通用事件总线
106
+
107
+ ```typescript
108
+ import { EventBus, SerialTypedEventBus } from '@ahoo-wang/fetcher-eventbus';
109
+
110
+ const supplier = (type: string) => new SerialTypedEventBus(type);
111
+ const bus = new EventBus<{ 'user-login': string; 'order-update': number }>(
112
+ supplier,
113
+ );
114
+
115
+ bus.on('user-login', {
116
+ name: 'welcome',
117
+ order: 1,
118
+ handle: username => console.log(`欢迎 ${username}!`),
119
+ });
120
+
121
+ await bus.emit('user-login', 'john-doe');
122
+ ```
123
+
124
+ ## 🔧 配置
125
+
126
+ ### EventHandler<EVENT> 接口
127
+
128
+ ```typescript
129
+ interface EventHandler<EVENT> {
130
+ name: string;
131
+ order: number;
132
+ handle: (event: EVENT) => void | Promise<void>;
133
+ once?: boolean; // 可选:在首次执行后移除
134
+ }
135
+ ```
136
+
137
+ ## 📚 API 参考
138
+
139
+ ### TypedEventBus<EVENT>
140
+
141
+ - `type: EventType` - 事件类型标识符
142
+ - `handlers: EventHandler<EVENT>[]` - 已注册处理器数组
143
+ - `on(handler: EventHandler<EVENT>): boolean` - 添加事件处理器
144
+ - `off(name: string): boolean` - 按名称移除事件处理器
145
+ - `emit(event: EVENT): Promise<void>` - 发射事件
146
+ - `destroy(): void` - 清理资源
147
+
148
+ ### EventHandler<EVENT>
149
+
150
+ ```typescript
151
+ interface EventHandler<EVENT> {
152
+ name: string;
153
+ order: number;
154
+ handle: (event: EVENT) => void | Promise<void>;
155
+ once?: boolean; // 可选:首次执行后移除
156
+ }
157
+ ```
158
+
159
+ ### EventBus<Events>
160
+
161
+ - `on<Key>(type: Key, handler: EventHandler<Events[Key]>): boolean`
162
+ - `off<Key>(type: Key, handler: EventHandler<Events[Key]>): boolean`
163
+ - `emit<Key>(type: Key, event: Events[Key]): void | Promise<void>`
164
+ - `destroy(): void`
165
+
166
+ ## 🧪 测试
167
+
168
+ ```bash
169
+ # 运行测试
170
+ pnpm test
171
+
172
+ # 运行测试并生成覆盖率报告
173
+ pnpm test --coverage
174
+ ```
175
+
176
+ ## 🤝 贡献
177
+
178
+ 欢迎贡献!请查看
179
+ [贡献指南](https://github.com/Ahoo-Wang/fetcher/blob/main/CONTRIBUTING.md) 获取更多详情。
180
+
181
+ ## 🌐 浏览器支持
182
+
183
+ - **BroadcastTypedEventBus** 需要 BroadcastChannel API 支持(现代浏览器)
184
+ - 其他实现适用于支持 ES2020+ 的所有环境
185
+
186
+ ## 📄 许可证
187
+
188
+ Apache-2.0
189
+
190
+ ---
191
+
192
+ <p align="center">
193
+ <a href="https://github.com/Ahoo-Wang/fetcher">Fetcher</a> 生态系统的一部分
194
+ </p>
@@ -0,0 +1,67 @@
1
+ import { TypedEventBus } from './typedEventBus';
2
+ import { EventHandler, EventType } from './types';
3
+ /**
4
+ * Broadcast implementation of TypedEventBus using BroadcastChannel API
5
+ *
6
+ * Enables cross-tab/window event broadcasting within the same origin. Events are first emitted
7
+ * locally via the delegate, then broadcasted to other tabs/windows. Incoming broadcasts from
8
+ * other tabs are handled by the delegate as well.
9
+ *
10
+ * Note: BroadcastChannel is only supported in modern browsers and requires same-origin policy.
11
+ *
12
+ * @template EVENT - The type of events this bus handles
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * const delegate = new SerialTypedEventBus<string>('user-events');
17
+ * const bus = new BroadcastTypedEventBus(delegate);
18
+ * bus.on({ name: 'user-login', order: 1, handle: (event) => console.log('User logged in:', event) });
19
+ * await bus.emit('john-doe'); // Emits locally and broadcasts to other tabs
20
+ * ```
21
+ *
22
+ * @example Custom channel name
23
+ * ```typescript
24
+ * const bus = new BroadcastTypedEventBus(delegate, 'my-custom-channel');
25
+ * ```
26
+ */
27
+ export declare class BroadcastTypedEventBus<EVENT> implements TypedEventBus<EVENT> {
28
+ private readonly delegate;
29
+ readonly type: EventType;
30
+ private readonly broadcastChannel;
31
+ /**
32
+ * Creates a broadcast typed event bus
33
+ *
34
+ * @param delegate - The underlying TypedEventBus for local event handling
35
+ * @param channel - Optional custom BroadcastChannel name; defaults to `_broadcast_:{type}`
36
+ */
37
+ constructor(delegate: TypedEventBus<EVENT>, channel?: string);
38
+ /**
39
+ * Gets a copy of all registered event handlers from the delegate
40
+ */
41
+ get handlers(): EventHandler<EVENT>[];
42
+ /**
43
+ * Emits an event locally and broadcasts it to other tabs/windows
44
+ *
45
+ * @param event - The event to emit
46
+ */
47
+ emit(event: EVENT): Promise<void>;
48
+ /**
49
+ * Removes an event handler by name from the delegate
50
+ *
51
+ * @param name - The name of the handler to remove
52
+ * @returns true if a handler was removed, false otherwise
53
+ */
54
+ off(name: string): boolean;
55
+ /**
56
+ * Adds an event handler to the delegate
57
+ *
58
+ * @param handler - The event handler to add
59
+ * @returns true if the handler was added, false if a handler with the same name already exists
60
+ */
61
+ on(handler: EventHandler<EVENT>): boolean;
62
+ /**
63
+ * Cleans up resources by closing the BroadcastChannel
64
+ */
65
+ destroy(): void;
66
+ }
67
+ //# sourceMappingURL=broadcastTypedEventBus.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"broadcastTypedEventBus.d.ts","sourceRoot":"","sources":["../src/broadcastTypedEventBus.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,sBAAsB,CAAC,KAAK,CAAE,YAAW,aAAa,CAAC,KAAK,CAAC;IAWtE,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAV3B,SAAgB,IAAI,EAAE,SAAS,CAAC;IAChC,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAmB;IAEpD;;;;;OAKG;gBAEgB,QAAQ,EAAE,aAAa,CAAC,KAAK,CAAC,EAC/C,OAAO,CAAC,EAAE,MAAM;IAclB;;OAEG;IACH,IAAI,QAAQ,IAAI,YAAY,CAAC,KAAK,CAAC,EAAE,CAEpC;IAED;;;;OAIG;IACG,IAAI,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC;IAKvC;;;;;OAKG;IACH,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAI1B;;;;;OAKG;IACH,EAAE,CAAC,OAAO,EAAE,YAAY,CAAC,KAAK,CAAC,GAAG,OAAO;IAIzC;;OAEG;IACH,OAAO;CAGR"}