mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-12 02:51:46 +00:00
fix: callback for time-changed event (#2577)
Co-authored-by: Derek Alsop <15299183+Azorant@users.noreply.github.com>
This commit is contained in:
@ -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 = [
|
||||
|
||||
Reference in New Issue
Block a user