-
Hi! I'm trying to create service which would generate colors from current track cover. My service barely works, but I ran into a problem, I don't disconnect from Mpris signals and it turns out that with each switched track I have more and more of them. How can I fix this problem?
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You seem to have defined two signals which you are not emitting, so I'll assume you want them to be props If you want to be able to connect to Didn't actually test this, but it should work. The Note: the import Service from 'resource:///com/github/Aylur/ags/service.js';
import Mpris, { MprisPlayer } from 'resource:///com/github/Aylur/ags/service/mpris.js';
import App from 'resource:///com/github/Aylur/ags/app.js';
import { execAsync } from 'resource:///com/github/Aylur/ags/utils.js';
class MaterialColors extends Service {
static {
Service.register(this, {}, {
'colors': ['jsobject'],
'covers-paths': ['jsobject'],
});
}
#colors = new Map();
#coverPaths = new Map();
#connections = new Map();
get colors() { return this.#colors; }
get cover_paths() { return this.#coverPaths; }
constructor() {
super();
Mpris.connect('player-added', (_, busName) => {
const player = Mpris.getPlayer(busName);
if (this.#connections.has(player))
return;
const id = player.connect(
'notify::trackid',
this.#onTrackId.bind(this),
);
this.#connections.set(player, id);
});
Mpris.connect('player-closed', (_, busName) => {
const player = Mpris.getPlayer(busName);
if (!this.#connections.has(player))
return;
const id = this.#connections.get(player);
player.disconnect(id);
this.#connections.delete(player);
});
}
/** @param {MprisPlayer} player */
#onTrackId(player) {
console.log(player._trackid);
execAsync(`python ${App.configDir}/bin/getCoverColors ${player.coverPath}`)
.then(commandString => {
this.#colors.set(player.busName, JSON.parse(commandString));
this.#coverPaths.set(player.busName, player.coverPath);
this.notify('colors');
this.notify('cover-paths');
this.emit('changed');
})
.catch(err => console.error(err.message));
}
}
export default new MaterialColors(); |
Beta Was this translation helpful? Give feedback.
You seem to have defined two signals which you are not emitting, so I'll assume you want them to be props
If you want to be able to connect to
notify::colors
on this service, then fix the register call, and emit a notify.Didn't actually test this, but it should work. The
player-closed
connection might be unnecessary, not sure, but just to be safe its there.Note: the
#prop
is a js private member