forked from
tokono.ma/diffuse
A music player that connects to your cloud/distributed storage.
1import { defineElement, DiffuseElement } from "~/common/element.js";
2
3/**
4 * @import {Track} from "~/definitions/types.d.ts"
5 * @import {ScrobbleActions, ScrobbleElement} from "~/components/supplement/types.d.ts"
6 */
7
8////////////////////////////////////////////
9// ELEMENT
10////////////////////////////////////////////
11
12/**
13 * @implements {ScrobbleActions}
14 */
15class ScrobblesConfigurator extends DiffuseElement {
16 static NAME = "diffuse/configurator/scrobbles";
17
18 // SCROBBLE ACTIONS
19
20 /**
21 * @param {Track} track
22 */
23 async nowPlaying(track) {
24 return await Promise.all(
25 this.#activeScrobblers().map((s) => s.nowPlaying(track)),
26 );
27 }
28
29 /**
30 * @param {Track} track
31 * @param {number} startedAt Unix timestamp in milliseconds
32 */
33 async scrobble(track, startedAt) {
34 return await Promise.all(
35 this.#activeScrobblers().map((s) => s.scrobble(track, startedAt)),
36 );
37 }
38
39 // MISC
40
41 /**
42 * All child scrobble elements, regardless of authentication state.
43 *
44 * @returns {ScrobbleElement[]}
45 */
46 scrobblers() {
47 return Array.from(this.root().children).flatMap((el) => {
48 if (!("isAuthenticated" in el && "nowPlaying" in el)) return [];
49 return [/** @type {ScrobbleElement} */ (/** @type {unknown} */ (el))];
50 });
51 }
52
53 /**
54 * Child scrobble elements that are currently authenticated.
55 *
56 * @returns {ScrobbleElement[]}
57 */
58 #activeScrobblers() {
59 return this.scrobblers().filter((s) => s.isAuthenticated());
60 }
61}
62
63export default ScrobblesConfigurator;
64
65////////////////////////////////////////////
66// REGISTER
67////////////////////////////////////////////
68
69export const CLASS = ScrobblesConfigurator;
70export const NAME = "dc-scrobbles";
71
72defineElement(NAME, CLASS);