| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- import { defineStore } from 'pinia'
- import { ref } from 'vue'
- import type { Ref } from 'vue'
- import { useEntityManager } from '~/composables/data/useEntityManager'
- import type { MercureEntityUpdate } from '~/types/interfaces'
- export const useSseStore = defineStore('sse', () => {
- const connected: Ref<boolean> = ref(false)
- const events: Ref<Array<MercureEntityUpdate>> = ref([])
- const addEvent = async (event: MercureEntityUpdate) => {
- const { em } = useEntityManager()
- const model = await em.getModelFromIri(event.iri)
- const instance = em.newInstance(model, JSON.parse(event.data))
- switch (event.operation) {
- case 'update':
- case 'create':
- if (model.entity === 'my_profile') {
- const accessProfileStore = useAccessProfileStore()
- accessProfileStore.initiateProfile(instance)
- } else {
- // Cas générique d'une entité standard
- em.save(instance, true)
- }
- break
- case 'delete':
- break
- default:
- throw new Error('SSE: unknown operation type')
- }
- }
- return {
- connected,
- events,
- addEvent,
- }
- })
|