@mundoit-lib/plugin-vue-event 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.
- package/dist/eventBus.cjs +40 -0
- package/dist/eventBus.d.cts +15 -0
- package/dist/eventBus.d.ts +15 -0
- package/dist/eventBus.js +33 -0
- package/dist/index.cjs +91 -0
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +83 -0
- package/dist/plugin.cjs +91 -0
- package/dist/plugin.d.cts +10 -0
- package/dist/plugin.d.ts +10 -0
- package/{src/index.js → dist/plugin.js} +33 -32
- package/package.json +38 -13
- package/src/eventBus.ts +72 -0
- package/src/index.ts +3 -0
- package/src/plugin.ts +91 -0
- package/.editorconfig +0 -12
- package/.vscode/setting.json +0 -43
- package/biome.json +0 -32
- package/dist/@mundoit-lib/plugin-vue-event.es.js +0 -73
- package/dist/@mundoit-lib/plugin-vue-event.umd.js +0 -1
- package/vite.config.js +0 -15
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var mitt = require('mitt');
|
|
4
|
+
|
|
5
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
6
|
+
|
|
7
|
+
var mitt__default = /*#__PURE__*/_interopDefault(mitt);
|
|
8
|
+
|
|
9
|
+
// src/eventBus.ts
|
|
10
|
+
var eventBus = mitt__default.default();
|
|
11
|
+
var useEventBus = () => {
|
|
12
|
+
return {
|
|
13
|
+
emit(event, ...args) {
|
|
14
|
+
eventBus.emit(event, args);
|
|
15
|
+
},
|
|
16
|
+
on(event, callback) {
|
|
17
|
+
const listener = (args) => callback(...args);
|
|
18
|
+
eventBus.on(event, listener);
|
|
19
|
+
return listener;
|
|
20
|
+
},
|
|
21
|
+
fire(event, ...args) {
|
|
22
|
+
eventBus.emit(event, args);
|
|
23
|
+
},
|
|
24
|
+
off(event, listener) {
|
|
25
|
+
eventBus.off(event, listener);
|
|
26
|
+
},
|
|
27
|
+
once(event, callback) {
|
|
28
|
+
const listener = (args) => callback(...args);
|
|
29
|
+
const wrappedListener = (args) => {
|
|
30
|
+
listener(args);
|
|
31
|
+
eventBus.off(event, wrappedListener);
|
|
32
|
+
};
|
|
33
|
+
eventBus.on(event, wrappedListener);
|
|
34
|
+
return wrappedListener;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
exports.eventBus = eventBus;
|
|
40
|
+
exports.useEventBus = useEventBus;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { EventType, Emitter, Handler } from 'mitt';
|
|
2
|
+
|
|
3
|
+
type EventBusEvents = Record<EventType, any>;
|
|
4
|
+
type UnwrappedHandler<T = any> = (...args: any[]) => T;
|
|
5
|
+
declare const eventBus: Emitter<EventBusEvents>;
|
|
6
|
+
interface EventBusInstance {
|
|
7
|
+
emit<K extends keyof EventBusEvents>(event: K, ...args: any[]): void;
|
|
8
|
+
on<K extends keyof EventBusEvents>(event: K, callback: UnwrappedHandler): Handler<EventBusEvents[K]>;
|
|
9
|
+
fire<K extends keyof EventBusEvents>(event: K, ...args: any[]): void;
|
|
10
|
+
off<K extends keyof EventBusEvents>(event: K, listener: Handler<EventBusEvents[K]>): void;
|
|
11
|
+
once<K extends keyof EventBusEvents>(event: K, callback: UnwrappedHandler): Handler<EventBusEvents[K]>;
|
|
12
|
+
}
|
|
13
|
+
declare const useEventBus: () => EventBusInstance;
|
|
14
|
+
|
|
15
|
+
export { type EventBusEvents, type EventBusInstance, eventBus, useEventBus };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { EventType, Emitter, Handler } from 'mitt';
|
|
2
|
+
|
|
3
|
+
type EventBusEvents = Record<EventType, any>;
|
|
4
|
+
type UnwrappedHandler<T = any> = (...args: any[]) => T;
|
|
5
|
+
declare const eventBus: Emitter<EventBusEvents>;
|
|
6
|
+
interface EventBusInstance {
|
|
7
|
+
emit<K extends keyof EventBusEvents>(event: K, ...args: any[]): void;
|
|
8
|
+
on<K extends keyof EventBusEvents>(event: K, callback: UnwrappedHandler): Handler<EventBusEvents[K]>;
|
|
9
|
+
fire<K extends keyof EventBusEvents>(event: K, ...args: any[]): void;
|
|
10
|
+
off<K extends keyof EventBusEvents>(event: K, listener: Handler<EventBusEvents[K]>): void;
|
|
11
|
+
once<K extends keyof EventBusEvents>(event: K, callback: UnwrappedHandler): Handler<EventBusEvents[K]>;
|
|
12
|
+
}
|
|
13
|
+
declare const useEventBus: () => EventBusInstance;
|
|
14
|
+
|
|
15
|
+
export { type EventBusEvents, type EventBusInstance, eventBus, useEventBus };
|
package/dist/eventBus.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import mitt from 'mitt';
|
|
2
|
+
|
|
3
|
+
// src/eventBus.ts
|
|
4
|
+
var eventBus = mitt();
|
|
5
|
+
var useEventBus = () => {
|
|
6
|
+
return {
|
|
7
|
+
emit(event, ...args) {
|
|
8
|
+
eventBus.emit(event, args);
|
|
9
|
+
},
|
|
10
|
+
on(event, callback) {
|
|
11
|
+
const listener = (args) => callback(...args);
|
|
12
|
+
eventBus.on(event, listener);
|
|
13
|
+
return listener;
|
|
14
|
+
},
|
|
15
|
+
fire(event, ...args) {
|
|
16
|
+
eventBus.emit(event, args);
|
|
17
|
+
},
|
|
18
|
+
off(event, listener) {
|
|
19
|
+
eventBus.off(event, listener);
|
|
20
|
+
},
|
|
21
|
+
once(event, callback) {
|
|
22
|
+
const listener = (args) => callback(...args);
|
|
23
|
+
const wrappedListener = (args) => {
|
|
24
|
+
listener(args);
|
|
25
|
+
eventBus.off(event, wrappedListener);
|
|
26
|
+
};
|
|
27
|
+
eventBus.on(event, wrappedListener);
|
|
28
|
+
return wrappedListener;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export { eventBus, useEventBus };
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var mitt = require('mitt');
|
|
4
|
+
var vueDemi = require('vue-demi');
|
|
5
|
+
|
|
6
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
7
|
+
|
|
8
|
+
var mitt__default = /*#__PURE__*/_interopDefault(mitt);
|
|
9
|
+
|
|
10
|
+
// src/eventBus.ts
|
|
11
|
+
var eventBus = mitt__default.default();
|
|
12
|
+
var useEventBus = () => {
|
|
13
|
+
return {
|
|
14
|
+
emit(event, ...args) {
|
|
15
|
+
eventBus.emit(event, args);
|
|
16
|
+
},
|
|
17
|
+
on(event, callback) {
|
|
18
|
+
const listener = (args) => callback(...args);
|
|
19
|
+
eventBus.on(event, listener);
|
|
20
|
+
return listener;
|
|
21
|
+
},
|
|
22
|
+
fire(event, ...args) {
|
|
23
|
+
eventBus.emit(event, args);
|
|
24
|
+
},
|
|
25
|
+
off(event, listener) {
|
|
26
|
+
eventBus.off(event, listener);
|
|
27
|
+
},
|
|
28
|
+
once(event, callback) {
|
|
29
|
+
const listener = (args) => callback(...args);
|
|
30
|
+
const wrappedListener = (args) => {
|
|
31
|
+
listener(args);
|
|
32
|
+
eventBus.off(event, wrappedListener);
|
|
33
|
+
};
|
|
34
|
+
eventBus.on(event, wrappedListener);
|
|
35
|
+
return wrappedListener;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
function plugin(VueOrApp) {
|
|
40
|
+
const bus = useEventBus();
|
|
41
|
+
if (vueDemi.isVue3) {
|
|
42
|
+
const app = VueOrApp;
|
|
43
|
+
app.config.globalProperties.$events = bus;
|
|
44
|
+
app.mixin({
|
|
45
|
+
beforeCreate() {
|
|
46
|
+
if (typeof this.$options.events !== "object") return;
|
|
47
|
+
this._eventListeners = [];
|
|
48
|
+
for (const [event, handler] of Object.entries(this.$options.events)) {
|
|
49
|
+
const boundHandler = handler.bind(this);
|
|
50
|
+
const listener = bus.on(event, boundHandler);
|
|
51
|
+
this._eventListeners.push({ event, listener });
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
beforeUnmount() {
|
|
55
|
+
if (!this._eventListeners) return;
|
|
56
|
+
for (const { event, listener } of this._eventListeners) {
|
|
57
|
+
bus.off(event, listener);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
} else if (vueDemi.isVue2) {
|
|
62
|
+
const Vue = VueOrApp;
|
|
63
|
+
Object.defineProperty(Vue.prototype, "$events", {
|
|
64
|
+
get() {
|
|
65
|
+
return bus;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
Vue.mixin({
|
|
69
|
+
beforeCreate() {
|
|
70
|
+
if (typeof this.$options.events !== "object") return;
|
|
71
|
+
this._eventListeners = [];
|
|
72
|
+
for (const [event, handler] of Object.entries(this.$options.events)) {
|
|
73
|
+
const boundHandler = handler.bind(this);
|
|
74
|
+
const listener = bus.on(event, boundHandler);
|
|
75
|
+
this._eventListeners.push({ event, listener });
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
beforeDestroy() {
|
|
79
|
+
if (!this._eventListeners) return;
|
|
80
|
+
for (const { event, listener } of this._eventListeners) {
|
|
81
|
+
bus.off(event, listener);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
var plugin_default = plugin;
|
|
88
|
+
|
|
89
|
+
exports.eventBus = eventBus;
|
|
90
|
+
exports.eventsPlugin = plugin_default;
|
|
91
|
+
exports.useEventBus = useEventBus;
|
package/dist/index.d.cts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import mitt from 'mitt';
|
|
2
|
+
import { isVue3, isVue2 } from 'vue-demi';
|
|
3
|
+
|
|
4
|
+
// src/eventBus.ts
|
|
5
|
+
var eventBus = mitt();
|
|
6
|
+
var useEventBus = () => {
|
|
7
|
+
return {
|
|
8
|
+
emit(event, ...args) {
|
|
9
|
+
eventBus.emit(event, args);
|
|
10
|
+
},
|
|
11
|
+
on(event, callback) {
|
|
12
|
+
const listener = (args) => callback(...args);
|
|
13
|
+
eventBus.on(event, listener);
|
|
14
|
+
return listener;
|
|
15
|
+
},
|
|
16
|
+
fire(event, ...args) {
|
|
17
|
+
eventBus.emit(event, args);
|
|
18
|
+
},
|
|
19
|
+
off(event, listener) {
|
|
20
|
+
eventBus.off(event, listener);
|
|
21
|
+
},
|
|
22
|
+
once(event, callback) {
|
|
23
|
+
const listener = (args) => callback(...args);
|
|
24
|
+
const wrappedListener = (args) => {
|
|
25
|
+
listener(args);
|
|
26
|
+
eventBus.off(event, wrappedListener);
|
|
27
|
+
};
|
|
28
|
+
eventBus.on(event, wrappedListener);
|
|
29
|
+
return wrappedListener;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
function plugin(VueOrApp) {
|
|
34
|
+
const bus = useEventBus();
|
|
35
|
+
if (isVue3) {
|
|
36
|
+
const app = VueOrApp;
|
|
37
|
+
app.config.globalProperties.$events = bus;
|
|
38
|
+
app.mixin({
|
|
39
|
+
beforeCreate() {
|
|
40
|
+
if (typeof this.$options.events !== "object") return;
|
|
41
|
+
this._eventListeners = [];
|
|
42
|
+
for (const [event, handler] of Object.entries(this.$options.events)) {
|
|
43
|
+
const boundHandler = handler.bind(this);
|
|
44
|
+
const listener = bus.on(event, boundHandler);
|
|
45
|
+
this._eventListeners.push({ event, listener });
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
beforeUnmount() {
|
|
49
|
+
if (!this._eventListeners) return;
|
|
50
|
+
for (const { event, listener } of this._eventListeners) {
|
|
51
|
+
bus.off(event, listener);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
} else if (isVue2) {
|
|
56
|
+
const Vue = VueOrApp;
|
|
57
|
+
Object.defineProperty(Vue.prototype, "$events", {
|
|
58
|
+
get() {
|
|
59
|
+
return bus;
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
Vue.mixin({
|
|
63
|
+
beforeCreate() {
|
|
64
|
+
if (typeof this.$options.events !== "object") return;
|
|
65
|
+
this._eventListeners = [];
|
|
66
|
+
for (const [event, handler] of Object.entries(this.$options.events)) {
|
|
67
|
+
const boundHandler = handler.bind(this);
|
|
68
|
+
const listener = bus.on(event, boundHandler);
|
|
69
|
+
this._eventListeners.push({ event, listener });
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
beforeDestroy() {
|
|
73
|
+
if (!this._eventListeners) return;
|
|
74
|
+
for (const { event, listener } of this._eventListeners) {
|
|
75
|
+
bus.off(event, listener);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
var plugin_default = plugin;
|
|
82
|
+
|
|
83
|
+
export { eventBus, plugin_default as eventsPlugin, useEventBus };
|
package/dist/plugin.cjs
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var vueDemi = require('vue-demi');
|
|
4
|
+
var mitt = require('mitt');
|
|
5
|
+
|
|
6
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
7
|
+
|
|
8
|
+
var mitt__default = /*#__PURE__*/_interopDefault(mitt);
|
|
9
|
+
|
|
10
|
+
// src/plugin.ts
|
|
11
|
+
var eventBus = mitt__default.default();
|
|
12
|
+
var useEventBus = () => {
|
|
13
|
+
return {
|
|
14
|
+
emit(event, ...args) {
|
|
15
|
+
eventBus.emit(event, args);
|
|
16
|
+
},
|
|
17
|
+
on(event, callback) {
|
|
18
|
+
const listener = (args) => callback(...args);
|
|
19
|
+
eventBus.on(event, listener);
|
|
20
|
+
return listener;
|
|
21
|
+
},
|
|
22
|
+
fire(event, ...args) {
|
|
23
|
+
eventBus.emit(event, args);
|
|
24
|
+
},
|
|
25
|
+
off(event, listener) {
|
|
26
|
+
eventBus.off(event, listener);
|
|
27
|
+
},
|
|
28
|
+
once(event, callback) {
|
|
29
|
+
const listener = (args) => callback(...args);
|
|
30
|
+
const wrappedListener = (args) => {
|
|
31
|
+
listener(args);
|
|
32
|
+
eventBus.off(event, wrappedListener);
|
|
33
|
+
};
|
|
34
|
+
eventBus.on(event, wrappedListener);
|
|
35
|
+
return wrappedListener;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// src/plugin.ts
|
|
41
|
+
function plugin(VueOrApp) {
|
|
42
|
+
const bus = useEventBus();
|
|
43
|
+
if (vueDemi.isVue3) {
|
|
44
|
+
const app = VueOrApp;
|
|
45
|
+
app.config.globalProperties.$events = bus;
|
|
46
|
+
app.mixin({
|
|
47
|
+
beforeCreate() {
|
|
48
|
+
if (typeof this.$options.events !== "object") return;
|
|
49
|
+
this._eventListeners = [];
|
|
50
|
+
for (const [event, handler] of Object.entries(this.$options.events)) {
|
|
51
|
+
const boundHandler = handler.bind(this);
|
|
52
|
+
const listener = bus.on(event, boundHandler);
|
|
53
|
+
this._eventListeners.push({ event, listener });
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
beforeUnmount() {
|
|
57
|
+
if (!this._eventListeners) return;
|
|
58
|
+
for (const { event, listener } of this._eventListeners) {
|
|
59
|
+
bus.off(event, listener);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
} else if (vueDemi.isVue2) {
|
|
64
|
+
const Vue = VueOrApp;
|
|
65
|
+
Object.defineProperty(Vue.prototype, "$events", {
|
|
66
|
+
get() {
|
|
67
|
+
return bus;
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
Vue.mixin({
|
|
71
|
+
beforeCreate() {
|
|
72
|
+
if (typeof this.$options.events !== "object") return;
|
|
73
|
+
this._eventListeners = [];
|
|
74
|
+
for (const [event, handler] of Object.entries(this.$options.events)) {
|
|
75
|
+
const boundHandler = handler.bind(this);
|
|
76
|
+
const listener = bus.on(event, boundHandler);
|
|
77
|
+
this._eventListeners.push({ event, listener });
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
beforeDestroy() {
|
|
81
|
+
if (!this._eventListeners) return;
|
|
82
|
+
for (const { event, listener } of this._eventListeners) {
|
|
83
|
+
bus.off(event, listener);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
var plugin_default = plugin;
|
|
90
|
+
|
|
91
|
+
module.exports = plugin_default;
|
package/dist/plugin.d.ts
ADDED
|
@@ -1,17 +1,12 @@
|
|
|
1
|
+
import { isVue3, isVue2 } from 'vue-demi';
|
|
1
2
|
import mitt from 'mitt';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
isVue3 = true;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const eventBus = mitt();
|
|
11
|
-
|
|
12
|
-
const bus = {
|
|
4
|
+
// src/plugin.ts
|
|
5
|
+
var eventBus = mitt();
|
|
6
|
+
var useEventBus = () => {
|
|
7
|
+
return {
|
|
13
8
|
emit(event, ...args) {
|
|
14
|
-
|
|
9
|
+
eventBus.emit(event, args);
|
|
15
10
|
},
|
|
16
11
|
on(event, callback) {
|
|
17
12
|
const listener = (args) => callback(...args);
|
|
@@ -24,61 +19,67 @@ function plugin(VueOrApp) {
|
|
|
24
19
|
off(event, listener) {
|
|
25
20
|
eventBus.off(event, listener);
|
|
26
21
|
},
|
|
22
|
+
once(event, callback) {
|
|
23
|
+
const listener = (args) => callback(...args);
|
|
24
|
+
const wrappedListener = (args) => {
|
|
25
|
+
listener(args);
|
|
26
|
+
eventBus.off(event, wrappedListener);
|
|
27
|
+
};
|
|
28
|
+
eventBus.on(event, wrappedListener);
|
|
29
|
+
return wrappedListener;
|
|
30
|
+
}
|
|
27
31
|
};
|
|
32
|
+
};
|
|
28
33
|
|
|
34
|
+
// src/plugin.ts
|
|
35
|
+
function plugin(VueOrApp) {
|
|
36
|
+
const bus = useEventBus();
|
|
29
37
|
if (isVue3) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
38
|
+
const app = VueOrApp;
|
|
39
|
+
app.config.globalProperties.$events = bus;
|
|
40
|
+
app.mixin({
|
|
33
41
|
beforeCreate() {
|
|
34
|
-
if (typeof this.$options.events !==
|
|
35
|
-
|
|
42
|
+
if (typeof this.$options.events !== "object") return;
|
|
36
43
|
this._eventListeners = [];
|
|
37
|
-
|
|
38
44
|
for (const [event, handler] of Object.entries(this.$options.events)) {
|
|
39
45
|
const boundHandler = handler.bind(this);
|
|
40
46
|
const listener = bus.on(event, boundHandler);
|
|
41
47
|
this._eventListeners.push({ event, listener });
|
|
42
48
|
}
|
|
43
49
|
},
|
|
44
|
-
|
|
45
50
|
beforeUnmount() {
|
|
46
51
|
if (!this._eventListeners) return;
|
|
47
|
-
|
|
48
52
|
for (const { event, listener } of this._eventListeners) {
|
|
49
53
|
bus.off(event, listener);
|
|
50
54
|
}
|
|
51
|
-
}
|
|
55
|
+
}
|
|
52
56
|
});
|
|
53
|
-
} else {
|
|
54
|
-
|
|
57
|
+
} else if (isVue2) {
|
|
58
|
+
const Vue = VueOrApp;
|
|
59
|
+
Object.defineProperty(Vue.prototype, "$events", {
|
|
55
60
|
get() {
|
|
56
61
|
return bus;
|
|
57
|
-
}
|
|
62
|
+
}
|
|
58
63
|
});
|
|
59
|
-
|
|
60
|
-
VueOrApp.mixin({
|
|
64
|
+
Vue.mixin({
|
|
61
65
|
beforeCreate() {
|
|
62
|
-
if (typeof this.$options.events !==
|
|
63
|
-
|
|
66
|
+
if (typeof this.$options.events !== "object") return;
|
|
64
67
|
this._eventListeners = [];
|
|
65
|
-
|
|
66
68
|
for (const [event, handler] of Object.entries(this.$options.events)) {
|
|
67
69
|
const boundHandler = handler.bind(this);
|
|
68
70
|
const listener = bus.on(event, boundHandler);
|
|
69
71
|
this._eventListeners.push({ event, listener });
|
|
70
72
|
}
|
|
71
73
|
},
|
|
72
|
-
|
|
73
74
|
beforeDestroy() {
|
|
74
75
|
if (!this._eventListeners) return;
|
|
75
|
-
|
|
76
76
|
for (const { event, listener } of this._eventListeners) {
|
|
77
77
|
bus.off(event, listener);
|
|
78
78
|
}
|
|
79
|
-
}
|
|
79
|
+
}
|
|
80
80
|
});
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
|
+
var plugin_default = plugin;
|
|
83
84
|
|
|
84
|
-
export default
|
|
85
|
+
export { plugin_default as default };
|
package/package.json
CHANGED
|
@@ -1,17 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mundoit-lib/plugin-vue-event",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Vue 3 plugin to handle global events between components, inspired by vue-events. This plugin provides a simple and efficient event bus system, allowing to emit, listen and unregister events in an intuitive way. Compatible with Vue 2 and Vue 3.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"main": "./dist
|
|
8
|
-
"module": "./dist
|
|
7
|
+
"main": "./dist/index.cjs",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
9
10
|
"exports": {
|
|
10
11
|
".": {
|
|
11
|
-
"
|
|
12
|
-
"
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"require": "./dist/index.cjs"
|
|
15
|
+
},
|
|
16
|
+
"./plugin": {
|
|
17
|
+
"types": "./dist/plugin.d.ts",
|
|
18
|
+
"import": "./dist/plugin.js",
|
|
19
|
+
"require": "./dist/plugin.cjs"
|
|
20
|
+
},
|
|
21
|
+
"./eventBus": {
|
|
22
|
+
"types": "./dist/eventBus.d.ts",
|
|
23
|
+
"import": "./dist/eventBus.js",
|
|
24
|
+
"require": "./dist/eventBus.cjs"
|
|
13
25
|
}
|
|
14
26
|
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"src"
|
|
30
|
+
],
|
|
15
31
|
"repository": {
|
|
16
32
|
"type": "git",
|
|
17
33
|
"url": "https://github.com/mundoit-lib/plugin-vue-event"
|
|
@@ -39,18 +55,27 @@
|
|
|
39
55
|
],
|
|
40
56
|
"author": "@mundoit-lib",
|
|
41
57
|
"scripts": {
|
|
42
|
-
"
|
|
43
|
-
"build": "
|
|
44
|
-
"
|
|
58
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
59
|
+
"build": "tsup",
|
|
60
|
+
"clean": "rm -rf dist && rm -rf node_modules",
|
|
61
|
+
"postinstall": "npx vue-demi-fix"
|
|
45
62
|
},
|
|
46
63
|
"dependencies": {
|
|
47
|
-
"mitt": "^3.0.1"
|
|
64
|
+
"mitt": "^3.0.1",
|
|
65
|
+
"vue-demi": "0.14.10"
|
|
48
66
|
},
|
|
49
67
|
"devDependencies": {
|
|
50
|
-
"@
|
|
51
|
-
"
|
|
68
|
+
"@types/node": "^22.19.1",
|
|
69
|
+
"tsup": "^8.5.1",
|
|
70
|
+
"typescript": "5.9.3"
|
|
71
|
+
},
|
|
72
|
+
"peerDependencies": {
|
|
73
|
+
"@vue/composition-api": "^1.0.0-rc.1",
|
|
74
|
+
"vue": "^2.0.0 || >=3.0.0"
|
|
52
75
|
},
|
|
53
|
-
"
|
|
54
|
-
"@
|
|
76
|
+
"peerDependenciesMeta": {
|
|
77
|
+
"@vue/composition-api": {
|
|
78
|
+
"optional": true
|
|
79
|
+
}
|
|
55
80
|
}
|
|
56
81
|
}
|
package/src/eventBus.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import mitt, { type Emitter, type EventType, type Handler } from 'mitt';
|
|
2
|
+
|
|
3
|
+
// Define el tipo de eventos de tu aplicación
|
|
4
|
+
// Puedes extender este tipo según tus necesidades
|
|
5
|
+
export type EventBusEvents = Record<EventType, any>;
|
|
6
|
+
|
|
7
|
+
// Tipos para los listeners
|
|
8
|
+
type EventListener<T = any> = (args: T) => void;
|
|
9
|
+
type UnwrappedHandler<T = any> = (...args: any[]) => T;
|
|
10
|
+
|
|
11
|
+
// Exportamos una única instancia del event bus
|
|
12
|
+
export const eventBus: Emitter<EventBusEvents> = mitt<EventBusEvents>();
|
|
13
|
+
|
|
14
|
+
// Interface para el retorno de useEventBus
|
|
15
|
+
export interface EventBusInstance {
|
|
16
|
+
emit<K extends keyof EventBusEvents>(event: K, ...args: any[]): void;
|
|
17
|
+
on<K extends keyof EventBusEvents>(
|
|
18
|
+
event: K,
|
|
19
|
+
callback: UnwrappedHandler
|
|
20
|
+
): Handler<EventBusEvents[K]>;
|
|
21
|
+
fire<K extends keyof EventBusEvents>(event: K, ...args: any[]): void;
|
|
22
|
+
off<K extends keyof EventBusEvents>(
|
|
23
|
+
event: K,
|
|
24
|
+
listener: Handler<EventBusEvents[K]>
|
|
25
|
+
): void;
|
|
26
|
+
once<K extends keyof EventBusEvents>(
|
|
27
|
+
event: K,
|
|
28
|
+
callback: UnwrappedHandler
|
|
29
|
+
): Handler<EventBusEvents[K]>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Exportamos helpers para usar en composables o archivos JS/TS
|
|
33
|
+
export const useEventBus = (): EventBusInstance => {
|
|
34
|
+
return {
|
|
35
|
+
emit<K extends keyof EventBusEvents>(event: K, ...args: any[]): void {
|
|
36
|
+
eventBus.emit(event, args);
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
on<K extends keyof EventBusEvents>(
|
|
40
|
+
event: K,
|
|
41
|
+
callback: UnwrappedHandler
|
|
42
|
+
): Handler<EventBusEvents[K]> {
|
|
43
|
+
const listener: Handler<EventBusEvents[K]> = (args: any) => callback(...args);
|
|
44
|
+
eventBus.on(event, listener);
|
|
45
|
+
return listener;
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
fire<K extends keyof EventBusEvents>(event: K, ...args: any[]): void {
|
|
49
|
+
eventBus.emit(event, args);
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
off<K extends keyof EventBusEvents>(
|
|
53
|
+
event: K,
|
|
54
|
+
listener: Handler<EventBusEvents[K]>
|
|
55
|
+
): void {
|
|
56
|
+
eventBus.off(event, listener);
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
once<K extends keyof EventBusEvents>(
|
|
60
|
+
event: K,
|
|
61
|
+
callback: UnwrappedHandler
|
|
62
|
+
): Handler<EventBusEvents[K]> {
|
|
63
|
+
const listener: Handler<EventBusEvents[K]> = (args: any) => callback(...args);
|
|
64
|
+
const wrappedListener: Handler<EventBusEvents[K]> = (args: any) => {
|
|
65
|
+
listener(args);
|
|
66
|
+
eventBus.off(event, wrappedListener);
|
|
67
|
+
};
|
|
68
|
+
eventBus.on(event, wrappedListener);
|
|
69
|
+
return wrappedListener;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
};
|
package/src/index.ts
ADDED
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { isVue2, isVue3 } from 'vue-demi';
|
|
2
|
+
import type { App, ComponentOptions } from 'vue-demi';
|
|
3
|
+
import { useEventBus, type EventBusInstance } from './eventBus';
|
|
4
|
+
|
|
5
|
+
// Tipos para Vue 2
|
|
6
|
+
interface Vue2Constructor {
|
|
7
|
+
prototype: any;
|
|
8
|
+
mixin: (options: any) => void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// Tipos para las opciones de componentes extendidas
|
|
12
|
+
interface ComponentWithEvents extends ComponentOptions {
|
|
13
|
+
events?: Record<string, (...args: any[]) => void>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Tipo interno para los listeners almacenados
|
|
17
|
+
interface StoredListener {
|
|
18
|
+
event: string;
|
|
19
|
+
listener: any;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Extensión de tipos para Vue 2
|
|
23
|
+
interface Vue2Instance {
|
|
24
|
+
$options: ComponentWithEvents;
|
|
25
|
+
_eventListeners?: StoredListener[];
|
|
26
|
+
$events?: EventBusInstance;
|
|
27
|
+
}
|
|
28
|
+
// Sobrecarga de tipos para el plugin
|
|
29
|
+
function plugin(app: App): void;
|
|
30
|
+
function plugin(Vue: Vue2Constructor): void;
|
|
31
|
+
function plugin(VueOrApp: App | Vue2Constructor): void {
|
|
32
|
+
const bus = useEventBus();
|
|
33
|
+
|
|
34
|
+
if (isVue3) {
|
|
35
|
+
// Vue 3
|
|
36
|
+
const app = VueOrApp as App;
|
|
37
|
+
app.config.globalProperties.$events = bus;
|
|
38
|
+
|
|
39
|
+
app.mixin({
|
|
40
|
+
beforeCreate(this: any) {
|
|
41
|
+
if (typeof this.$options.events !== 'object') return;
|
|
42
|
+
this._eventListeners = [] as StoredListener[];
|
|
43
|
+
|
|
44
|
+
for (const [event, handler] of Object.entries(this.$options.events)) {
|
|
45
|
+
// @ts-ignore
|
|
46
|
+
const boundHandler = handler.bind(this);
|
|
47
|
+
const listener = bus.on(event, boundHandler);
|
|
48
|
+
this._eventListeners.push({ event, listener });
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
beforeUnmount(this: any) {
|
|
52
|
+
if (!this._eventListeners) return;
|
|
53
|
+
|
|
54
|
+
for (const { event, listener } of this._eventListeners as StoredListener[]) {
|
|
55
|
+
bus.off(event, listener);
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
} else if (isVue2) {
|
|
60
|
+
// Vue 2
|
|
61
|
+
const Vue = VueOrApp as Vue2Constructor;
|
|
62
|
+
|
|
63
|
+
Object.defineProperty(Vue.prototype, '$events', {
|
|
64
|
+
get() {
|
|
65
|
+
return bus;
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
Vue.mixin({
|
|
70
|
+
beforeCreate(this: Vue2Instance) {
|
|
71
|
+
if (typeof this.$options.events !== 'object') return;
|
|
72
|
+
this._eventListeners = [];
|
|
73
|
+
|
|
74
|
+
for (const [event, handler] of Object.entries(this.$options.events)) {
|
|
75
|
+
const boundHandler = handler.bind(this);
|
|
76
|
+
const listener = bus.on(event, boundHandler);
|
|
77
|
+
this._eventListeners.push({ event, listener });
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
beforeDestroy(this: Vue2Instance) {
|
|
81
|
+
if (!this._eventListeners) return;
|
|
82
|
+
|
|
83
|
+
for (const { event, listener } of this._eventListeners) {
|
|
84
|
+
bus.off(event, listener);
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export default plugin;
|
package/.editorconfig
DELETED
package/.vscode/setting.json
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"[javascript]": {
|
|
3
|
-
"editor.defaultFormatter": "biomejs.biome"
|
|
4
|
-
},
|
|
5
|
-
"biomejs.biome": {
|
|
6
|
-
"formatOnSave": true,
|
|
7
|
-
"formatOnPaste": true,
|
|
8
|
-
"formatOnType": true,
|
|
9
|
-
"formatOnSaveTimeout": 5000,
|
|
10
|
-
"formatOnPasteTimeout": 5000,
|
|
11
|
-
"formatOnTypeTimeout": 5000,
|
|
12
|
-
"formatOnSaveOptions": {
|
|
13
|
-
"tabSize": 2,
|
|
14
|
-
"useTabs": false,
|
|
15
|
-
"semi": true,
|
|
16
|
-
"singleQuote": false,
|
|
17
|
-
"trailingComma": "none",
|
|
18
|
-
"bracketSpacing": true,
|
|
19
|
-
"arrowParens": "always",
|
|
20
|
-
"printWidth": 80
|
|
21
|
-
},
|
|
22
|
-
"formatOnPasteOptions": {
|
|
23
|
-
"tabSize": 2,
|
|
24
|
-
"useTabs": false,
|
|
25
|
-
"semi": true,
|
|
26
|
-
"singleQuote": false,
|
|
27
|
-
"trailingComma": "none",
|
|
28
|
-
"bracketSpacing": true,
|
|
29
|
-
"arrowParens": "always",
|
|
30
|
-
"printWidth": 80
|
|
31
|
-
},
|
|
32
|
-
"formatOnTypeOptions": {
|
|
33
|
-
"tabSize": 2,
|
|
34
|
-
"useTabs": false,
|
|
35
|
-
"semi": true,
|
|
36
|
-
"singleQuote": false,
|
|
37
|
-
"trailingComma": "none",
|
|
38
|
-
"bracketSpacing": true,
|
|
39
|
-
"arrowParens": "always",
|
|
40
|
-
"printWidth": 80
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
package/biome.json
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
|
|
3
|
-
"vcs": {
|
|
4
|
-
"enabled": true,
|
|
5
|
-
"clientKind": "git",
|
|
6
|
-
"useIgnoreFile": false
|
|
7
|
-
},
|
|
8
|
-
"files": {
|
|
9
|
-
"ignoreUnknown": true,
|
|
10
|
-
"ignore": ["/node_modules", "/dist", "/.git", "/.github", "/.vscode"]
|
|
11
|
-
},
|
|
12
|
-
"formatter": {
|
|
13
|
-
"enabled": true,
|
|
14
|
-
"formatWithErrors": true,
|
|
15
|
-
"indentStyle": "space",
|
|
16
|
-
"useEditorconfig": true
|
|
17
|
-
},
|
|
18
|
-
"organizeImports": {
|
|
19
|
-
"enabled": true
|
|
20
|
-
},
|
|
21
|
-
"linter": {
|
|
22
|
-
"enabled": true,
|
|
23
|
-
"rules": {
|
|
24
|
-
"recommended": true
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
|
-
"javascript": {
|
|
28
|
-
"formatter": {
|
|
29
|
-
"quoteStyle": "double"
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
function c(n) {
|
|
2
|
-
return { all: n = n || /* @__PURE__ */ new Map(), on: function(o, i) {
|
|
3
|
-
var t = n.get(o);
|
|
4
|
-
t ? t.push(i) : n.set(o, [i]);
|
|
5
|
-
}, off: function(o, i) {
|
|
6
|
-
var t = n.get(o);
|
|
7
|
-
t && (i ? t.splice(t.indexOf(i) >>> 0, 1) : n.set(o, []));
|
|
8
|
-
}, emit: function(o, i) {
|
|
9
|
-
var t = n.get(o);
|
|
10
|
-
t && t.slice().map(function(e) {
|
|
11
|
-
e(i);
|
|
12
|
-
}), (t = n.get("*")) && t.slice().map(function(e) {
|
|
13
|
-
e(o, i);
|
|
14
|
-
});
|
|
15
|
-
} };
|
|
16
|
-
}
|
|
17
|
-
function v(n) {
|
|
18
|
-
let o = !1;
|
|
19
|
-
typeof n.version == "string" && n.version.startsWith("3.") && (o = !0);
|
|
20
|
-
const i = c(), t = {
|
|
21
|
-
emit(e, ...s) {
|
|
22
|
-
i.emit(e, s);
|
|
23
|
-
},
|
|
24
|
-
on(e, s) {
|
|
25
|
-
const r = (f) => s(...f);
|
|
26
|
-
return i.on(e, r), r;
|
|
27
|
-
},
|
|
28
|
-
fire(e, ...s) {
|
|
29
|
-
i.emit(e, s);
|
|
30
|
-
},
|
|
31
|
-
off(e, s) {
|
|
32
|
-
i.off(e, s);
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
o ? (n.config.globalProperties.$events = t, n.mixin({
|
|
36
|
-
beforeCreate() {
|
|
37
|
-
if (typeof this.$options.events == "object") {
|
|
38
|
-
this._eventListeners = [];
|
|
39
|
-
for (const [e, s] of Object.entries(this.$options.events)) {
|
|
40
|
-
const r = s.bind(this), f = t.on(e, r);
|
|
41
|
-
this._eventListeners.push({ event: e, listener: f });
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
},
|
|
45
|
-
beforeUnmount() {
|
|
46
|
-
if (this._eventListeners)
|
|
47
|
-
for (const { event: e, listener: s } of this._eventListeners)
|
|
48
|
-
t.off(e, s);
|
|
49
|
-
}
|
|
50
|
-
})) : (Object.defineProperty(n.prototype, "$events", {
|
|
51
|
-
get() {
|
|
52
|
-
return t;
|
|
53
|
-
}
|
|
54
|
-
}), n.mixin({
|
|
55
|
-
beforeCreate() {
|
|
56
|
-
if (typeof this.$options.events == "object") {
|
|
57
|
-
this._eventListeners = [];
|
|
58
|
-
for (const [e, s] of Object.entries(this.$options.events)) {
|
|
59
|
-
const r = s.bind(this), f = t.on(e, r);
|
|
60
|
-
this._eventListeners.push({ event: e, listener: f });
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
},
|
|
64
|
-
beforeDestroy() {
|
|
65
|
-
if (this._eventListeners)
|
|
66
|
-
for (const { event: e, listener: s } of this._eventListeners)
|
|
67
|
-
t.off(e, s);
|
|
68
|
-
}
|
|
69
|
-
}));
|
|
70
|
-
}
|
|
71
|
-
export {
|
|
72
|
-
v as default
|
|
73
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
(function(f,u){typeof exports=="object"&&typeof module<"u"?module.exports=u():typeof define=="function"&&define.amd?define(u):(f=typeof globalThis<"u"?globalThis:f||self,f["@mundoit-lib/plugin-vue-event"]=u())})(this,function(){"use strict";function f(n){return{all:n=n||new Map,on:function(o,s){var t=n.get(o);t?t.push(s):n.set(o,[s])},off:function(o,s){var t=n.get(o);t&&(s?t.splice(t.indexOf(s)>>>0,1):n.set(o,[]))},emit:function(o,s){var t=n.get(o);t&&t.slice().map(function(e){e(s)}),(t=n.get("*"))&&t.slice().map(function(e){e(o,s)})}}}function u(n){let o=!1;typeof n.version=="string"&&n.version.startsWith("3.")&&(o=!0);const s=f(),t={emit(e,...i){s.emit(e,i)},on(e,i){const r=c=>i(...c);return s.on(e,r),r},fire(e,...i){s.emit(e,i)},off(e,i){s.off(e,i)}};o?(n.config.globalProperties.$events=t,n.mixin({beforeCreate(){if(typeof this.$options.events=="object"){this._eventListeners=[];for(const[e,i]of Object.entries(this.$options.events)){const r=i.bind(this),c=t.on(e,r);this._eventListeners.push({event:e,listener:c})}}},beforeUnmount(){if(this._eventListeners)for(const{event:e,listener:i}of this._eventListeners)t.off(e,i)}})):(Object.defineProperty(n.prototype,"$events",{get(){return t}}),n.mixin({beforeCreate(){if(typeof this.$options.events=="object"){this._eventListeners=[];for(const[e,i]of Object.entries(this.$options.events)){const r=i.bind(this),c=t.on(e,r);this._eventListeners.push({event:e,listener:c})}}},beforeDestroy(){if(this._eventListeners)for(const{event:e,listener:i}of this._eventListeners)t.off(e,i)}}))}return u});
|
package/vite.config.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
import { defineConfig } from 'vite';
|
|
3
|
-
|
|
4
|
-
const packagename = '@mundoit-lib/plugin-vue-event';
|
|
5
|
-
|
|
6
|
-
export default defineConfig({
|
|
7
|
-
build: {
|
|
8
|
-
lib: {
|
|
9
|
-
entry: path.resolve(__dirname, 'src/index.js'),
|
|
10
|
-
formats: ['es', 'umd'],
|
|
11
|
-
name: packagename,
|
|
12
|
-
fileName: (format) => `${packagename}.${format}.js`,
|
|
13
|
-
},
|
|
14
|
-
},
|
|
15
|
-
});
|