Skip to content

Commit

Permalink
feat(me): add batches to library (un)saving
Browse files Browse the repository at this point in the history
  • Loading branch information
netlob committed Sep 7, 2024
1 parent 3464e3b commit 721cc91
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/lib/me/MeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,25 @@ export class MeManager extends Manager {
* @param {string} ids Array of IDs.
*/
async saveTracks(ids: string[]): Promise<void> {
await this.http.put(`/v1/me/tracks`, {
ids
});
// Use an async loop to preserve the order of saves
for (let i = 0; i < ids.length; i += 50) {
await this.http.put(`/v1/me/tracks`, {
ids: ids.slice(i, i + 50)
});
}
}

/**
* @description Remove multiple saved tracks by ID. (required scropes: user-library-read).
* @param {string} ids Array of IDs.
*/
async unsaveTracks(ids: string[]): Promise<void> {
await this.http.delete(`/v1/me/tracks`, {
query: { ids: ids.join(',') }
});
// Use an async loop to preserve the order of unsaves
for (let i = 0; i < ids.length; i += 50) {
await this.http.delete(`/v1/me/tracks`, {
query: { ids: ids.slice(i, i + 50).join(',') }
});
}
}

async playlists(options?: {
Expand Down

0 comments on commit 721cc91

Please sign in to comment.