0

I have a problem when executing this code:

require(['$api/models','$api/library#Library'], function(models,Library) {

// THIS ONLY HAPPEN FOR TOP LIST
var uri=Library.forCurrentUser().toplist.uri;
// IF YOU USE ANY OTHER PLAYLIST IT WORKS FINE
//  var uri="spotify:user:vdesabou:playlist:0xy2zExFmPzJZsY0X0bCC5";

var playlist = models.Playlist.fromURI(uri);
playlist.load('tracks').done(function() {
    console.log("loaded 1");
playlist.tracks.snapshot().done(function(snapshot) {
    console.log("snapshot length 1 " + snapshot.length);

    snapshot.loadAll('name')
       .done(function(snap_tracks) { console.log("loaded tracks length 1 " + snap_tracks.length);    })
       .fail(function() { console.log("loadAll failed"); });
    }).fail(function() { console.log("snapshot failed"); });

}).fail(function() { console.log("playlist load tracks failed"); });

});

If I execute multiple time (by reloading my application), I don't get results about 1 time out of 3

When it doesn't work:

loaded 1

When it works I get:

loaded 1
snapshot length 1 20 
loaded tracks length 1 20 

This is happening only for top list playlist, any other playlist is ok.

What could be wrong? Thanks

Thomas
  • 3,348
  • 4
  • 35
  • 49
vdesabou
  • 17
  • 4
  • What if you named them playlist1, playlist2, and playlist3? I don't know that it would help, but might help shed light on the issue. You could also put 1,2,3 on your log messages. – Thomas Dec 14 '13 at 21:35
  • I can't rename the playlist because it is the top list. I have updated my ordinal post to show you how I get the top list uri with Library.forCurrentUser().toplist.uri . It is only happening with top list, any other playlist is working fine. – vdesabou Dec 15 '13 at 10:08

2 Answers2

0

Either it's a localized issue (I can't repro), or due to the asynchronous nature of the functions, you can't make the same call twice on the same playlist.

I've tried your code 3 times (on Windows v0.9.6) without seeing the behavior you're seeing.

Renaming playlist1,2,3 didn't change the behavior for me, works both ways.

Thomas
  • 3,348
  • 4
  • 35
  • 49
  • I bet for a localized issue then (I'm using OSX 10.9) because the asynchronous nature of functions is not a problem for all regular playlists, this problem only happening with top list. I'm not sure to understand how you could rename the playlists? You can't do that with a top list I think. Note that I've updated the code in my original post to make it clear I use the URI of my top list and that it is not related to execute same code multiple time. Even with one execution I see the problem. Can you try again on your side? Thanks for your help – vdesabou Dec 15 '13 at 10:21
  • I meant rename the playlist variable. Will try updated code shortly – Thomas Dec 16 '13 at 14:00
  • I was able to repro on my 6th try. All I can think is to set a flag on success, and also make a timer to rerun. Or going with your original 'run it 3 times' solution, set a flag so you just use the first response. – Thomas Dec 16 '13 at 14:37
  • Is there any way to report this to Spotify to get it fixed instead? – vdesabou Dec 16 '13 at 15:31
  • There is no official way to get things fixed, that I know of. Trying to get ahold of Spotify employees on #spotify on freenode or writing on the forums are they only options I know of. A few other times I ran it, it did complete after longer than expected (7 seconds) – Thomas Dec 16 '13 at 17:03
0

Library's toplist property is already a playlist, so you don't need to create a new Playlist object from its URI. This snippet does the same as yours, but loads the toplist property and uses it to get the tracks directly instead of creating a new playlist.

require(['$api/models','$api/library#Library'], function(models,Library) {

  var library = Library.forCurrentUser();
  library.load("toplist").done(function() {
    var toplist = library.toplist;

    toplist.load('tracks').done(function() {
      console.log("loaded 1");
      toplist.tracks.snapshot().done(function(snapshot) {
        console.log("snapshot length 1 " + snapshot.length);

        snapshot.loadAll('name')
          .done(function(snap_tracks) { console.log("loaded tracks length 1 " + snap_tracks.length);    })
          .fail(function() { console.log("loadAll failed"); });

      }).fail(function() { console.log("snapshot failed"); });

    }).fail(function() { console.log("playlist load tracks failed"); });

  }).fail(function() {
    console.log("Could not load toplist.");
  });

});

i did try the snippet you provided, and similarly to Thomas I couldn't find anything wrong with it. Hope this helps though.

Michael Thelin
  • 4,710
  • 3
  • 23
  • 29