Skip to content

Commit

Permalink
[bugfix]: defer restore queue until mpv exists
Browse files Browse the repository at this point in the history
  • Loading branch information
kgarner7 committed Jan 7, 2024
1 parent 960427f commit c5e08b6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
28 changes: 15 additions & 13 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,10 @@ const DEFAULT_MPV_PARAMETERS = (extraParameters?: string[]) => {

let mpvInstance: MpvAPI | null = null;

const createMpv = (data: { extraParameters?: string[]; properties?: Record<string, any> }) => {
const createMpv = async (data: {
extraParameters?: string[];
properties?: Record<string, any>;
}): Promise<MpvAPI> => {
const { extraParameters, properties } = data;

const params = uniq([...DEFAULT_MPV_PARAMETERS(extraParameters), ...(extraParameters || [])]);
Expand All @@ -457,15 +460,14 @@ const createMpv = (data: { extraParameters?: string[]; properties?: Record<strin
params,
);

// eslint-disable-next-line promise/catch-or-return
mpv.start()
.catch((error) => {
console.log('MPV failed to start', error);
})
.finally(() => {
console.log('Setting MPV properties: ', properties);
mpv.setMultipleProperties(properties || {});
});
try {
await mpv.start();
} catch (error) {
console.log('MPV failed to start', error);
} finally {
console.log('Setting MPV properties: ', properties);
await mpv.setMultipleProperties(properties || {});
}

mpv.on('status', (status, ...rest) => {
console.log('MPV Event: status', status.property, status.value, rest);
Expand Down Expand Up @@ -530,15 +532,15 @@ ipcMain.on(
'player-restart',
async (_event, data: { extraParameters?: string[]; properties?: Record<string, any> }) => {
mpvInstance?.quit();
mpvInstance = createMpv(data);
mpvInstance = await createMpv(data);
},
);

ipcMain.on(
ipcMain.handle(
'player-initialize',
async (_event, data: { extraParameters?: string[]; properties?: Record<string, any> }) => {
console.log('Initializing MPV with data: ', data);
mpvInstance = createMpv(data);
mpvInstance = await createMpv(data);
},
);

Expand Down
4 changes: 2 additions & 2 deletions src/main/preload/mpv-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { ipcRenderer, IpcRendererEvent } from 'electron';
import { PlayerData, PlayerState } from '/@/renderer/store';

const initialize = (data: { extraParameters?: string[]; properties?: Record<string, any> }) => {
ipcRenderer.send('player-initialize', data);
return ipcRenderer.invoke('player-initialize', data);
};

const restart = (data: { extraParameters?: string[]; properties?: Record<string, any> }) => {
ipcRenderer.send('player-restart', data);
return ipcRenderer.invoke('player-restart', data);
};

const isRunning = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const App = () => {
...getMpvProperties(useSettingsStore.getState().playback.mpvProperties),
};

mpvPlayer?.initialize({
await mpvPlayer?.initialize({
extraParameters,
properties,
});
Expand Down

0 comments on commit c5e08b6

Please sign in to comment.