Skip to content

Commit 5f1bccc

Browse files
pdeschainfernando-cortezSamuelBellomo
authored
feat: PubSub improvement: ISubscriber unsub [MTT-2765] (#612)
* ISubscriber now contains Unsubscribe method * Used the new API to unsubscribe directly from an ISubscriber - better readability in trivial cases * added changelog * Update Assets/Scripts/Gameplay/UI/IPConnectionWindow.cs Co-authored-by: Fernando Cortez <[email protected]> * Addressing comments * removed unnecessary assert in MessageChannel Unsubscribe and replaced it with conditional Co-authored-by: Fernando Cortez <[email protected]> Co-authored-by: Sam Bellomo <[email protected]>
1 parent 3b36b09 commit 5f1bccc

File tree

9 files changed

+37
-34
lines changed

9 files changed

+37
-34
lines changed

Assets/Scripts/Gameplay/GameState/ServerBossRoomState.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ public class ServerBossRoomState : GameStateBehaviour
5959
/// </summary>
6060
[Inject] ISubscriber<LifeStateChangedEventMessage> m_LifeStateChangedEventMessageSubscriber;
6161

62-
IDisposable m_Subscription;
63-
6462
[Inject] ConnectionManager m_ConnectionManager;
6563
[Inject] PersistentGameState m_PersistentGameState;
6664

@@ -79,7 +77,7 @@ void OnNetworkSpawn()
7977
return;
8078
}
8179
m_PersistentGameState.Reset();
82-
m_Subscription = m_LifeStateChangedEventMessageSubscriber.Subscribe(OnLifeStateChangedEventMessage);
80+
m_LifeStateChangedEventMessageSubscriber.Subscribe(OnLifeStateChangedEventMessage);
8381

8482
NetworkManager.Singleton.OnClientDisconnectCallback += OnClientDisconnect;
8583
NetworkManager.Singleton.SceneManager.OnLoadEventCompleted += OnLoadEventCompleted;
@@ -90,16 +88,15 @@ void OnNetworkSpawn()
9088

9189
void OnNetworkDespawn()
9290
{
93-
m_Subscription?.Dispose();
94-
91+
m_LifeStateChangedEventMessageSubscriber?.Unsubscribe(OnLifeStateChangedEventMessage);
9592
NetworkManager.Singleton.OnClientDisconnectCallback -= OnClientDisconnect;
9693
NetworkManager.Singleton.SceneManager.OnLoadEventCompleted -= OnLoadEventCompleted;
9794
NetworkManager.Singleton.SceneManager.OnSynchronizeComplete -= OnSynchronizeComplete;
9895
}
9996

10097
protected override void OnDestroy()
10198
{
102-
m_Subscription?.Dispose();
99+
m_LifeStateChangedEventMessageSubscriber?.Unsubscribe(OnLifeStateChangedEventMessage);
103100

104101
if (m_NetcodeHooks)
105102
{

Assets/Scripts/Gameplay/UI/IPConnectionWindow.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ public class IPConnectionWindow : MonoBehaviour
2020

2121
[Inject] IPUIMediator m_IPUIMediator;
2222

23-
IDisposable m_Subscription;
23+
ISubscriber<ConnectStatus> m_ConnectStatusSubscriber;
2424

2525
[Inject]
2626
void InjectDependencies(ISubscriber<ConnectStatus> connectStatusSubscriber)
2727
{
28-
m_Subscription = connectStatusSubscriber.Subscribe(OnConnectStatusMessage);
28+
m_ConnectStatusSubscriber = connectStatusSubscriber;
29+
m_ConnectStatusSubscriber.Subscribe(OnConnectStatusMessage);
2930
}
3031

3132
void Awake()
@@ -35,7 +36,7 @@ void Awake()
3536

3637
void OnDestroy()
3738
{
38-
m_Subscription.Dispose();
39+
m_ConnectStatusSubscriber?.Unsubscribe(OnConnectStatusMessage);
3940
}
4041

4142
void OnConnectStatusMessage(ConnectStatus connectStatus)

Assets/Scripts/Gameplay/UI/IPUIMediator.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ public class IPUIMediator : MonoBehaviour
4141

4242
public IPHostingUI IPHostingUI => m_IPHostingUI;
4343

44-
IDisposable m_Subscription;
44+
ISubscriber<ConnectStatus> m_ConnectStatusSubscriber;
4545

4646
[Inject]
4747
void InjectDependencies(ISubscriber<ConnectStatus> connectStatusSubscriber)
4848
{
49-
m_Subscription = connectStatusSubscriber.Subscribe(OnConnectStatusMessage);
49+
m_ConnectStatusSubscriber = connectStatusSubscriber;
50+
m_ConnectStatusSubscriber.Subscribe(OnConnectStatusMessage);
5051
}
5152

5253
void Awake()
@@ -63,7 +64,7 @@ void Start()
6364

6465
void OnDestroy()
6566
{
66-
m_Subscription.Dispose();
67+
m_ConnectStatusSubscriber?.Unsubscribe(OnConnectStatusMessage);
6768
}
6869

6970
void OnConnectStatusMessage(ConnectStatus connectStatus)

Assets/Scripts/Gameplay/UI/Lobby/LobbyJoiningUI.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public class LobbyJoiningUI : MonoBehaviour
2828
IObjectResolver m_Container;
2929
LobbyUIMediator m_LobbyUIMediator;
3030
UpdateRunner m_UpdateRunner;
31-
IDisposable m_Subscriptions;
31+
ISubscriber<LobbyListFetchedMessage> m_LocalLobbiesRefreshedSub;
32+
3233
List<LobbyListItemUI> m_LobbyListItems = new List<LobbyListItemUI>();
3334

3435
void Awake()
@@ -40,13 +41,13 @@ void OnDisable()
4041
{
4142
if (m_UpdateRunner != null)
4243
{
43-
m_UpdateRunner.Unsubscribe(PeriodicRefresh);
44+
m_UpdateRunner?.Unsubscribe(PeriodicRefresh);
4445
}
4546
}
4647

4748
void OnDestroy()
4849
{
49-
m_Subscriptions?.Dispose();
50+
m_LocalLobbiesRefreshedSub?.Unsubscribe(UpdateUI);
5051
}
5152

5253
[Inject]
@@ -59,7 +60,8 @@ void InjectDependenciesAndInitialize(
5960
m_Container = container;
6061
m_LobbyUIMediator = lobbyUIMediator;
6162
m_UpdateRunner = updateRunner;
62-
m_Subscriptions = localLobbiesRefreshedSub.Subscribe(UpdateUI);
63+
m_LocalLobbiesRefreshedSub = localLobbiesRefreshedSub;
64+
m_LocalLobbiesRefreshedSub.Subscribe(UpdateUI);
6365
}
6466

6567
/// <summary>

Assets/Scripts/Gameplay/UI/Lobby/LobbyUIMediator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class LobbyUIMediator : MonoBehaviour
2929
LocalLobby m_LocalLobby;
3030
NameGenerationData m_NameGenerationData;
3131
ConnectionManager m_ConnectionManager;
32-
IDisposable m_Subscriptions;
32+
ISubscriber<ConnectStatus> m_ConnectStatusSubscriber;
3333

3434
const string k_DefaultLobbyName = "no-name";
3535

@@ -50,10 +50,10 @@ ConnectionManager connectionManager
5050
m_LobbyServiceFacade = lobbyServiceFacade;
5151
m_LocalLobby = localLobby;
5252
m_ConnectionManager = connectionManager;
53-
53+
m_ConnectStatusSubscriber = connectStatusSub;
5454
RegenerateName();
5555

56-
m_Subscriptions = connectStatusSub.Subscribe(OnConnectStatus);
56+
m_ConnectStatusSubscriber.Subscribe(OnConnectStatus);
5757
}
5858

5959
void OnConnectStatus(ConnectStatus status)
@@ -66,7 +66,7 @@ void OnConnectStatus(ConnectStatus status)
6666

6767
void OnDestroy()
6868
{
69-
m_Subscriptions?.Dispose();
69+
m_ConnectStatusSubscriber?.Unsubscribe(OnConnectStatus);
7070
}
7171

7272
//Lobby and Relay calls done from UI

Assets/Scripts/Gameplay/UI/UnityServicesUIHandler.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Unity.BossRoom.Gameplay.UI
1010
{
1111
public class UnityServicesUIHandler : MonoBehaviour
1212
{
13-
IDisposable m_Subscriptions;
13+
ISubscriber<UnityServiceErrorMessage> m_ServiceErrorSubscription;
1414

1515
void Awake()
1616
{
@@ -20,7 +20,8 @@ void Awake()
2020
[Inject]
2121
void Initialize(ISubscriber<UnityServiceErrorMessage> serviceError)
2222
{
23-
m_Subscriptions = serviceError.Subscribe(ServiceErrorHandler);
23+
m_ServiceErrorSubscription = serviceError;
24+
m_ServiceErrorSubscription.Subscribe(ServiceErrorHandler);
2425
}
2526

2627
void ServiceErrorHandler(UnityServiceErrorMessage error)
@@ -86,7 +87,7 @@ void HandleLobbyError(UnityServiceErrorMessage error)
8687

8788
void OnDestroy()
8889
{
89-
m_Subscriptions?.Dispose();
90+
m_ServiceErrorSubscription?.Unsubscribe(ServiceErrorHandler);
9091
}
9192
}
9293
}

Assets/Scripts/Infrastructure/PubSub/IMessageChannel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ public interface IPublisher<T>
1010
public interface ISubscriber<T>
1111
{
1212
IDisposable Subscribe(Action<T> handler);
13+
void Unsubscribe(Action<T> handler);
1314
}
1415

1516
public interface IMessageChannel<T> : IPublisher<T>, ISubscriber<T>, IDisposable
1617
{
1718
bool IsDisposed { get; }
18-
void Unsubscribe(Action<T> handler);
1919
}
2020

2121
public interface IBufferedMessageChannel<T> : IMessageChannel<T>

Assets/Scripts/Infrastructure/PubSub/MessageChannel.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,19 @@ public virtual IDisposable Subscribe(Action<T> handler)
6868

6969
public void Unsubscribe(Action<T> handler)
7070
{
71-
Assert.IsTrue(IsSubscribed(handler), "Attempting to unsubscribe with a handler that is not subscribed");
72-
73-
if (m_PendingHandlers.ContainsKey(handler))
71+
if (IsSubscribed(handler))
7472
{
75-
if (m_PendingHandlers[handler])
73+
if (m_PendingHandlers.ContainsKey(handler))
7674
{
77-
m_PendingHandlers.Remove(handler);
75+
if (m_PendingHandlers[handler])
76+
{
77+
m_PendingHandlers.Remove(handler);
78+
}
79+
}
80+
else
81+
{
82+
m_PendingHandlers[handler] = false;
7883
}
79-
}
80-
else
81-
{
82-
m_PendingHandlers[handler] = false;
8384
}
8485
}
8586

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1919
* NetworkObjectSpawner handles dynamically spawning in-scene placed NetworkObjects (#717) - You can't place a NetworkObject in scene directly and destroy it at runtime. This PR showcases proper handling of NetworkObjects that you'd wish to place inside of scenes, but would still want to destroy at game-time. Examples of these are: Imps, VandalImps, ImpBoss. NetworkObjects such as doors, crystals, door switch, etc. remain the same, statically-placed in scene.
2020
* Quality levels settings set up for Desktop [MTT-4450] (#713)
2121
* Added custom RNSM config with graph for RTT instead of single value (#747)
22-
22+
* Added Unsubscribe API for the ISubscriber<T> along with refactoring of the codebase to use this API instead of IDisposable handle when there is just one subscription (#612)
2323
### Changed
2424
* Updated tools, authentication and relay packages (#690)
2525
* Replaced our dependency injection solution with VContainer. (#679)

0 commit comments

Comments
 (0)