eventStore.ts 545 B

123456789101112131415161718192021
  1. import { defineStore } from 'pinia'
  2. import { ref } from 'vue'
  3. export const useEventStore = defineStore('eventStore', () => {
  4. const events = ref(new Map<string, Function[]>())
  5. function emit(event: string, ...args: any[]) {
  6. if (events.value.has(event)) {
  7. events.value.get(event)?.forEach(callback => callback(...args))
  8. }
  9. }
  10. function on(event: string, callback: Function) {
  11. if (!events.value.has(event)) {
  12. events.value.set(event, [])
  13. }
  14. events.value.get(event)?.push(callback)
  15. }
  16. return { emit, on }
  17. })