import {defineStore} from "pinia"; import {Ref, ref} from "@vue/reactivity"; import {useEntityManager} from "~/composables/data/useEntityManager"; export const useSseStore = defineStore('sse', () => { const connected: Ref = ref(false) const events: Ref> = ref([]) const addEvent = async (event: MercureEntityUpdate) => { const {em} = useEntityManager() // TODO: voir à refactorer le "get model from iri" const matches = event.iri.match(/^\/api\/(\w+)\/.*/) if (!matches || !matches[1]) { throw new Error('cannot parse the IRI') } const entityName = matches[1] const model = em.getModelFor(entityName) switch (event.operation) { case "update": case "create": await em.persist(model, JSON.parse(event.data)) break case "delete": break default: throw new Error('SSE: unknown operation type') } } return { connected, events, addEvent } })