sse.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { defineStore } from 'pinia'
  2. import { ref } from 'vue'
  3. import type { Ref } from 'vue'
  4. import { useEntityManager } from '~/composables/data/useEntityManager'
  5. import type { MercureEntityUpdate } from '~/types/interfaces'
  6. export const useSseStore = defineStore('sse', () => {
  7. const connected: Ref<boolean> = ref(false)
  8. const events: Ref<Array<MercureEntityUpdate>> = ref([])
  9. const addEvent = async (event: MercureEntityUpdate) => {
  10. const { em } = useEntityManager()
  11. const model = await em.getModelFromIri(event.iri)
  12. const instance = em.newInstance(model, JSON.parse(event.data))
  13. switch (event.operation) {
  14. case 'update':
  15. case 'create':
  16. if (model.entity === 'my_profile') {
  17. const accessProfileStore = useAccessProfileStore()
  18. accessProfileStore.initiateProfile(instance)
  19. } else {
  20. // Cas générique d'une entité standard
  21. em.save(instance, true)
  22. }
  23. break
  24. case 'delete':
  25. break
  26. default:
  27. throw new Error('SSE: unknown operation type')
  28. }
  29. }
  30. return {
  31. connected,
  32. events,
  33. addEvent,
  34. }
  35. })