players: fix duplicate players

Also async read from cache
This commit is contained in:
2 * r + 2 * t 2025-04-08 23:21:32 +10:00
parent f178c361b3
commit b43a576615

View file

@ -1,5 +1,5 @@
import { isRealPlayer } from "@/utils/mpris"; import { isRealPlayer } from "@/utils/mpris";
import { GLib, GObject, property, readFile, register, writeFileAsync } from "astal"; import { GLib, GObject, property, readFileAsync, register, writeFileAsync } from "astal";
import AstalMpris from "gi://AstalMpris"; import AstalMpris from "gi://AstalMpris";
@register({ GTypeName: "Players" }) @register({ GTypeName: "Players" })
@ -60,9 +60,9 @@ export default class Players extends GObject.Object {
} }
makeCurrent(player: AstalMpris.Player) { makeCurrent(player: AstalMpris.Player) {
const index = this.#players.indexOf(player); const index = this.#players.findIndex(p => p.busName === player.busName);
// Ignore if already current // Ignore if already current
if (index === 0) return; if (index === 0 || !isRealPlayer(player)) return;
// Remove if present // Remove if present
else if (index > 0) this.#players.splice(index, 1); else if (index > 0) this.#players.splice(index, 1);
// Connect signals if not already in list (i.e. new player) // Connect signals if not already in list (i.e. new player)
@ -71,6 +71,7 @@ export default class Players extends GObject.Object {
// Add to front // Add to front
this.#players.unshift(player); this.#players.unshift(player);
this.#updatePlayer(); this.#updatePlayer();
this.notify("list");
// Save to file // Save to file
this.#save(); this.#save();
@ -110,35 +111,28 @@ export default class Players extends GObject.Object {
// Load players // Load players
if (GLib.file_test(this.#path, GLib.FileTest.EXISTS)) { if (GLib.file_test(this.#path, GLib.FileTest.EXISTS)) {
this.#players = readFile(this.#path) readFileAsync(this.#path).then(out => {
.split("\n") for (const busName of out.split("\n").reverse()) {
.map(p => mpris.players.find(p2 => p2.busName === p)) const player = mpris.get_players().find(p => p.busName === busName);
.filter(isRealPlayer) as AstalMpris.Player[]; if (player) this.makeCurrent(player);
}
// Add new players from in between sessions // Add new players from in between sessions
for (const player of mpris.players) for (const player of mpris.get_players()) this.makeCurrent(player);
if (!this.#players.includes(player) && isRealPlayer(player)) this.#players.push(player); });
} else { } else {
const sortOrder = [ const sortOrder = [
AstalMpris.PlaybackStatus.PLAYING, AstalMpris.PlaybackStatus.PLAYING,
AstalMpris.PlaybackStatus.PAUSED, AstalMpris.PlaybackStatus.PAUSED,
AstalMpris.PlaybackStatus.STOPPED, AstalMpris.PlaybackStatus.STOPPED,
]; ];
this.#players = mpris.players const players = mpris
.filter(isRealPlayer) .get_players()
.sort((a, b) => sortOrder.indexOf(a.playbackStatus) - sortOrder.indexOf(b.playbackStatus)); .sort((a, b) => sortOrder.indexOf(b.playbackStatus) - sortOrder.indexOf(a.playbackStatus));
for (const player of players) this.makeCurrent(player);
} }
this.#updatePlayer();
this.#save();
// Connect signals to loaded players
for (const player of this.#players) this.#connectPlayerSignals(player);
// Add and connect signals when added // Add and connect signals when added
mpris.connect("player-added", (_, player) => { mpris.connect("player-added", (_, player) => this.makeCurrent(player));
if (isRealPlayer(player)) {
this.makeCurrent(player);
this.notify("list");
}
});
// Remove when closed // Remove when closed
mpris.connect("player-closed", (_, player) => { mpris.connect("player-closed", (_, player) => {