Fix potential doubling of events

This commit is contained in:
smoogipoo 2021-04-20 21:20:08 +09:00
parent a9e4a0ed50
commit 4cc3321d54

View File

@ -302,14 +302,15 @@ namespace osu.Game.Online.Spectator
/// <param name="callback">The action to perform when a user begins playing.</param> /// <param name="callback">The action to perform when a user begins playing.</param>
/// <param name="runOnceImmediately">Whether the action provided in <paramref name="callback"/> should be run once immediately for all users currently playing.</param> /// <param name="runOnceImmediately">Whether the action provided in <paramref name="callback"/> should be run once immediately for all users currently playing.</param>
public void BindUserBeganPlaying(Action<int, SpectatorState> callback, bool runOnceImmediately = false) public void BindUserBeganPlaying(Action<int, SpectatorState> callback, bool runOnceImmediately = false)
{
// The lock is taken before the event is subscribed to to prevent doubling of events.
lock (userLock)
{ {
OnUserBeganPlaying += callback; OnUserBeganPlaying += callback;
if (!runOnceImmediately) if (!runOnceImmediately)
return; return;
lock (userLock)
{
foreach (var (userId, state) in playingUserStates) foreach (var (userId, state) in playingUserStates)
callback(userId, state); callback(userId, state);
} }