mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 13:47:15 +08:00
refactor: 消息总线
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { useThemeStore } from '@/store'
|
||||
import mitt from 'mitt'
|
||||
import * as NaiveUI from 'naive-ui'
|
||||
|
||||
export async function setupNaiveDiscreteApi() {
|
||||
@@ -16,4 +17,5 @@ export async function setupNaiveDiscreteApi() {
|
||||
window.$notification = notification
|
||||
window.$message = message
|
||||
window.$dialog = dialog
|
||||
window.$bus = mitt()
|
||||
}
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
import { reactive } from 'vue'
|
||||
|
||||
type EventCallback<T = any> = (payload: T) => void
|
||||
|
||||
interface EventBusInterface {
|
||||
events: Record<string, EventCallback[]>
|
||||
emit<T = any>(event: string, data?: T): void
|
||||
on<T = any>(event: string, callback: EventCallback<T>): void
|
||||
off<T = any>(event: string, callback: EventCallback<T>): void
|
||||
}
|
||||
|
||||
const EventBus: EventBusInterface = reactive({
|
||||
events: {} as Record<string, EventCallback[]>,
|
||||
emit<T>(event: string, data?: T): void {
|
||||
if (this.events[event]) {
|
||||
this.events[event].forEach((callback) => callback(data))
|
||||
}
|
||||
},
|
||||
on<T>(event: string, callback: EventCallback<T> = () => {}): void {
|
||||
if (!this.events[event]) {
|
||||
this.events[event] = []
|
||||
}
|
||||
this.events[event].push(callback)
|
||||
},
|
||||
off<T>(event: string, callback: EventCallback<T>): void {
|
||||
if (this.events[event]) {
|
||||
const index = this.events[event].indexOf(callback)
|
||||
if (index > -1) {
|
||||
this.events[event].splice(index, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
export default EventBus
|
||||
@@ -1,6 +1,5 @@
|
||||
export * from './auth'
|
||||
export * from './common'
|
||||
export * from './event'
|
||||
export * from './file'
|
||||
export * from './http'
|
||||
export * from './storage'
|
||||
|
||||
Reference in New Issue
Block a user