1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-19 01:03:02 +08:00
osu-lazer/osu.Game/Screens/OnlinePlay/Components/SelectionPollingComponent.cs

54 lines
1.4 KiB
C#
Raw Normal View History

2020-12-18 23:15:41 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System.Threading.Tasks;
using osu.Framework.Allocation;
2020-12-25 12:38:11 +08:00
using osu.Game.Online.Rooms;
2020-12-18 23:15:41 +08:00
namespace osu.Game.Screens.OnlinePlay.Components
2020-12-18 23:15:41 +08:00
{
/// <summary>
/// A <see cref="RoomPollingComponent"/> that polls for the currently-selected room.
/// </summary>
public class SelectionPollingComponent : RoomPollingComponent
{
[Resolved]
private IRoomManager roomManager { get; set; }
private readonly Room room;
public SelectionPollingComponent(Room room)
2020-12-18 23:15:41 +08:00
{
this.room = room;
2020-12-18 23:15:41 +08:00
}
private GetRoomRequest pollReq;
protected override Task Poll()
{
if (!API.IsLoggedIn)
return base.Poll();
if (room.RoomID.Value == null)
2020-12-18 23:15:41 +08:00
return base.Poll();
var tcs = new TaskCompletionSource<bool>();
pollReq?.Cancel();
pollReq = new GetRoomRequest(room.RoomID.Value.Value);
2020-12-18 23:15:41 +08:00
pollReq.Success += result =>
{
RoomManager.AddOrUpdateRoom(result);
2020-12-18 23:15:41 +08:00
tcs.SetResult(true);
};
pollReq.Failure += _ => tcs.SetResult(false);
API.Queue(pollReq);
return tcs.Task;
}
}
}