sse.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 as string))
  13. switch (event.operation) {
  14. case 'update':
  15. case 'create':
  16. if (model.entity === 'my_profile') {
  17. const accessProfileStore = useAccessProfileStore()
  18. // @ts-expect-error on sait que instance est du type AccessProfile ici
  19. accessProfileStore.initiateProfile(instance)
  20. } else {
  21. // Cas générique d'une entité standard
  22. em.save(instance, true)
  23. }
  24. break
  25. case 'delete':
  26. break
  27. default:
  28. throw new Error('SSE: unknown operation type')
  29. }
  30. }
  31. return {
  32. connected,
  33. events,
  34. addEvent,
  35. }
  36. })