Skip to content

Commit

Permalink
feat: updated track collection with max item threshold, lastfm await …
Browse files Browse the repository at this point in the history
…nowplaying update
  • Loading branch information
Venipa committed Apr 18, 2024
1 parent ed6052b commit 875d5f5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ytmdesktop2",
"version": "0.12.5",
"version": "0.12.6",
"private": false,
"author": "Venipa <admin@venipa.net>",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/app/lib/lastfm/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ export class LastFMClient {
}
async updateNowPlaying(...tracks: { artist: string, track: string, album?: string, duration?: number }[]) {
if (!this.session) throw new Error("Invalid session");
const updatePromise = this.callMethod("track.updateNowPlaying", "post", {
return await Promise.resolve(this.callMethod("track.updateNowPlaying", "post", {
query: {
sk: this.session,
...tracks[0]
}
})
}))
}
async scrobble(...tracks: { artist: string, track: string, timestamp: number, album?: string, duration?: number }[]) {
if (!this.session) throw new Error("Invalid session");
Expand Down
37 changes: 32 additions & 5 deletions src/app/plugins/trackProvider.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,33 @@ type TrackState = {
liked: boolean;
disliked: boolean;
};
const tracks: {
[id: string]: TrackData;
} = {};
type TrackEntry = { id: string } & TrackData;
const trackCollection = new class {
private _tracks = new Array<TrackEntry>();
constructor() { }
private _add(id: string, value: TrackEntry) {
if (this._tracks.length > 10) this._tracks.shift();
value.id = id;
this._tracks.push(value);
}
private _update(id: string, value: TrackEntry, idx?: number) {
this._tracks.splice(idx ?? this._tracks.findIndex(x => x.id === id), 1, value);
}
addOrUpdate(id: string, value: Omit<TrackEntry, "id">) {
const idx = this._tracks.findIndex(x => x.id === id);
if (idx === -1) this._add(id, value as TrackEntry);
else this._update(id, value as TrackEntry, idx);
return value;
}

remove(id: string) {
return this._tracks.splice(this._tracks.findIndex(x => x.id === id), 1);
}
findById(id: string) {
return this._tracks.find(x => x.id === id);
}
}

const parseTrackDuration = (td: TrackData) => {

return ((dur) => (dur ? Number.parseInt(dur) : null))(
Expand Down Expand Up @@ -55,8 +79,10 @@ export default class TrackProvider extends BaseProvider implements AfterInit {
super("track");
}
async AfterInit() { }
private _trackDataCache: TrackEntry;
get trackData() {
return tracks[this._activeTrackId];
if (this._trackDataCache?.id === this._activeTrackId) return this._trackDataCache;
return this._trackDataCache = trackCollection.findById(this._activeTrackId);
}
async getActiveTrackByDOM() {
return this.views.youtubeView.webContents
Expand All @@ -68,7 +94,7 @@ export default class TrackProvider extends BaseProvider implements AfterInit {
@IpcOn("track:info-req")
private async __onTrackInfo(ev, ytTrack: TrackData) {
if (!ytTrack.video) return;
const track = tracks[ytTrack.video.videoId] = {
const track = {
...ytTrack,
meta: {
thumbnail: (ytTrack?.video?.thumbnail?.thumbnails ?? ytTrack?.context?.thumbnail?.thumbnails)?.sort(firstBy(d => d.height, 'desc'))[0]?.url,
Expand All @@ -77,6 +103,7 @@ export default class TrackProvider extends BaseProvider implements AfterInit {
duration: parseTrackDuration(ytTrack)
}
};
trackCollection.addOrUpdate(ytTrack.video.videoId, ytTrack);

if (
track.video.videoId === this._activeTrackId ||
Expand Down

0 comments on commit 875d5f5

Please sign in to comment.