vue3-event-bus-plugin 1.0.0 → 1.0.2

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.
@@ -0,0 +1 @@
1
+ class e{constructor(e={}){this.events={},this.eventCache={},this.options={enableCache:!1!==e.enableCache,maxCacheSize:e.maxCacheSize||10,clearCacheAfterSubscribe:!1!==e.clearCacheAfterSubscribe,...e}}on(e,t,s={}){if("function"!=typeof t)throw new TypeError("回调函数必须是函数类型");this.events[e]||(this.events[e]=[]);const n={callback:t,once:!!s.once};return this.events[e].push(n),this.options.enableCache&&this.eventCache[e]&&([...this.eventCache[e]].forEach(s=>{try{t(...s.args)}catch(t){console.error(`EventBus 缓存事件 "${e}" 回调执行出错:`,t)}n.once&&this.off(e,t)}),this.options.clearCacheAfterSubscribe&&delete this.eventCache[e]),()=>this.off(e,t)}once(e,t){return this.on(e,t,{once:!0})}off(e,t){if(this.events[e]){if(!t)return void delete this.events[e];this.events[e]=this.events[e].filter(e=>e.callback!==t),0===this.events[e].length&&delete this.events[e]}}emit(e,...t){let s=!1;if(this.events[e]&&(s=!0,[...this.events[e]].forEach(s=>{try{s.callback(...t)}catch(t){console.error(`EventBus 事件 "${e}" 回调执行出错:`,t)}s.once&&this.off(e,s.callback)})),!s&&this.options.enableCache&&(this.eventCache[e]||(this.eventCache[e]=[]),this.eventCache[e].push({args:[...t],timestamp:Date.now()}),this.options.maxCacheSize>0)){const t=this.eventCache[e].length-this.options.maxCacheSize;t>0&&this.eventCache[e].splice(0,t)}}listenerCount(e){return this.events[e]?this.events[e].length:0}cacheCount(e){return this.eventCache[e]?this.eventCache[e].length:0}clear(e=!0){this.events={},e&&this.clearCache()}clearCache(e){e?delete this.eventCache[e]:this.eventCache={}}getEvents(){return Object.keys(this.events)}getCachedEvents(){return Object.keys(this.eventCache)}getOptions(){return{...this.options}}updateOptions(e){this.options={...this.options,...e}}}let t=new e;const s={install:function(s,n={}){n.eventBusOptions&&(t=new e(n.eventBusOptions)),s.provide("eventBus",t),s.config.globalProperties.$eventBus=t,n.debug&&console.log("Vue 3 EventBus 已安装",{options:n})}},n=t;export{e as EventBus,s as default,n as eventBus};
@@ -0,0 +1 @@
1
+ !function(e,t){"object"==typeof exports&&typeof module<"u"?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=typeof globalThis<"u"?globalThis:e||self).Vue3EventBus={})}(this,function(e){"use strict";class t{constructor(e={}){this.events={},this.eventCache={},this.options={enableCache:!1!==e.enableCache,maxCacheSize:e.maxCacheSize||10,clearCacheAfterSubscribe:!1!==e.clearCacheAfterSubscribe,...e}}on(e,t,s={}){if("function"!=typeof t)throw new TypeError("回调函数必须是函数类型");this.events[e]||(this.events[e]=[]);const n={callback:t,once:!!s.once};return this.events[e].push(n),this.options.enableCache&&this.eventCache[e]&&([...this.eventCache[e]].forEach(s=>{try{t(...s.args)}catch(t){console.error(`EventBus 缓存事件 "${e}" 回调执行出错:`,t)}n.once&&this.off(e,t)}),this.options.clearCacheAfterSubscribe&&delete this.eventCache[e]),()=>this.off(e,t)}once(e,t){return this.on(e,t,{once:!0})}off(e,t){if(this.events[e]){if(!t)return void delete this.events[e];this.events[e]=this.events[e].filter(e=>e.callback!==t),0===this.events[e].length&&delete this.events[e]}}emit(e,...t){let s=!1;if(this.events[e]&&(s=!0,[...this.events[e]].forEach(s=>{try{s.callback(...t)}catch(t){console.error(`EventBus 事件 "${e}" 回调执行出错:`,t)}s.once&&this.off(e,s.callback)})),!s&&this.options.enableCache&&(this.eventCache[e]||(this.eventCache[e]=[]),this.eventCache[e].push({args:[...t],timestamp:Date.now()}),this.options.maxCacheSize>0)){const t=this.eventCache[e].length-this.options.maxCacheSize;t>0&&this.eventCache[e].splice(0,t)}}listenerCount(e){return this.events[e]?this.events[e].length:0}cacheCount(e){return this.eventCache[e]?this.eventCache[e].length:0}clear(e=!0){this.events={},e&&this.clearCache()}clearCache(e){e?delete this.eventCache[e]:this.eventCache={}}getEvents(){return Object.keys(this.events)}getCachedEvents(){return Object.keys(this.eventCache)}getOptions(){return{...this.options}}updateOptions(e){this.options={...this.options,...e}}}let s=new t;const n={install:function(e,n={}){n.eventBusOptions&&(s=new t(n.eventBusOptions)),e.provide("eventBus",s),e.config.globalProperties.$eventBus=s,n.debug&&console.log("Vue 3 EventBus 已安装",{options:n})}},i=s;e.EventBus=t,e.default=n,e.eventBus=i,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
package/package.json CHANGED
@@ -1,15 +1,21 @@
1
1
  {
2
2
  "name": "vue3-event-bus-plugin",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "A simple and lightweight EventBus for Vue 3, supporting both Composition API and Option API",
5
- "main": "index.js",
6
5
  "type": "module",
7
6
  "scripts": {
8
7
  "test": "echo \"Error: no test specified\" && exit 1",
9
- "build": "echo \"Build completed\"",
8
+ "build": "vite build",
10
9
  "prepare": "npm run build"
11
10
  },
11
+ "main": "dist/vue3-event-bus-plugin.umd.js",
12
+ "module": "dist/vue3-event-bus-plugin.es.js",
13
+ "types": "dist/index.d.ts",
14
+ "files": [
15
+ "dist"
16
+ ],
12
17
  "keywords": [
18
+ "vue3-event-bus-plugin",
13
19
  "vue3",
14
20
  "eventbus",
15
21
  "vue-event-bus",
@@ -22,6 +28,9 @@
22
28
  "vue": "^3.0.0"
23
29
  },
24
30
  "devDependencies": {
31
+ "@vitejs/plugin-vue": "^6.0.3",
32
+ "rollup-plugin-terser": "^7.0.2",
33
+ "vite": "^7.3.0",
25
34
  "vue": "^3.0.0"
26
35
  },
27
36
  "repository": {
package/index.js DELETED
@@ -1,43 +0,0 @@
1
- /**
2
- * Vue 3 EventBus 主入口文件
3
- * 支持 Vue.use() 安装方式
4
- */
5
-
6
- import EventBus from './src/EventBus.js'
7
-
8
- // 创建单例实例,支持通过 install 方法传递选项
9
- let eventBusInstance = new EventBus()
10
-
11
- /**
12
- * Vue 3 插件安装函数
13
- * @param {import('vue').App} app - Vue 应用实例
14
- * @param {Object} options - 插件选项
15
- */
16
- function install(app, options = {}) {
17
- // 如果提供了 EventBus 配置,重新创建实例
18
- if (options.eventBusOptions) {
19
- eventBusInstance = new EventBus(options.eventBusOptions)
20
- }
21
-
22
- // 全局注入 eventBus,支持 Composition API
23
- app.provide('eventBus', eventBusInstance)
24
-
25
- // 添加到全局属性,支持 Option API
26
- app.config.globalProperties.$eventBus = eventBusInstance
27
-
28
- // 可选:在控制台输出安装信息
29
- if (options.debug) {
30
- console.log('Vue 3 EventBus 已安装', { options })
31
- }
32
- }
33
-
34
- // 导出插件
35
- export default {
36
- install,
37
- }
38
-
39
- // 导出单例实例,支持直接使用
40
- export const eventBus = eventBusInstance
41
-
42
- // 导出类,支持自定义实例
43
- export { EventBus }
package/src/EventBus.js DELETED
@@ -1,242 +0,0 @@
1
- /**
2
- * Vue 3 EventBus 核心实现
3
- * 支持事件订阅、发布、取消订阅等功能
4
- * 支持事件缓存,解决先执行顺序先emit后on的问题
5
- */
6
-
7
- class EventBus {
8
- constructor(options = {}) {
9
- // 存储事件订阅者
10
- this.events = {}
11
- // 存储已发布但未被订阅的事件(事件缓存)
12
- this.eventCache = {}
13
- // 配置选项
14
- this.options = {
15
- // 是否启用事件缓存
16
- enableCache: options.enableCache !== false,
17
- // 每个事件的最大缓存数量,0表示不限制
18
- maxCacheSize: options.maxCacheSize || 10,
19
- // 是否在订阅后自动清除缓存
20
- clearCacheAfterSubscribe: options.clearCacheAfterSubscribe !== false,
21
- ...options,
22
- }
23
- }
24
-
25
- /**
26
- * 订阅事件
27
- * @param {string} eventName - 事件名称
28
- * @param {Function} callback - 回调函数
29
- * @param {Object} options - 选项配置
30
- * @param {boolean} options.once - 是否只触发一次
31
- * @returns {Function} - 取消订阅的函数
32
- */
33
- on(eventName, callback, options = {}) {
34
- if (typeof callback !== 'function') {
35
- throw new TypeError('回调函数必须是函数类型')
36
- }
37
-
38
- if (!this.events[eventName]) {
39
- this.events[eventName] = []
40
- }
41
-
42
- // 创建订阅者对象
43
- const subscriber = {
44
- callback,
45
- once: !!options.once,
46
- }
47
-
48
- this.events[eventName].push(subscriber)
49
-
50
- // 检查是否有缓存的事件,如果有则触发
51
- if (this.options.enableCache && this.eventCache[eventName]) {
52
- // 复制缓存列表,防止在处理过程中修改
53
- const cachedEvents = [...this.eventCache[eventName]]
54
-
55
- // 触发所有缓存的事件
56
- cachedEvents.forEach((cached) => {
57
- try {
58
- callback(...cached.args)
59
- } catch (error) {
60
- console.error(`EventBus 缓存事件 "${eventName}" 回调执行出错:`, error)
61
- }
62
-
63
- // 如果是一次性事件,触发后移除订阅
64
- if (subscriber.once) {
65
- this.off(eventName, callback)
66
- }
67
- })
68
-
69
- // 根据配置清除缓存
70
- if (this.options.clearCacheAfterSubscribe) {
71
- delete this.eventCache[eventName]
72
- }
73
- }
74
-
75
- // 返回取消订阅的函数,方便在组件中使用
76
- return () => this.off(eventName, callback)
77
- }
78
-
79
- /**
80
- * 订阅一次事件
81
- * @param {string} eventName - 事件名称
82
- * @param {Function} callback - 回调函数
83
- * @returns {Function} - 取消订阅的函数
84
- */
85
- once(eventName, callback) {
86
- return this.on(eventName, callback, { once: true })
87
- }
88
-
89
- /**
90
- * 取消订阅事件
91
- * @param {string} eventName - 事件名称
92
- * @param {Function} [callback] - 可选的回调函数,不提供则取消该事件的所有订阅
93
- */
94
- off(eventName, callback) {
95
- if (!this.events[eventName]) {
96
- return
97
- }
98
-
99
- if (!callback) {
100
- // 取消该事件的所有订阅
101
- delete this.events[eventName]
102
- return
103
- }
104
-
105
- // 取消指定的订阅
106
- this.events[eventName] = this.events[eventName].filter(
107
- (listener) => listener.callback !== callback,
108
- )
109
-
110
- // 如果没有订阅者了,清理该事件
111
- if (this.events[eventName].length === 0) {
112
- delete this.events[eventName]
113
- }
114
- }
115
-
116
- /**
117
- * 发布/触发事件
118
- * @param {string} eventName - 事件名称
119
- * @param {...any} args - 传递给回调函数的参数
120
- */
121
- emit(eventName, ...args) {
122
- let hasListeners = false
123
-
124
- // 如果有订阅者,直接触发
125
- if (this.events[eventName]) {
126
- hasListeners = true
127
- // 复制一份订阅者列表,防止在触发过程中修改订阅列表
128
- const listeners = [...this.events[eventName]]
129
-
130
- listeners.forEach((listener) => {
131
- try {
132
- listener.callback(...args)
133
- } catch (error) {
134
- console.error(`EventBus 事件 "${eventName}" 回调执行出错:`, error)
135
- }
136
-
137
- // 如果是一次性事件,触发后自动取消订阅
138
- if (listener.once) {
139
- this.off(eventName, listener.callback)
140
- }
141
- })
142
- }
143
-
144
- // 如果没有订阅者且启用了事件缓存,将事件缓存起来
145
- if (!hasListeners && this.options.enableCache) {
146
- // 初始化缓存数组
147
- if (!this.eventCache[eventName]) {
148
- this.eventCache[eventName] = []
149
- }
150
-
151
- // 添加到缓存
152
- this.eventCache[eventName].push({
153
- args: [...args],
154
- timestamp: Date.now(),
155
- })
156
-
157
- // 限制缓存数量
158
- if (this.options.maxCacheSize > 0) {
159
- const overflow = this.eventCache[eventName].length - this.options.maxCacheSize
160
- if (overflow > 0) {
161
- // 移除最早的缓存
162
- this.eventCache[eventName].splice(0, overflow)
163
- }
164
- }
165
- }
166
- }
167
-
168
- /**
169
- * 获取指定事件的订阅者数量
170
- * @param {string} eventName - 事件名称
171
- * @returns {number} - 订阅者数量
172
- */
173
- listenerCount(eventName) {
174
- return this.events[eventName] ? this.events[eventName].length : 0
175
- }
176
-
177
- /**
178
- * 获取指定事件的缓存数量
179
- * @param {string} eventName - 事件名称
180
- * @returns {number} - 缓存事件数量
181
- */
182
- cacheCount(eventName) {
183
- return this.eventCache[eventName] ? this.eventCache[eventName].length : 0
184
- }
185
-
186
- /**
187
- * 清除所有事件订阅
188
- * @param {boolean} clearCache - 是否同时清除事件缓存
189
- */
190
- clear(clearCache = true) {
191
- this.events = {}
192
- if (clearCache) {
193
- this.clearCache()
194
- }
195
- }
196
-
197
- /**
198
- * 清除事件缓存
199
- * @param {string} [eventName] - 可选,指定要清除的事件缓存,不提供则清除所有
200
- */
201
- clearCache(eventName) {
202
- if (eventName) {
203
- delete this.eventCache[eventName]
204
- } else {
205
- this.eventCache = {}
206
- }
207
- }
208
-
209
- /**
210
- * 获取所有订阅的事件名称
211
- * @returns {string[]} - 事件名称数组
212
- */
213
- getEvents() {
214
- return Object.keys(this.events)
215
- }
216
-
217
- /**
218
- * 获取所有缓存的事件名称
219
- * @returns {string[]} - 事件名称数组
220
- */
221
- getCachedEvents() {
222
- return Object.keys(this.eventCache)
223
- }
224
-
225
- /**
226
- * 获取事件缓存配置
227
- * @returns {Object} - 配置选项
228
- */
229
- getOptions() {
230
- return { ...this.options }
231
- }
232
-
233
- /**
234
- * 更新事件缓存配置
235
- * @param {Object} newOptions - 新的配置选项
236
- */
237
- updateOptions(newOptions) {
238
- this.options = { ...this.options, ...newOptions }
239
- }
240
- }
241
-
242
- export default EventBus