Skip to content

Commit

Permalink
Fix memory leaks, make app always appear on the default screen
Browse files Browse the repository at this point in the history
  • Loading branch information
peancored committed Feb 15, 2024
1 parent 143621c commit 5e2dc3c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
41 changes: 17 additions & 24 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
shell,
ipcMain,
protocol,
screen,
powerSaveBlocker,
} from 'electron';
import { autoUpdater } from 'electron-updater';
Expand Down Expand Up @@ -77,6 +76,13 @@ ipcMain.on('rescan-songs', async (event) => {
});
});

const isDebug =
process.env.NODE_ENV === 'development' || process.env.DEBUG_PROD === 'true';

ipcMain.on('check-dev', async (event) => {
event.reply('check-dev', isDebug);
});

ipcMain.on('like-song', async (event, id, liked) => {
store.set(`songs.${id}.liked`, liked);
});
Expand All @@ -94,11 +100,8 @@ if (process.env.NODE_ENV === 'production') {
sourceMapSupport.install();
}

const isDebug =
process.env.NODE_ENV === 'development' || process.env.DEBUG_PROD === 'true';

if (isDebug) {
require('electron-debug')();
// require('electron-debug')();
}

const installExtensions = async () => {
Expand Down Expand Up @@ -131,17 +134,12 @@ const createWindow = async () => {
return path.join(RESOURCES_PATH, ...paths);
};

const displays = screen.getAllDisplays();
const externalDisplay = displays.find((display) => {
return display.bounds.x !== 0 || display.bounds.y !== 0;
});

mainWindow = new BrowserWindow({
show: false,
x: externalDisplay ? externalDisplay.bounds.x + 50 : 0,
y: externalDisplay ? externalDisplay.bounds.y + 50 : 0,
width: externalDisplay ? externalDisplay.bounds.width : 1024,
height: externalDisplay ? externalDisplay.bounds.height : 728,
x: 0,
y: 0,
width: 1024,
height: 728,
icon: getAssetPath('icon.png'),
webPreferences: {
preload: app.isPackaged
Expand Down Expand Up @@ -201,21 +199,16 @@ protocol.registerSchemesAsPrivileged([
app
.whenReady()
.then(() => {
// protocol.handle('gh', (request) =>
// net.fetch(
// `file://${decodeURIComponent(request.url.slice('gh://'.length))}`,
// ),
// );
protocol.registerFileProtocol('gh', (request, callback) => {
const url = decodeURIComponent(request.url.substr(5));
callback({ path: url });
});

createWindow();
// app.on('activate', () => {
// // On macOS it's common to re-create a window in the app when the
// // dock icon is clicked and there are no other windows open.
// if (mainWindow === null) createWindow();
// });
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) createWindow();
});
})
.catch(console.log);
3 changes: 2 additions & 1 deletion src/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export type Channels =
| 'rescan-songs'
| 'like-song'
| 'prevent-sleep'
| 'resume-sleep';
| 'resume-sleep'
| 'check-dev';

const electronHandler = {
ipcRenderer: {
Expand Down
18 changes: 15 additions & 3 deletions src/renderer/views/SongView/SongView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function SongView() {
const [audioPlayer, setAudioPlayer] = useState<AudioPlayer | null>(null);
const [trackData, setTrackData] = useState<TrackConfig[]>([]);
const [volumeControls, setVolumeControls] = useState<VolumeControl[]>([]);
const [isDev, setIsDev] = useState(true);

const { id } = useParams();
const navigate = useNavigate();
Expand All @@ -46,6 +47,14 @@ export function SongView() {
};
}, []);

useEffect(() => {
window.electron.ipcRenderer.sendMessage('check-dev');

window.electron.ipcRenderer.once('check-dev', (dev: boolean) => {
setIsDev(dev);
});
}, []);

const loadSong = useCallback(() => {
window.electron.ipcRenderer.once<IpcLoadSongResponse>(
'load-song',
Expand Down Expand Up @@ -107,10 +116,13 @@ export function SongView() {
return () => {
clearInterval(audioPolling);

audioPlayer.stop();
// audioPlayer.destroy();
if (isDev) {
audioPlayer.stop();
} else {
audioPlayer.destroy();
}
};
}, [audioPlayer]);
}, [audioPlayer, isDev]);

useEffect(() => {
if (volumeControls.length === 0 || !audioPlayer) {
Expand Down

0 comments on commit 5e2dc3c

Please sign in to comment.