-
Notifications
You must be signed in to change notification settings - Fork 955
/
Copy pathmain.ts
148 lines (131 loc) · 4.54 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { BrowserWindow } from 'electron';
import registerCallback, {
MediaType,
type SongInfo,
SongInfoEvent,
} from '@/providers/song-info';
import { createBackend } from '@/utils';
import { LastFmScrobbler } from './services/lastfm';
import { ListenbrainzScrobbler } from './services/listenbrainz';
import { SlackScrobbler } from './services/slack';
import type { ScrobblerPluginConfig } from './index';
import type { ScrobblerBase } from './services/base';
export type SetConfType = (
conf: Partial<Omit<ScrobblerPluginConfig, 'enabled'>>,
) => void | Promise<void>;
export const backend = createBackend<
{
config?: ScrobblerPluginConfig;
window?: BrowserWindow;
enabledScrobblers: Map<string, ScrobblerBase>;
toggleScrobblers(
config: ScrobblerPluginConfig,
window: BrowserWindow,
): void;
createSessions(
config: ScrobblerPluginConfig,
setConfig: SetConfType,
): Promise<void>;
setConfig?: SetConfType;
},
ScrobblerPluginConfig
>({
enabledScrobblers: new Map(),
toggleScrobblers(config: ScrobblerPluginConfig, window: BrowserWindow) {
if (config.scrobblers.lastfm && config.scrobblers.lastfm.enabled) {
this.enabledScrobblers.set('lastfm', new LastFmScrobbler(window));
} else {
this.enabledScrobblers.delete('lastfm');
}
if (
config.scrobblers.listenbrainz &&
config.scrobblers.listenbrainz.enabled
) {
this.enabledScrobblers.set('listenbrainz', new ListenbrainzScrobbler());
} else {
this.enabledScrobblers.delete('listenbrainz');
}
if (config.scrobblers.slack && config.scrobblers.slack.enabled) {
this.enabledScrobblers.set('slack', new SlackScrobbler(window));
} else {
this.enabledScrobblers.delete('slack');
}
},
async createSessions(config: ScrobblerPluginConfig, setConfig: SetConfType) {
for (const [, scrobbler] of this.enabledScrobblers) {
if (!scrobbler.isSessionCreated(config)) {
await scrobbler.createSession(config, setConfig);
}
}
},
async start({ getConfig, setConfig, window }) {
const config = (this.config = await getConfig());
// This will store the timeout that will trigger addScrobble
let scrobbleTimer: NodeJS.Timeout | undefined;
this.window = window;
this.toggleScrobblers(config, window);
await this.createSessions(config, setConfig);
this.setConfig = setConfig;
registerCallback((songInfo: SongInfo, event) => {
if (event === SongInfoEvent.TimeChanged) return;
// Set remove the old scrobble timer
clearTimeout(scrobbleTimer);
if (!songInfo.isPaused) {
const configNonnull = this.config!;
// Scrobblers normally have no trouble working with official music videos
if (
!configNonnull.scrobbleOtherMedia &&
songInfo.mediaType !== MediaType.Audio &&
songInfo.mediaType !== MediaType.OriginalMusicVideo
) {
return;
}
// Scrobble when the song is halfway through, or has passed the 4-minute mark
const scrobbleTime = Math.min(
Math.ceil(songInfo.songDuration / 2),
4 * 60,
);
if (scrobbleTime > (songInfo.elapsedSeconds ?? 0)) {
// Scrobble still needs to happen
const timeToWait =
(scrobbleTime - (songInfo.elapsedSeconds ?? 0)) * 1000;
scrobbleTimer = setTimeout(
(info, config) => {
this.enabledScrobblers.forEach((scrobbler) =>
scrobbler.addScrobble(info, config, setConfig),
);
},
timeToWait,
songInfo,
configNonnull,
);
}
this.enabledScrobblers.forEach((scrobbler) =>
scrobbler.setNowPlaying(songInfo, configNonnull, setConfig),
);
}
});
},
async onConfigChange(newConfig: ScrobblerPluginConfig) {
this.enabledScrobblers.clear();
this.toggleScrobblers(newConfig, this.window!);
for (const [scrobblerName, scrobblerConfig] of Object.entries(
newConfig.scrobblers,
)) {
if (scrobblerConfig.enabled) {
const scrobbler = this.enabledScrobblers.get(scrobblerName);
if (
this.config?.scrobblers?.[
scrobblerName as keyof typeof newConfig.scrobblers
]?.enabled !== scrobblerConfig.enabled &&
scrobbler &&
!scrobbler.isSessionCreated(newConfig) &&
this.setConfig
) {
await scrobbler.createSession(newConfig, this.setConfig);
}
}
}
this.config = newConfig;
},
});