Add thread safety to PollingComponent.Poll implementations

This commit is contained in:
Dean Herbert 2022-01-28 13:43:53 +09:00
parent d1158acb82
commit c44af4853d
2 changed files with 17 additions and 12 deletions

View File

@ -33,7 +33,7 @@ namespace osu.Game.Screens.OnlinePlay.Components
}); });
} }
private GetRoomsRequest pollReq; private GetRoomsRequest lastPollRequest;
protected override Task Poll() protected override Task Poll()
{ {
@ -45,10 +45,11 @@ namespace osu.Game.Screens.OnlinePlay.Components
var tcs = new TaskCompletionSource<bool>(); var tcs = new TaskCompletionSource<bool>();
pollReq?.Cancel(); lastPollRequest?.Cancel();
pollReq = new GetRoomsRequest(Filter.Value.Status, Filter.Value.Category);
pollReq.Success += result => var req = new GetRoomsRequest(Filter.Value.Status, Filter.Value.Category);
req.Success += result =>
{ {
foreach (var existing in RoomManager.Rooms.ToArray()) foreach (var existing in RoomManager.Rooms.ToArray())
{ {
@ -66,10 +67,11 @@ namespace osu.Game.Screens.OnlinePlay.Components
tcs.SetResult(true); tcs.SetResult(true);
}; };
pollReq.Failure += _ => tcs.SetResult(false); req.Failure += _ => tcs.SetResult(false);
API.Queue(pollReq); API.Queue(req);
lastPollRequest = req;
return tcs.Task; return tcs.Task;
} }
} }

View File

@ -18,7 +18,7 @@ namespace osu.Game.Screens.OnlinePlay.Components
this.room = room; this.room = room;
} }
private GetRoomRequest pollReq; private GetRoomRequest lastPollRequest;
protected override Task Poll() protected override Task Poll()
{ {
@ -30,19 +30,22 @@ namespace osu.Game.Screens.OnlinePlay.Components
var tcs = new TaskCompletionSource<bool>(); var tcs = new TaskCompletionSource<bool>();
pollReq?.Cancel(); lastPollRequest?.Cancel();
pollReq = new GetRoomRequest(room.RoomID.Value.Value);
pollReq.Success += result => var req = new GetRoomRequest(room.RoomID.Value.Value);
req.Success += result =>
{ {
result.RemoveExpiredPlaylistItems(); result.RemoveExpiredPlaylistItems();
RoomManager.AddOrUpdateRoom(result); RoomManager.AddOrUpdateRoom(result);
tcs.SetResult(true); tcs.SetResult(true);
}; };
pollReq.Failure += _ => tcs.SetResult(false); req.Failure += _ => tcs.SetResult(false);
API.Queue(pollReq); API.Queue(req);
lastPollRequest = req;
return tcs.Task; return tcs.Task;
} }