Skip to content

Commit

Permalink
Fix event listener init
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolabosco87 committed Apr 19, 2023
1 parent 730ac4f commit 12a7d6b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 39 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions src/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Routes>
Expand Down
4 changes: 4 additions & 0 deletions src/hooks/useCheckIfReminderTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
34 changes: 0 additions & 34 deletions src/hooks/useListenForNewTrackEvent.ts

This file was deleted.

23 changes: 23 additions & 0 deletions src/hooks/useSetListenerForNewTrackEvent.ts
Original file line number Diff line number Diff line change
@@ -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<EventAddTrackPlayload>("addTrack", (event) => {
addTrack(event.payload.track, event.payload.frequency);
});
}, []);
};
2 changes: 1 addition & 1 deletion src/state/actions.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down

0 comments on commit 12a7d6b

Please sign in to comment.