Skip to content

Commit

Permalink
Fixed event listeners not being detached correctly
Browse files Browse the repository at this point in the history
- Improved preloader component performance.
- Updated app dependencies.
  • Loading branch information
Sandakan committed Nov 7, 2024
1 parent 9c7304a commit d6575a2
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 148 deletions.
237 changes: 119 additions & 118 deletions package-lock.json

Large diffs are not rendered by default.

29 changes: 19 additions & 10 deletions src/renderer/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,12 @@ export default function App() {
useEffect(
() => {
// check for app updates on app startup after 5 seconds.
setTimeout(checkForAppUpdates, 5000);
const timeoutId = setTimeout(checkForAppUpdates, 5000);
// checks for app updates every 10 minutes.
const id = setInterval(checkForAppUpdates, 1000 * 60 * 15);
const intervalId = setInterval(checkForAppUpdates, 1000 * 60 * 15);
return () => {
clearInterval(id);
clearTimeout(timeoutId);
clearInterval(intervalId);
};
},
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down Expand Up @@ -321,21 +322,25 @@ export default function App() {
}, []);

useEffect(() => {
player.addEventListener('error', (err) => managePlaybackErrors(err));
player.addEventListener('play', () => {
const handlePlayerErrorEvent = (err: unknown) => managePlaybackErrors(err);
const handlePlayerPlayEvent = () => {
dispatch({
type: 'CURRENT_SONG_PLAYBACK_STATE',
data: true
});
window.api.playerControls.songPlaybackStateChange(true);
});
player.addEventListener('pause', () => {
};
const handlePlayerPauseEvent = () => {
dispatch({
type: 'CURRENT_SONG_PLAYBACK_STATE',
data: false
});
window.api.playerControls.songPlaybackStateChange(false);
});
};

player.addEventListener('error', handlePlayerErrorEvent);
player.addEventListener('play', handlePlayerPlayEvent);
player.addEventListener('pause', handlePlayerPauseEvent);
window.api.quitEvent.beforeQuitEvent(handleBeforeQuitEvent);

window.api.windowControls.onWindowBlur(() => manageWindowBlurOrFocus('blur'));
Expand All @@ -345,10 +350,14 @@ export default function App() {
window.api.fullscreen.onLeaveFullscreen(() => manageWindowFullscreen('windowed'));

return () => {
player.removeEventListener('error', handlePlayerErrorEvent);
player.removeEventListener('play', handlePlayerPlayEvent);
player.removeEventListener('pause', handlePlayerPauseEvent);
window.api.quitEvent.removeBeforeQuitEventListener(handleBeforeQuitEvent);
};

// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [manageWindowBlurOrFocus, manageWindowFullscreen, handleBeforeQuitEvent]);

useEffect(() => {
const displayDefaultTitleBar = () => {
Expand Down Expand Up @@ -491,7 +500,7 @@ export default function App() {
}

return () => {
// document.removeEventListener('localStorage', syncLocalStorage);
document.removeEventListener('localStorage', syncLocalStorage);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/components/AlbumsPage/AlbumsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ const AlbumsPage = () => {
data={albumsData}
fixedItemWidth={MIN_ITEM_WIDTH}
fixedItemHeight={MIN_ITEM_HEIGHT}
scrollTopOffset={currentlyActivePage.data?.scrollTopOffset}
// scrollTopOffset={currentlyActivePage.data?.scrollTopOffset}
itemContent={(index, item) => {
return (
<Album
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/components/GenresPage/GenresPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ const GenresPage = () => {
data={genresData}
fixedItemWidth={MIN_ITEM_WIDTH}
fixedItemHeight={MIN_ITEM_HEIGHT}
scrollTopOffset={currentlyActivePage.data?.scrollTopOffset}
// scrollTopOffset={currentlyActivePage.data?.scrollTopOffset}
itemContent={(index, genre) => {
return (
<Genre
Expand Down
6 changes: 3 additions & 3 deletions src/renderer/src/components/LyricsPage/LyricsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export const syncedLyricsRegex = /^\[\d+:\d{1,2}\.\d{1,3}]/gm;
// export const delay = 0.35;

let isScrollingByCode = false;
document.addEventListener('lyrics/scrollIntoView', () => {
isScrollingByCode = true;
});
// document.addEventListener('lyrics/scrollIntoView', () => {
// isScrollingByCode = true;
// });

const LyricsPage = () => {
const preferences = useStore(store, (state) => state.localStorage.preferences);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ const Notification = (props: AppNotification) => {

if (notificationRef.current && !isNotificationAnimationDisabled) {
notificationRef.current.classList.add('disappear-to-bottom');
notificationRef.current.addEventListener('animationend', () =>
updateNotifications((currNotifications) => currNotifications.filter((x) => x.id !== id))
notificationRef.current.addEventListener(
'animationend',
() =>
updateNotifications((currNotifications) => currNotifications.filter((x) => x.id !== id)),
{ once: true }
);
} else updateNotifications((currNotifications) => currNotifications.filter((x) => x.id !== id));
}, [id, preferences?.isReducedMotion, type, updateNotifications]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ const PlaylistsPage = () => {
data={playlists}
fixedItemWidth={MIN_ITEM_WIDTH}
fixedItemHeight={MIN_ITEM_HEIGHT}
scrollTopOffset={currentlyActivePage.data?.scrollTopOffset}
// scrollTopOffset={currentlyActivePage.data?.scrollTopOffset}
itemContent={(index, playlist) => {
return <Playlist index={index} selectAllHandler={selectAllHandler} {...playlist} />;
}}
Expand Down
40 changes: 28 additions & 12 deletions src/renderer/src/components/Preloader/Preloader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,47 @@ let isLoadedOnce = false;

const hidePreloader = () => {
const preloader = document.querySelector('.preloader');
if (preloader) {
const isHidable = preloader && !preloader.classList.contains('!invisible');
console.warn('hide preloader requested', {
time: window.performance.now() - contentLoadStart,
state: document.readyState,
isHidable
});

if (isHidable) {
preloader.classList.add('!invisible', '!opacity-0');
console.warn(
'preloader hidden',
window.performance.now() - contentLoadStart,
document.readyState
);
}
};

const eventHandler = () => {
setTimeout(hidePreloader, 1000);

console.warn('contentLoad', window.performance.now() - contentLoadStart);
};
window.addEventListener(
'load',
() => {
setTimeout(hidePreloader, 1000);

window.addEventListener('load', eventHandler);
console.warn('contentLoad', window.performance.now() - contentLoadStart, document.readyState);
},
{ once: true }
);

const Preloader = () => {
const [isPreloaderRemoved, setIsPreloaderRemoved] = useState(false);

useEffect(() => {
// console.log(window.performance.now());
if (window.performance.now() > 3000) hidePreloader();
// this removes preloader in 10 seconds no matter what didn't load.
setTimeout(() => {
// this removes preloader in 5 seconds no matter what didn't load.
const timeoutId = setTimeout(() => {
hidePreloader();
isLoadedOnce = true;
setIsPreloaderRemoved(true);
}, 3000);
}, 5000);

return () => {
clearTimeout(timeoutId);
};
}, []);

return (
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/src/hooks/useNetworkConnectivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ const useNetworkConnectivity = () => {

useEffect(() => {
const controller = new AbortController();

setIsOnline(navigator.onLine);

window.addEventListener('online', () => setIsOnline(true), {
signal: controller.signal
});
window.addEventListener('offline', () => setIsOnline(false), {
signal: controller.signal
});

return () => controller.abort();
}, []);

Expand Down

0 comments on commit d6575a2

Please sign in to comment.