diff --git a/osu.Game.Tests/Visual/Gameplay/TestSceneSpectator.cs b/osu.Game.Tests/Visual/Gameplay/TestSceneSpectator.cs index ea66144b21..def662d3ea 100644 --- a/osu.Game.Tests/Visual/Gameplay/TestSceneSpectator.cs +++ b/osu.Game.Tests/Visual/Gameplay/TestSceneSpectator.cs @@ -221,7 +221,7 @@ namespace osu.Game.Tests.Visual.Gameplay callbackInvoked = false; }); - AddStep("bind event again", () => testSpectatorStreamingClient.OnUserBeganPlaying += callbackAction); + AddStep("bind event with run once immediately", () => testSpectatorStreamingClient.BindUserBeganPlaying(callbackAction, true)); AddAssert("callback invoked", () => callbackInvoked); // Don't leave the event bound if test run succeeded. diff --git a/osu.Game/Online/Spectator/SpectatorStreamingClient.cs b/osu.Game/Online/Spectator/SpectatorStreamingClient.cs index 7bea49e102..4bbc420223 100644 --- a/osu.Game/Online/Spectator/SpectatorStreamingClient.cs +++ b/osu.Game/Online/Spectator/SpectatorStreamingClient.cs @@ -69,25 +69,10 @@ namespace osu.Game.Online.Spectator /// public event Action OnNewFrames; - private event Action onUserBeganPlaying; - /// /// Called whenever a user starts a play session, or immediately if the user is being watched and currently in a play session. /// - public event Action OnUserBeganPlaying - { - add - { - onUserBeganPlaying += value; - - lock (userLock) - { - foreach (var (userId, state) in currentUserStates) - value?.Invoke(userId, state); - } - } - remove => onUserBeganPlaying -= value; - } + public event Action OnUserBeganPlaying; /// /// Called whenever a user finishes a play session. @@ -153,7 +138,7 @@ namespace osu.Game.Online.Spectator lock (userLock) currentUserStates[userId] = state; - onUserBeganPlaying?.Invoke(userId, state); + OnUserBeganPlaying?.Invoke(userId, state); return Task.CompletedTask; } @@ -290,5 +275,24 @@ namespace osu.Game.Online.Spectator lastSendTime = Time.Current; } + + /// + /// Bind an action to with the option of running the bound action once immediately. + /// + /// The action to perform when a user begins playing. + /// Whether the action provided in should be run once immediately for all users currently playing. + public void BindUserBeganPlaying(Action callback, bool runOnceImmediately = false) + { + OnUserBeganPlaying += callback; + + if (!runOnceImmediately) + return; + + lock (userLock) + { + foreach (var (userId, state) in currentUserStates) + callback(userId, state); + } + } } }