fix: callback for time-changed event (#2577)

Co-authored-by: Derek Alsop <15299183+Azorant@users.noreply.github.com>
This commit is contained in:
JellyBrick
2024-11-03 19:18:06 +09:00
committed by GitHub
parent 516fbff3d7
commit 1e4cd699db
13 changed files with 137 additions and 91 deletions

View File

@ -149,8 +149,17 @@ const handleData = async (
return songInfo;
};
export enum SongInfoEvent {
VideoSrcChanged = 'ytmd:video-src-changed',
PlayOrPaused = 'ytmd:play-or-paused',
TimeChanged = 'ytmd:time-changed',
}
// This variable will be filled with the callbacks once they register
export type SongInfoCallback = (songInfo: SongInfo, event?: string) => void;
export type SongInfoCallback = (
songInfo: SongInfo,
event: SongInfoEvent,
) => void;
const callbacks: Set<SongInfoCallback> = new Set();
// This function will allow plugins to register callback that will be triggered when data changes
@ -173,7 +182,7 @@ const registerProvider = (win: BrowserWindow) => {
if (tempSongInfo) {
for (const c of callbacks) {
c(tempSongInfo, 'ytmd:video-src-changed');
c(tempSongInfo, SongInfoEvent.VideoSrcChanged);
}
}
});
@ -199,11 +208,29 @@ const registerProvider = (win: BrowserWindow) => {
if (tempSongInfo) {
for (const c of callbacks) {
c(tempSongInfo, 'ytmd:play-or-paused');
c(tempSongInfo, SongInfoEvent.PlayOrPaused);
}
}
},
);
ipcMain.on('ytmd:time-changed', async (_, seconds: number) => {
const tempSongInfo = await dataMutex.runExclusive<SongInfo | null>(() => {
if (!songInfo) {
return null;
}
songInfo.elapsedSeconds = seconds;
return songInfo;
});
if (tempSongInfo) {
for (const c of callbacks) {
c(tempSongInfo, SongInfoEvent.TimeChanged);
}
}
});
};
const suffixesToRemove = [