|
| 1 | +//const GlobalEventBus = new StoreEventBus(); |
| 2 | +// |
| 3 | +//export const EventBusPlugin: Plugin = { |
| 4 | +// install(app: App, ...options: any[]) { |
| 5 | +// app.config.globalProperties.$events = GlobalEventBus; |
| 6 | +// app.config.globalProperties.$emitEvent = GlobalEventBus.emit.bind(GlobalEventBus); |
| 7 | +// } |
| 8 | +//}; |
| 9 | +//export function useEvent<Key extends keyof StoreEventsMap>(type: Key, handler: Handler<StoreEventsMap[Key]>): void; |
| 10 | +//export function useEvent(type: "*", handler: WildcardHandler<StoreEventsMap>): void; |
| 11 | +//export function useEvent(type, handler): void { |
| 12 | +// const scope = getCurrentScope(); |
| 13 | +// if (!scope) { |
| 14 | +// throw new Error('No scope found. Are you using the plugin outside of a Vue component/setup?'); |
| 15 | +// } |
| 16 | +// |
| 17 | +// GlobalEventBus.on(type, handler); |
| 18 | +// |
| 19 | +// onScopeDispose(() => { |
| 20 | +// GlobalEventBus.off(type, handler); |
| 21 | +// console.log('disposed event listener for', type); |
| 22 | +// }); |
| 23 | +//} |
| 24 | +// |
| 25 | +//export function emitEvent<Key extends keyof StoreEventsMap>(type: Key, data: StoreEventsMap[Key]): void; |
| 26 | +//export function emitEvent(type, data): void { |
| 27 | +// GlobalEventBus.emit(type, data); |
| 28 | +//} |
| 29 | +//export {useEvent, emitEvent}; |
| 30 | + |
| 31 | +import {getCurrentInstance, onBeforeUnmount} from "vue"; |
| 32 | +import eventBus, {Emitter} from "./EventBus"; |
| 33 | +import {StoreEventsMap} from "./StoreEventsMap"; |
| 34 | + |
| 35 | +export type EventKey = keyof StoreEventsMap | string; |
| 36 | + |
| 37 | +export class StoreEventBus { |
| 38 | + |
| 39 | + protected bus: Emitter<StoreEventsMap> = eventBus<StoreEventsMap>(); |
| 40 | + |
| 41 | + /** |
| 42 | + * Dispatch an event to the main event bus |
| 43 | + */ |
| 44 | + public $dispatch(type: EventKey, evt?: StoreEventsMap[EventKey]) { |
| 45 | + this.bus.emit(type, evt); |
| 46 | + } |
| 47 | + |
| 48 | + public getAllEvents() { |
| 49 | + const events = []; |
| 50 | + |
| 51 | + this.bus.all.forEach((handlers, type) => { |
| 52 | + events.push({type, handlers}); |
| 53 | + }); |
| 54 | + |
| 55 | + return events; |
| 56 | + } |
| 57 | + |
| 58 | + public hasEventHandler(type: EventKey) { |
| 59 | + return this.bus.all.has(type); |
| 60 | + } |
| 61 | + |
| 62 | + public removeAllListeners() { |
| 63 | + this.bus.all.clear(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * By default, when an event is used in a component, and the component is unmounted, the event listener will be removed. |
| 68 | + * You can set detached to true to disable this. |
| 69 | + */ |
| 70 | + public $on( |
| 71 | + type: EventKey, |
| 72 | + handler: (event: StoreEventsMap[EventKey]) => void, |
| 73 | + detached: boolean = false |
| 74 | + ): () => void { |
| 75 | + |
| 76 | + const offFn = () => this.bus.off(type, handler); |
| 77 | + |
| 78 | + if (!detached && getCurrentInstance()) { |
| 79 | + onBeforeUnmount(() => offFn()); |
| 80 | + } |
| 81 | + |
| 82 | + this.bus.on(type, handler); |
| 83 | + |
| 84 | + return offFn; |
| 85 | + } |
| 86 | + |
| 87 | + public $off(type: EventKey, handler: (event: StoreEventsMap[EventKey]) => void) { |
| 88 | + this.bus.off(type, handler); |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments