From 12a7d6bc467f286aa453eaf6b76f0d60a92242d7 Mon Sep 17 00:00:00 2001 From: Nicola Date: Wed, 19 Apr 2023 11:48:49 +0200 Subject: [PATCH] Fix event listener init --- package-lock.json | 4 +-- src/Router.tsx | 2 -- src/hooks/useCheckIfReminderTime.ts | 4 +++ src/hooks/useListenForNewTrackEvent.ts | 34 --------------------- src/hooks/useSetListenerForNewTrackEvent.ts | 23 ++++++++++++++ src/state/actions.ts | 2 +- 6 files changed, 30 insertions(+), 39 deletions(-) delete mode 100644 src/hooks/useListenForNewTrackEvent.ts create mode 100644 src/hooks/useSetListenerForNewTrackEvent.ts diff --git a/package-lock.json b/package-lock.json index cea0ac1..381101c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "quick-tracker", - "version": "0.0.8", + "version": "0.0.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "quick-tracker", - "version": "0.0.8", + "version": "0.0.3", "dependencies": { "@emotion/react": "11.10.6", "@mantine/core": "5.10.4", diff --git a/src/Router.tsx b/src/Router.tsx index 66f19cb..ee9417b 100644 --- a/src/Router.tsx +++ b/src/Router.tsx @@ -4,11 +4,9 @@ import { Home } from "./features/Home/Home"; import { ManualTrack } from "./features/ManualTrack"; import { Settings } from "./features/Settings"; import { useCheckIfReminderTime } from "./hooks/useCheckIfReminderTime"; -import { useListenForNewTrackEvent } from "./hooks/useListenForNewTrackEvent"; export const Router = () => { useCheckIfReminderTime(); - useListenForNewTrackEvent(); return ( diff --git a/src/hooks/useCheckIfReminderTime.ts b/src/hooks/useCheckIfReminderTime.ts index 757936d..d0d7a4d 100644 --- a/src/hooks/useCheckIfReminderTime.ts +++ b/src/hooks/useCheckIfReminderTime.ts @@ -11,15 +11,19 @@ import { useSnapshot } from "valtio"; import { isReminderTime } from "../lib/utils"; import { state } from "../state/state"; import { useGetPopupPosition } from "./useGetPopupPosition"; +import { useSetListenerForNewTrackEvent } from "./useSetListenerForNewTrackEvent"; let reminderInterval: NodeJS.Timer; export const useCheckIfReminderTime = () => { const { settings } = useSnapshot(state); const getPopupPosition = useGetPopupPosition(); + const setListenerForNewTrackEvent = useSetListenerForNewTrackEvent(); useEffect(() => { reminderInterval = setInterval(async () => { + setListenerForNewTrackEvent(); + // const prMonitor = await primaryMonitor(); // const allMonitors = await availableMonitors(); // const monitor = await currentMonitor(); diff --git a/src/hooks/useListenForNewTrackEvent.ts b/src/hooks/useListenForNewTrackEvent.ts deleted file mode 100644 index 2ea53c0..0000000 --- a/src/hooks/useListenForNewTrackEvent.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { UnlistenFn, listen } from "@tauri-apps/api/event"; -import { useEffect } from "react"; -import { addTrack } from "../state/actions"; -import { Frequency } from "../state/types"; - -let listener: UnlistenFn; - -export type EventAddTrackPlayload = { - track: string; - frequency: Frequency; -}; - -export const useListenForNewTrackEvent = () => { - useEffect(() => { - const addListener = async () => { - if (listener) { - listener(); - } - - console.log("Add listener"); - listener = await listen("addTrack", (event) => { - addTrack(event.payload.track, event.payload.frequency); - }); - }; - - addListener(); - - return () => { - if (listener) { - listener(); - } - }; - }, []); -}; diff --git a/src/hooks/useSetListenerForNewTrackEvent.ts b/src/hooks/useSetListenerForNewTrackEvent.ts new file mode 100644 index 0000000..0e028e7 --- /dev/null +++ b/src/hooks/useSetListenerForNewTrackEvent.ts @@ -0,0 +1,23 @@ +import { UnlistenFn, listen } from "@tauri-apps/api/event"; +import { useCallback, useEffect } from "react"; +import { addTrack } from "../state/actions"; +import { Frequency } from "../state/types"; + +let listener: UnlistenFn | undefined = undefined; + +export type EventAddTrackPlayload = { + track: string; + frequency: Frequency; +}; + +export const useSetListenerForNewTrackEvent = () => { + return useCallback(async () => { + if (listener) { + return; + } + + listener = await listen("addTrack", (event) => { + addTrack(event.payload.track, event.payload.frequency); + }); + }, []); +}; diff --git a/src/state/actions.ts b/src/state/actions.ts index b19b16a..3a03254 100644 --- a/src/state/actions.ts +++ b/src/state/actions.ts @@ -1,6 +1,6 @@ import { v4 as uuidv4 } from "uuid"; import { state } from "./state"; -import { Frequency, Settings, Track } from "./types"; +import { Frequency, Settings, type Track } from "./types"; export const addTrack = (track: string, duration: Frequency, startTime = new Date()) => { state.trackings.push({