Move schedule fetching into useSchedule composable
This commit is contained in:
parent
ddecfa3805
commit
13f344472e
5 changed files with 49 additions and 35 deletions
|
@ -27,35 +27,6 @@
|
|||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import type { Schedule } from '~/shared/types/schedule';
|
||||
const { data: session } = await useAccountSession();
|
||||
|
||||
const schedule = useSchedule();
|
||||
|
||||
await callOnce(async () => {
|
||||
schedule.value = await $fetch("/api/schedule");
|
||||
})
|
||||
|
||||
const source = ref<EventSource | null>(null);
|
||||
|
||||
onMounted(() => {
|
||||
console.log("Opening event source")
|
||||
source.value = new EventSource("/api/events");
|
||||
source.value.addEventListener("message", (message) => {
|
||||
console.log("Message", message.data);
|
||||
});
|
||||
source.value.addEventListener("update", (message) => {
|
||||
const updatedSchedule: Schedule = JSON.parse(message.data);
|
||||
console.log("Update", updatedSchedule);
|
||||
schedule.value = updatedSchedule;
|
||||
});
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
if (source.value) {
|
||||
console.log("Closing event source")
|
||||
source.value.close();
|
||||
source.value = null;
|
||||
}
|
||||
});
|
||||
const schedule = await useSchedule();
|
||||
</script>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue