| 123456789101112131415161718192021 |
- import { defineStore } from 'pinia'
- import { ref } from 'vue'
- export const useEventStore = defineStore('eventStore', () => {
- const events = ref(new Map<string, Function[]>())
- function emit(event: string, ...args: any[]) {
- if (events.value.has(event)) {
- events.value.get(event)?.forEach(callback => callback(...args))
- }
- }
- function on(event: string, callback: Function) {
- if (!events.value.has(event)) {
- events.value.set(event, [])
- }
- events.value.get(event)?.push(callback)
- }
- return { emit, on }
- })
|