Change to explicit method instead

This commit is contained in:
smoogipoo 2021-04-16 17:29:42 +09:00
parent 5652490d61
commit ca74f413cd
2 changed files with 22 additions and 18 deletions

View File

@ -221,7 +221,7 @@ public void OnUserBeganPlayingCallbackInvokedOnNewAdd()
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.

View File

@ -69,25 +69,10 @@ public class SpectatorStreamingClient : Component, ISpectatorClient
/// </summary>
public event Action<int, FrameDataBundle> OnNewFrames;
private event Action<int, SpectatorState> onUserBeganPlaying;
/// <summary>
/// Called whenever a user starts a play session, or immediately if the user is being watched and currently in a play session.
/// </summary>
public event Action<int, SpectatorState> OnUserBeganPlaying
{
add
{
onUserBeganPlaying += value;
lock (userLock)
{
foreach (var (userId, state) in currentUserStates)
value?.Invoke(userId, state);
}
}
remove => onUserBeganPlaying -= value;
}
public event Action<int, SpectatorState> OnUserBeganPlaying;
/// <summary>
/// Called whenever a user finishes a play session.
@ -153,7 +138,7 @@ Task ISpectatorClient.UserBeganPlaying(int userId, SpectatorState state)
lock (userLock)
currentUserStates[userId] = state;
onUserBeganPlaying?.Invoke(userId, state);
OnUserBeganPlaying?.Invoke(userId, state);
return Task.CompletedTask;
}
@ -290,5 +275,24 @@ private void purgePendingFrames()
lastSendTime = Time.Current;
}
/// <summary>
/// Bind an action to <see cref="OnUserBeganPlaying"/> with the option of running the bound action once immediately.
/// </summary>
/// <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>
public void BindUserBeganPlaying(Action<int, SpectatorState> callback, bool runOnceImmediately = false)
{
OnUserBeganPlaying += callback;
if (!runOnceImmediately)
return;
lock (userLock)
{
foreach (var (userId, state) in currentUserStates)
callback(userId, state);
}
}
}
}