-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
253d57c
commit b3e9f71
Showing
6 changed files
with
54 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import goalState from "@/state/goals"; | ||
import { events } from "@/utils/constants"; | ||
import { Schemas } from "@/utils/schemas"; | ||
import { onUnmounted, ref } from "vue"; | ||
|
||
export function useSSE(url: string) { | ||
const eventSource = ref<EventSource | null>(null); | ||
|
||
const connect = () => { | ||
if (eventSource.value) { | ||
return; | ||
} | ||
|
||
const es = new EventSource(url); | ||
es.onopen = () => { | ||
console.log("connected"); | ||
console.log("readystate:", es.readyState); | ||
}; | ||
es.onerror = (event) => { | ||
console.error("error", event); | ||
}; | ||
|
||
es.addEventListener(events.DEFAULT_GOAL_CREATED, (event) => { | ||
const json = JSON.parse(event.data); | ||
const parsedData = Schemas.GoalSchema.parse(json); | ||
goalState.addGoal(parsedData.category_id, parsedData); | ||
}); | ||
|
||
eventSource.value = es; | ||
}; | ||
|
||
onUnmounted(() => { | ||
if (eventSource.value) { | ||
eventSource.value.close(); | ||
} | ||
eventSource.value = null; | ||
}); | ||
|
||
return { | ||
eventSource, | ||
connect, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.