// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Collections.Generic; using System.Threading.Tasks; using osu.Framework.Allocation; using osu.Framework.Configuration; using osu.Game.Online; using osu.Game.Online.API; using osu.Game.Online.API.Requests; using osu.Game.Online.Multiplayer; using osu.Game.Screens.Multi.Lounge.Components; namespace osu.Game.Screens.Multi { public class RoomPollingComponent : PollingComponent { /// /// Invoked when s have been retrieved from the API. /// public Action> RoomsRetrieved; /// /// The to use when polling for s. /// public readonly Bindable Filter = new Bindable(); [Resolved] private APIAccess api { get; set; } public RoomPollingComponent() { Filter.BindValueChanged(_ => { if (IsLoaded) PollImmediately(); }); } private GetRoomsRequest pollReq; protected override Task Poll() { if (!api.IsLoggedIn) return base.Poll(); var tcs = new TaskCompletionSource(); pollReq?.Cancel(); pollReq = new GetRoomsRequest(Filter.Value.PrimaryFilter); pollReq.Success += result => { RoomsRetrieved?.Invoke(result); tcs.SetResult(true); }; pollReq.Failure += _ => tcs.SetResult(false); api.Queue(pollReq); return tcs.Task; } } }