Skip to content
This repository has been archived by the owner on Jan 10, 2021. It is now read-only.

Commit

Permalink
Fixed a bug in awaiting a track from a link.
Browse files Browse the repository at this point in the history
 (possibly awaiting other things from links too. not tested these though)
Added a "Test" to the Console TestClient
  • Loading branch information
woellij committed Feb 4, 2016
1 parent 4ce86a6 commit 6d6988e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
44 changes: 32 additions & 12 deletions SpotiFire.SpotifyLib/SpotifyLibExtensions.cs
Expand Up @@ -3,8 +3,8 @@
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;

namespace SpotiFire
{
Expand Down Expand Up @@ -45,36 +45,53 @@ public static Search SearchPlaylist(this Session session, string query, int play

private readonly static List<Tuple<IAsyncLoaded, TaskCompletionSource<IAsyncLoaded>>> waiting = new List<Tuple<IAsyncLoaded, TaskCompletionSource<IAsyncLoaded>>>();

private readonly static Timer _timer = new Timer(OnTimerTick, null, Timeout.Infinite, 100);
private readonly static Timer _timer = new Timer(20);

static SessionExtensions()
{
_timer.Elapsed += (sender, args) => OnTimerTick(null);
}

private static void OnTimerTick(object state)
{
List<Tuple<IAsyncLoaded, TaskCompletionSource<IAsyncLoaded>>> finished = null;

List<Tuple<IAsyncLoaded, TaskCompletionSource<IAsyncLoaded>>> awaited;
lock (waiting)
{
if (waiting.Any())
// let's make a copy of the list so we don't run into problems removing items while iterating over it
awaited = waiting.ToList();
}
if (awaited.Any())
{
finished = new List<Tuple<IAsyncLoaded, TaskCompletionSource<IAsyncLoaded>>>();
foreach (Tuple<IAsyncLoaded, TaskCompletionSource<IAsyncLoaded>> t in awaited)
{
finished = new List<Tuple<IAsyncLoaded, TaskCompletionSource<IAsyncLoaded>>>();
foreach (Tuple<IAsyncLoaded, TaskCompletionSource<IAsyncLoaded>> t in waiting)
if (t.Item1.IsLoaded && t.Item1.IsReady)
{
if (t.Item1.IsLoaded && t.Item1.IsReady)
{
finished.Add(t);
waiting.Remove(t);
}
finished.Add(t);
waiting.Remove(t);
}
}
}

if (finished != null)
{
foreach (Tuple<IAsyncLoaded, TaskCompletionSource<IAsyncLoaded>> t in finished)
{
t.Item2.TrySetResult(t.Item1);
}
}

lock (waiting) {
if (waiting.Count == 0) {
// stop the time as long as it's not needed
_timer.Stop();
}
}
}



// Load made a task
private static Task<IAsyncLoaded> Load(this IAsyncLoaded loadable)
{
Expand All @@ -89,6 +106,9 @@ private static Task<IAsyncLoaded> Load(this IAsyncLoaded loadable)
{
waiting.Add(new Tuple<IAsyncLoaded, TaskCompletionSource<IAsyncLoaded>>(loadable, tcs));
}
if (!_timer.Enabled) {
_timer.Start();
}
}
return tcs.Task;
}
Expand Down Expand Up @@ -166,7 +186,7 @@ public static Task Play(this Session session, Track track)
session.PlayerPlay();

return tcs.Task;
}
}
}

public static class PlaylistExtensions
Expand Down
10 changes: 10 additions & 0 deletions SpotiFire.TestClient/Program.cs
Expand Up @@ -107,6 +107,8 @@ static async Task Run()
await session.Login(Console.ReadLine(), Console.ReadLine(), false);
session.PreferredBitrate = BitRate.Bitrate320k;

await TestAwaitLink("spotify:track:0yA1MBQ60SoiYt7xqdS3H1", session);

await session.PlaylistContainer;
while (session.PlaylistContainer.Playlists.Count < 1)
{
Expand Down Expand Up @@ -167,6 +169,14 @@ static async Task Run()
await session.Logout();
}

private static async Task TestAwaitLink(string trackUri, Session session)
{
var link = session.ParseLink(trackUri);
Console.WriteLine("Awaiting the Link as a Track. Should write Track title next.");
var track = await link.AsTrack();
Console.WriteLine("Successfully awaited Track with title {0}.", track.Name);
}

static async Task PlayPlaylistForever(Session session, Playlist starred)
{
int num = 0;
Expand Down

0 comments on commit 6d6988e

Please sign in to comment.