From c4a44878a22b0c0f38ce496b66b1c4c4f48e11ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dobrza=C5=84ski?= Date: Mon, 26 Jun 2023 16:44:38 +0200 Subject: [PATCH 01/10] WIP remove abstract from interface, add transition utils --- .../EventEngine/Core/EventEngineInterfaces.cs | 9 +++++---- src/Api/PubnubApi/EventEngine/Core/Utils.cs | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 src/Api/PubnubApi/EventEngine/Core/Utils.cs diff --git a/src/Api/PubnubApi/EventEngine/Core/EventEngineInterfaces.cs b/src/Api/PubnubApi/EventEngine/Core/EventEngineInterfaces.cs index 201f027de..c71f0df24 100644 --- a/src/Api/PubnubApi/EventEngine/Core/EventEngineInterfaces.cs +++ b/src/Api/PubnubApi/EventEngine/Core/EventEngineInterfaces.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using System.Collections.Generic; namespace PubnubApi.PubnubEventEngine.Core { @@ -17,14 +18,14 @@ internal interface IEffectCancelInvocation : IEffectInvocation { } internal interface IEvent { }; internal interface IState { - public abstract IEnumerable OnEntry { get; } - public abstract IEnumerable OnExit { get; } + public IEnumerable OnEntry { get; } + public IEnumerable OnExit { get; } /// /// The EE transition pure function. /// /// Input event /// Target state and invocation list, or null for no-transition - public abstract System.Tuple> Transition(IEvent e); + public System.Tuple> Transition(IEvent e); } } \ No newline at end of file diff --git a/src/Api/PubnubApi/EventEngine/Core/Utils.cs b/src/Api/PubnubApi/EventEngine/Core/Utils.cs new file mode 100644 index 000000000..ca8f3e026 --- /dev/null +++ b/src/Api/PubnubApi/EventEngine/Core/Utils.cs @@ -0,0 +1,14 @@ +using System; +using System.Threading.Tasks; +using System.Collections.Generic; + +namespace PubnubApi.PubnubEventEngine.Core +{ + internal static class Utils + { + internal static Tuple> With(this IState state, params IEffectInvocation[] invocations) + { + return new Tuple>(state, invocations); + } + } +} \ No newline at end of file From 10e59d73d0824e05503079218f5ddf1c752ecb99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dobrza=C5=84ski?= Date: Mon, 26 Jun 2023 18:52:26 +0200 Subject: [PATCH 02/10] Simplified the invocations during transitions, added onEntry and onExit transitions, added HandshakingState and UnsubscribedState --- src/Api/PubnubApi/EventEngine/Core/Utils.cs | 5 + .../Subscribe/Events/SubscriptionEvents.cs | 5 +- .../Invocations/SubscriptionInvocations.cs | 8 +- .../Subscribe/States/HandshakeFailedState.cs | 92 ++++----- .../States/HandshakeReconnectingState.cs | 119 +++++------ .../Subscribe/States/HandshakeStoppedState.cs | 122 ++++++----- .../Subscribe/States/HandshakingState.cs | 74 ++++++- .../Subscribe/States/ReceiveFailedState.cs | 100 ++++----- .../States/ReceiveReconnectingState.cs | 193 ++++++++---------- .../Subscribe/States/ReceiveStoppedState.cs | 99 ++++----- .../Subscribe/States/ReceivingState.cs | 144 ++++++------- .../Subscribe/States/SubscribedState.cs | 13 -- .../Subscribe/States/UnsubscribedState.cs | 50 ++--- 13 files changed, 489 insertions(+), 535 deletions(-) delete mode 100644 src/Api/PubnubApi/EventEngine/Subscribe/States/SubscribedState.cs diff --git a/src/Api/PubnubApi/EventEngine/Core/Utils.cs b/src/Api/PubnubApi/EventEngine/Core/Utils.cs index ca8f3e026..0a780331b 100644 --- a/src/Api/PubnubApi/EventEngine/Core/Utils.cs +++ b/src/Api/PubnubApi/EventEngine/Core/Utils.cs @@ -10,5 +10,10 @@ internal static Tuple> With(this IState s { return new Tuple>(state, invocations); } + + internal static IEffectInvocation[] AsArray(this IEffectInvocation invocation) + { + return new IEffectInvocation[] { invocation }; + } } } \ No newline at end of file diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/Events/SubscriptionEvents.cs b/src/Api/PubnubApi/EventEngine/Subscribe/Events/SubscriptionEvents.cs index ff3688cce..fc71f0745 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/Events/SubscriptionEvents.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/Events/SubscriptionEvents.cs @@ -4,7 +4,6 @@ namespace PubnubApi.PubnubEventEngine.Subscribe.Events { public class SubscriptionChangedEvent : Core.IEvent { public IEnumerable Channels; public IEnumerable ChannelGroups; - public SubscriptionCursor Cursor; } public class SubscriptionRestoredEvent : Core.IEvent { @@ -19,7 +18,7 @@ public class HandshakeSuccessEvent : Core.IEvent { public class HandshakeFailureEvent : Core.IEvent { // TODO status or reason? - public PNStatus status; + public PNStatus Status; } public class HandshakeReconnectSuccessEvent : HandshakeSuccessEvent { @@ -70,7 +69,7 @@ public class ReceiveReconnectGiveUpEvent : Core.IEvent { public IEnumerable ChannelGroups; public SubscriptionCursor Cursor; // TODO status or reason? - public PNStatus status; + public PNStatus Status; } public class DisconnectEvent : Core.IEvent { diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/Invocations/SubscriptionInvocations.cs b/src/Api/PubnubApi/EventEngine/Subscribe/Invocations/SubscriptionInvocations.cs index 3ec6c133c..417e6aa82 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/Invocations/SubscriptionInvocations.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/Invocations/SubscriptionInvocations.cs @@ -9,11 +9,15 @@ internal class EmitMessagesInvocation : Core.IEffectInvocation { internal class EmitStatusInvocation : Core.IEffectInvocation { public IEnumerable Channels; public IEnumerable ChannelGroups; + // TODO merge status variables into one + public PNStatusCategory StatusCategory; + public PNStatus Status; } internal class HandshakeInvocation : Core.IEffectInvocation { public IEnumerable Channels; public IEnumerable ChannelGroups; + // TODO if we need these, figure out how to pass them. public Dictionary InitialSubscribeQueryParams = new Dictionary(); public Dictionary ExternalQueryParams = new Dictionary(); } @@ -27,9 +31,8 @@ internal class ReceiveMessagesInvocation : Core.IEffectInvocation internal class CancelReceiveMessagesInvocation : ReceiveMessagesInvocation, Core.IEffectCancelInvocation { } - internal class HandshakeCancelInvocation : HandshakeInvocation, Core.IEffectCancelInvocation { } + internal class CancelHandshakeInvocation : HandshakeInvocation, Core.IEffectCancelInvocation { } - //internal class ReconnectInvocation : Core.IEffectInvocation { } internal class HandshakeReconnectInvocation: Core.IEffectInvocation { public IEnumerable Channels; @@ -46,5 +49,4 @@ internal class ReceiveReconnectInvocation: Core.IEffectInvocation } internal class CancelReceiveReconnectInvocation: ReceiveReconnectInvocation, Core.IEffectCancelInvocation { } - //internal class CancelReconnectInvocation : ReconnectInvocation, Core.IEffectCancelInvocation { } } \ No newline at end of file diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeFailedState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeFailedState.cs index 8816c77db..74b2c2c79 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeFailedState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeFailedState.cs @@ -3,61 +3,41 @@ using PubnubApi.PubnubEventEngine.Core; using PubnubApi.PubnubEventEngine.Subscribe.Invocations; -namespace PubnubApi.PubnubEventEngine.Subscribe.States { - internal class HandshakeFailedState : Core.IState { +namespace PubnubApi.PubnubEventEngine.Subscribe.States +{ + internal class HandshakeFailedState : Core.IState + { + public IEnumerable Channels; + public IEnumerable ChannelGroups; - public IEnumerable Channels; - public IEnumerable ChannelGroups; - public SubscriptionCursor Cursor; + public IEnumerable OnEntry { get; } + public IEnumerable OnExit { get; } - public IEnumerable OnEntry { get; } - public IEnumerable OnExit { get; } - public Tuple> Transition(IEvent e) { - switch (e) { - case Events.SubscriptionChangedEvent subscriptionChanged: - return new Tuple>( - new HandshakingState() { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - }, - new[] { - new HandshakeInvocation() { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - }, - } - ); - case Events.ReconnectEvent reconnect: - return new Tuple>( - new HandshakingState() { - Channels = reconnect.Channels, - ChannelGroups = reconnect.ChannelGroups, - }, - new[] { - new HandshakeInvocation() { - Channels = reconnect.Channels, - ChannelGroups = reconnect.ChannelGroups, - }, - } - ); - case Events.SubscriptionRestoredEvent subscriptionRestored: - return new Tuple>( - new ReceivingState() { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups, - Cursor = subscriptionRestored.Cursor - }, - new[] { - new ReceiveMessagesInvocation() { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups, - Cursor = subscriptionRestored.Cursor - }, - } - ); - - default: return null; - } - } - } -} + public Tuple> Transition(IEvent e) + { + switch (e) + { + case Events.SubscriptionChangedEvent subscriptionChanged: + return new HandshakingState() + { + Channels = subscriptionChanged.Channels, + ChannelGroups = subscriptionChanged.ChannelGroups, + }.With(); + case Events.ReconnectEvent reconnect: + return new HandshakingState() + { + Channels = reconnect.Channels, + ChannelGroups = reconnect.ChannelGroups, + }.With(); + case Events.SubscriptionRestoredEvent subscriptionRestored: + return new ReceivingState() + { + Channels = subscriptionRestored.Channels, + ChannelGroups = subscriptionRestored.ChannelGroups, + Cursor = subscriptionRestored.Cursor + }.With(); + default: return null; + } + } + } +} \ No newline at end of file diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeReconnectingState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeReconnectingState.cs index db5f59d88..d9dbd4269 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeReconnectingState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeReconnectingState.cs @@ -3,71 +3,60 @@ using PubnubApi.PubnubEventEngine.Core; using PubnubApi.PubnubEventEngine.Subscribe.Invocations; -namespace PubnubApi.PubnubEventEngine.Subscribe.States { - internal class HandshakeReconnectingState : Core.IState { +namespace PubnubApi.PubnubEventEngine.Subscribe.States +{ + internal class HandshakeReconnectingState : Core.IState + { + public IEnumerable Channels; + public IEnumerable ChannelGroups; - public IEnumerable Channels; - public IEnumerable ChannelGroups; + public IEnumerable OnEntry { get; } + public IEnumerable OnExit { get; } = new CancelHandshakeReconnectInvocation().AsArray(); - public IEnumerable OnEntry { get; } - public IEnumerable OnExit { get; } - public Tuple> Transition(IEvent e) { - switch (e) { - case Events.SubscriptionChangedEvent subscriptionChanged: - return new Tuple>( - new HandshakingState() { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - }, - new[] { - new HandshakeInvocation() { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - }, - } - ); - case Events.DisconnectEvent disconnect: - return new Tuple>( - new HandshakeStoppedState() { - Channels = disconnect.Channels, - ChannelGroups = disconnect.ChannelGroups - }, - null - ); - case Events.HandshakeReconnectGiveUpEvent handshakeReconnectGiveUp: - return new Tuple>( - new HandshakeFailedState() { - Channels = handshakeReconnectGiveUp.Channels, - ChannelGroups = handshakeReconnectGiveUp.ChannelGroups - }, - null - ); - case Events.HandshakeReconnectSuccessEvent handshakeReconnectSuccess: - return new Tuple>( - new ReceivingState() { - Channels = handshakeReconnectSuccess.Channels, - ChannelGroups = handshakeReconnectSuccess.ChannelGroups, - Cursor = handshakeReconnectSuccess.Cursor - }, - new[] { - new EmitStatusInvocation() { - Channels = handshakeReconnectSuccess.Channels, - ChannelGroups = handshakeReconnectSuccess.ChannelGroups, - }, - } - ); - case Events.SubscriptionRestoredEvent subscriptionRestored: - return new Tuple>( - new HandshakeFailedState() { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups, - Cursor = subscriptionRestored.Cursor - }, - null - ); - - default: return null; - } - } - } + public Tuple> Transition(IEvent e) + { + switch (e) + { + case Events.SubscriptionChangedEvent subscriptionChanged: + return new HandshakingState() + { + Channels = subscriptionChanged.Channels, + ChannelGroups = subscriptionChanged.ChannelGroups, + }.With(null); + case Events.DisconnectEvent disconnect: + return new HandshakeStoppedState() + { + Channels = disconnect.Channels, + ChannelGroups = disconnect.ChannelGroups + }.With(null); + case Events.HandshakeReconnectGiveUpEvent handshakeReconnectGiveUp: + return new HandshakeFailedState() + { + Channels = handshakeReconnectGiveUp.Channels, + ChannelGroups = handshakeReconnectGiveUp.ChannelGroups + }.With(); + case Events.HandshakeReconnectSuccessEvent handshakeReconnectSuccess: + return new ReceivingState() + { + Channels = handshakeReconnectSuccess.Channels, + ChannelGroups = handshakeReconnectSuccess.ChannelGroups, + Cursor = handshakeReconnectSuccess.Cursor + }.With( + new EmitStatusInvocation() + { + Channels = handshakeReconnectSuccess.Channels, + ChannelGroups = handshakeReconnectSuccess.ChannelGroups, + StatusCategory = PNStatusCategory.PNReconnectedCategory + } + ); + case Events.SubscriptionRestoredEvent subscriptionRestored: + return new HandshakeFailedState() + { + Channels = subscriptionRestored.Channels, + ChannelGroups = subscriptionRestored.ChannelGroups + }.With(); + default: return null; + } + } + } } \ No newline at end of file diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeStoppedState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeStoppedState.cs index 885acc5a8..b1b205148 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeStoppedState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeStoppedState.cs @@ -3,59 +3,75 @@ using PubnubApi.PubnubEventEngine.Core; using PubnubApi.PubnubEventEngine.Subscribe.Invocations; -namespace PubnubApi.PubnubEventEngine.Subscribe.States { - internal class HandshakeStoppedState : Core.IState { +namespace PubnubApi.PubnubEventEngine.Subscribe.States +{ + internal class HandshakeStoppedState : Core.IState + { + public IEnumerable Channels; + public IEnumerable ChannelGroups; - public IEnumerable Channels; - public IEnumerable ChannelGroups; + public IEnumerable OnEntry { get; } + public IEnumerable OnExit { get; } - public IEnumerable OnEntry { get; } - public IEnumerable OnExit { get; } - public Tuple> Transition(IEvent e) { - switch (e) { - case Events.SubscriptionChangedEvent subscriptionChanged: - return new Tuple>( - new HandshakingState() { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - }, - new[] { - new HandshakeInvocation() { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - }, - } - ); - case Events.ReconnectEvent reconnect: - return new Tuple>( - new HandshakingState() { - Channels = reconnect.Channels, - ChannelGroups = reconnect.ChannelGroups, - }, - new[] { - new HandshakeInvocation() { - Channels = reconnect.Channels, - ChannelGroups = reconnect.ChannelGroups, - }, - } - ); - case Events.SubscriptionRestoredEvent subscriptionRestored: - return new Tuple>( - new ReceivingState() { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups, - Cursor = subscriptionRestored.Cursor - }, - new[] { - new ReceiveMessagesInvocation() { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups, - }, - } - ); - - default: return null; - } - } - } + public Tuple> Transition(IEvent e) + { + switch (e) + { + case Events.SubscriptionChangedEvent subscriptionChanged: + return new HandshakingState() + { + Channels = subscriptionChanged.Channels, + ChannelGroups = subscriptionChanged.ChannelGroups, + }.With(null); + case Events.ReconnectEvent reconnect: + return new HandshakingState() + { + Channels = reconnect.Channels, + ChannelGroups = reconnect.ChannelGroups, + }.With(null); + case Events.SubscriptionRestoredEvent subscriptionRestored: + return new ReceivingState() + { + Channels = subscriptionRestored.Channels, + ChannelGroups = subscriptionRestored.ChannelGroups, + Cursor = subscriptionRestored.Cursor + }.With(null); + case Events.HandshakeFailureEvent handshakeFailure: + return new HandshakeReconnectingState() + { + Channels = this.Channels, + ChannelGroups = this.ChannelGroups, + }.With( + new EmitStatusInvocation() + { + Status = handshakeFailure.Status + } + ); + case Events.DisconnectEvent disconnect: + return new HandshakeStoppedState() + { + Channels = disconnect.Channels, + ChannelGroups = disconnect.ChannelGroups, + }.With( + new EmitStatusInvocation() + { + StatusCategory = PNStatusCategory.PNDisconnectedCategory + } + ); + case Events.HandshakeSuccessEvent handshakeSuccess: + return new ReceivingState() + { + Channels = this.Channels, + ChannelGroups = this.ChannelGroups, + Cursor = handshakeSuccess.cursor + }.With( + new EmitStatusInvocation() + { + StatusCategory = PNStatusCategory.PNConnectedCategory + } + ); + default: return null; + } + } + } } \ No newline at end of file diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakingState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakingState.cs index a17469e96..36f341b9e 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakingState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakingState.cs @@ -1,17 +1,71 @@ using System; using System.Collections.Generic; using PubnubApi.PubnubEventEngine.Core; +using PubnubApi.PubnubEventEngine.Subscribe.Invocations; -namespace PubnubApi.PubnubEventEngine.Subscribe.States { - internal class HandshakingState : Core.IState { +namespace PubnubApi.PubnubEventEngine.Subscribe.States +{ + internal class HandshakingState : Core.IState + { + public IEnumerable Channels; + public IEnumerable ChannelGroups; - public IEnumerable Channels; - public IEnumerable ChannelGroups; + public IEnumerable OnEntry => new HandshakeInvocation() + { Channels = this.Channels, ChannelGroups = this.ChannelGroups }.AsArray(); - public IEnumerable OnEntry { get; } - public IEnumerable OnExit { get; } - public Tuple> Transition(IEvent e) { - throw new NotImplementedException(); - } - } + public IEnumerable OnExit { get; } = new CancelHandshakeInvocation().AsArray(); + + public Tuple> Transition(IEvent e) + { + switch (e) + { + case Events.SubscriptionChangedEvent subscriptionChanged: + return new States.HandshakingState() + { + Channels = subscriptionChanged.Channels, + ChannelGroups = subscriptionChanged.ChannelGroups + }.With(); + case Events.SubscriptionRestoredEvent subscriptionRestored: + return new States.ReceivingState() + { + Channels = subscriptionRestored.Channels, + ChannelGroups = subscriptionRestored.ChannelGroups, + Cursor = subscriptionRestored.Cursor + }.With(); + case Events.HandshakeFailureEvent handshakeFailure: + return new States.HandshakeFailedState() + { + Channels = this.Channels, + ChannelGroups = this.ChannelGroups + }.With( + new EmitStatusInvocation() + { + Status = handshakeFailure.Status + } + ); + case Events.DisconnectEvent disconnect: + return new States.HandshakeStoppedState() + { + Channels = disconnect.Channels, + ChannelGroups = disconnect.ChannelGroups, + }.With( + new EmitStatusInvocation() + { + StatusCategory = PNStatusCategory.PNDisconnectedCategory + } + ); + case Events.HandshakeSuccessEvent success: + return new ReceivingState() + { + Channels = this.Channels, + ChannelGroups = this.ChannelGroups, + Cursor = success.cursor + }.With(new EmitStatusInvocation() + { + StatusCategory = PNStatusCategory.PNConnectedCategory + }); + default: return null; + } + } + } } \ No newline at end of file diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveFailedState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveFailedState.cs index 8b3b68489..5e7edbe5c 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveFailedState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveFailedState.cs @@ -3,66 +3,44 @@ using PubnubApi.PubnubEventEngine.Core; using PubnubApi.PubnubEventEngine.Subscribe.Invocations; -namespace PubnubApi.PubnubEventEngine.Subscribe.States { - internal class ReceiveFailedState : Core.IState { +namespace PubnubApi.PubnubEventEngine.Subscribe.States +{ + internal class ReceiveFailedState : Core.IState + { + public IEnumerable Channels; + public IEnumerable ChannelGroups; + public SubscriptionCursor Cursor; - public IEnumerable Channels; - public IEnumerable ChannelGroups; - public SubscriptionCursor Cursor; - - public IEnumerable OnEntry { get; } - public IEnumerable OnExit { get; } - public Tuple> Transition(IEvent e) { - switch (e) { - case Events.SubscriptionChangedEvent subscriptionChanged: - return new Tuple>( - new ReceivingState() { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - Cursor = subscriptionChanged.Cursor - }, - new[] { - new ReceiveMessagesInvocation() { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - Cursor = subscriptionChanged.Cursor - }, - } - ); - case Events.ReconnectEvent reconnect: - return new Tuple>( - new ReceivingState() { - Channels = reconnect.Channels, - ChannelGroups = reconnect.ChannelGroups, - Cursor = reconnect.Cursor - }, - new[] { - new ReceiveMessagesInvocation() { - Channels = reconnect.Channels, - ChannelGroups = reconnect.ChannelGroups, - Cursor = reconnect.Cursor - }, - } - ); - case Events.SubscriptionRestoredEvent subscriptionRestored: - return new Tuple>( - new ReceivingState() { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups, - Cursor = subscriptionRestored.Cursor - }, - new[] { - new ReceiveMessagesInvocation() { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups, - Cursor = subscriptionRestored.Cursor - }, - } - ); - - default: return null; - } - } - } -} + public IEnumerable OnEntry { get; } + public IEnumerable OnExit { get; } + public Tuple> Transition(IEvent e) + { + switch (e) + { + case Events.SubscriptionChangedEvent subscriptionChanged: + return new ReceivingState() + { + Channels = subscriptionChanged.Channels, + ChannelGroups = subscriptionChanged.ChannelGroups, + Cursor = this.Cursor + }.With(null); + case Events.ReconnectEvent reconnect: + return new ReceivingState() + { + Channels = reconnect.Channels, + ChannelGroups = reconnect.ChannelGroups, + Cursor = reconnect.Cursor + }.With(null); + case Events.SubscriptionRestoredEvent subscriptionRestored: + return new ReceivingState() + { + Channels = subscriptionRestored.Channels, + ChannelGroups = subscriptionRestored.ChannelGroups, + Cursor = subscriptionRestored.Cursor + }.With(null); + default: return null; + } + } + } +} \ No newline at end of file diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveReconnectingState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveReconnectingState.cs index d18036c5a..d0bb19898 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveReconnectingState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveReconnectingState.cs @@ -3,111 +3,94 @@ using PubnubApi.PubnubEventEngine.Core; using PubnubApi.PubnubEventEngine.Subscribe.Invocations; -namespace PubnubApi.PubnubEventEngine.Subscribe.States { - internal class ReceiveReconnectingState : Core.IState { +namespace PubnubApi.PubnubEventEngine.Subscribe.States +{ + internal class ReceiveReconnectingState : Core.IState + { + public IEnumerable Channels; + public IEnumerable ChannelGroups; + public SubscriptionCursor Cursor; - public IEnumerable Channels; - public IEnumerable ChannelGroups; - public SubscriptionCursor Cursor; - - public IEnumerable OnEntry { get; } - public IEnumerable OnExit { get; } - public Tuple> Transition(IEvent e) { - switch (e) { - case Events.SubscriptionChangedEvent subscriptionChanged: - return new Tuple>( - new ReceivingState() { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - Cursor = subscriptionChanged.Cursor - }, - new[] { - new ReceiveMessagesInvocation() { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - Cursor = subscriptionChanged.Cursor - }, - } - ); - case Events.DisconnectEvent disconnect: - return new Tuple>( - new ReceiveStoppedState() { - Channels = disconnect.Channels, - ChannelGroups = disconnect.ChannelGroups, - Cursor = disconnect.Cursor - }, - new[] { - new EmitStatusInvocation() { - Channels = disconnect.Channels, - ChannelGroups = disconnect.ChannelGroups, - }, - } - ); - case Events.SubscriptionRestoredEvent subscriptionRestored: - return new Tuple>( - new ReceivingState() { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups, - Cursor = subscriptionRestored.Cursor - }, - new[] { - new ReceiveMessagesInvocation() { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups, - Cursor = subscriptionRestored.Cursor - }, - } - ); - case Events.ReceiveReconnectSuccessEvent receiveReconnectSuccess: - return new Tuple>( - new ReceivingState() { - Channels = receiveReconnectSuccess.Channels, - ChannelGroups = receiveReconnectSuccess.ChannelGroups, - Cursor = receiveReconnectSuccess.Cursor - }, - new IEffectInvocation[] { - new EmitStatusInvocation() { - Channels = receiveReconnectSuccess.Channels, - ChannelGroups = receiveReconnectSuccess.ChannelGroups, - }, - new ReceiveMessagesInvocation() { - Channels = receiveReconnectSuccess.Channels, - ChannelGroups = receiveReconnectSuccess.ChannelGroups, - Cursor = receiveReconnectSuccess.Cursor - } - } - ); - case Events.ReceiveReconnectFailureEvent receiveReconnectFailure: - return new Tuple>( - new ReceiveReconnectingState() { - Channels = receiveReconnectFailure.Channels, - ChannelGroups = receiveReconnectFailure.ChannelGroups, - Cursor = receiveReconnectFailure.Cursor - }, - new[] - { - new ReceiveReconnectInvocation - { - Channels = receiveReconnectFailure.Channels, - ChannelGroups = receiveReconnectFailure.ChannelGroups, - Cursor = receiveReconnectFailure.Cursor - } - } - ); - case Events.ReceiveReconnectGiveUpEvent receiveReconnectGiveUp: - return new Tuple>( - new ReceiveFailedState() { - Channels = receiveReconnectGiveUp.Channels, - ChannelGroups = receiveReconnectGiveUp.ChannelGroups, - Cursor = receiveReconnectGiveUp.Cursor - }, - null - ); - - default: return null; - } - } - } -} + public IEnumerable OnEntry => new ReceiveReconnectInvocation() + { Channels = this.Channels, ChannelGroups = this.ChannelGroups, Cursor = this.Cursor }.AsArray(); + public IEnumerable OnExit { get; } = + new CancelReceiveReconnectInvocation().AsArray(); + public Tuple> Transition(IEvent e) + { + switch (e) + { + case Events.SubscriptionChangedEvent subscriptionChanged: + return new ReceivingState() + { + Channels = subscriptionChanged.Channels, + ChannelGroups = subscriptionChanged.ChannelGroups, + Cursor = this.Cursor + }.With( + new ReceiveMessagesInvocation() + { + Channels = subscriptionChanged.Channels, + ChannelGroups = subscriptionChanged.ChannelGroups, + Cursor = this.Cursor + } + ); + case Events.DisconnectEvent disconnect: + return new ReceiveStoppedState() + { + Channels = disconnect.Channels, + ChannelGroups = disconnect.ChannelGroups, + Cursor = disconnect.Cursor + }.With( + new EmitStatusInvocation() + { + Channels = disconnect.Channels, + ChannelGroups = disconnect.ChannelGroups, + StatusCategory = PNStatusCategory.PNDisconnectedCategory + } + ); + case Events.SubscriptionRestoredEvent subscriptionRestored: + return new ReceivingState() + { + Channels = subscriptionRestored.Channels, + ChannelGroups = subscriptionRestored.ChannelGroups, + Cursor = subscriptionRestored.Cursor + }.With(null); + case Events.ReceiveReconnectSuccessEvent receiveReconnectSuccess: + return new ReceivingState() + { + Channels = receiveReconnectSuccess.Channels, + ChannelGroups = receiveReconnectSuccess.ChannelGroups, + Cursor = receiveReconnectSuccess.Cursor + }.With( + new EmitStatusInvocation() + { + Channels = receiveReconnectSuccess.Channels, + ChannelGroups = receiveReconnectSuccess.ChannelGroups, + StatusCategory = PNStatusCategory.PNReconnectedCategory + } + ); + case Events.ReceiveReconnectFailureEvent receiveReconnectFailure: + return new ReceiveReconnectingState() + { + Channels = receiveReconnectFailure.Channels, + ChannelGroups = receiveReconnectFailure.ChannelGroups, + Cursor = receiveReconnectFailure.Cursor + }.With(null); + case Events.ReceiveReconnectGiveUpEvent receiveReconnectGiveUp: + return new ReceiveFailedState() + { + Channels = receiveReconnectGiveUp.Channels, + ChannelGroups = receiveReconnectGiveUp.ChannelGroups, + Cursor = receiveReconnectGiveUp.Cursor + }.With( + new EmitStatusInvocation() + { + Status = receiveReconnectGiveUp.Status + } + ); + default: return null; + } + } + } +} \ No newline at end of file diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveStoppedState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveStoppedState.cs index a66411b36..16119744c 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveStoppedState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveStoppedState.cs @@ -3,66 +3,45 @@ using PubnubApi.PubnubEventEngine.Core; using PubnubApi.PubnubEventEngine.Subscribe.Invocations; -namespace PubnubApi.PubnubEventEngine.Subscribe.States { - internal class ReceiveStoppedState : Core.IState { +namespace PubnubApi.PubnubEventEngine.Subscribe.States +{ + internal class ReceiveStoppedState : Core.IState + { + public IEnumerable Channels; + public IEnumerable ChannelGroups; + public SubscriptionCursor Cursor; - public IEnumerable Channels; - public IEnumerable ChannelGroups; - public SubscriptionCursor Cursor; + public IEnumerable OnEntry { get; } + public IEnumerable OnExit { get; } - public IEnumerable OnEntry { get; } - public IEnumerable OnExit { get; } - public Tuple> Transition(IEvent e) { - switch (e) { - case Events.SubscriptionChangedEvent subscriptionChanged: - return new Tuple>( - new ReceivingState() { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - Cursor = subscriptionChanged.Cursor, - }, - new[] { - new ReceiveMessagesInvocation() { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - Cursor = subscriptionChanged.Cursor, - }, - } - ); - case Events.ReconnectEvent reconnect: - return new Tuple>( - new ReceivingState() { - Channels = reconnect.Channels, - ChannelGroups = reconnect.ChannelGroups, - Cursor = reconnect.Cursor - }, - new[] { - new ReceiveMessagesInvocation() { - Channels = reconnect.Channels, - ChannelGroups = reconnect.ChannelGroups, - Cursor = reconnect.Cursor - }, - } - ); - case Events.SubscriptionRestoredEvent subscriptionRestored: - return new Tuple>( - new ReceivingState() { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups, - Cursor = subscriptionRestored.Cursor - }, - new[] { - new ReceiveMessagesInvocation() { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups, - Cursor = subscriptionRestored.Cursor - }, - } - ); - - default: return null; - } - } - } -} + public Tuple> Transition(IEvent e) + { + switch (e) + { + case Events.SubscriptionChangedEvent subscriptionChanged: + return new ReceivingState() + { + Channels = subscriptionChanged.Channels, + ChannelGroups = subscriptionChanged.ChannelGroups, + Cursor = this.Cursor, + }.With(); + case Events.ReconnectEvent reconnect: + return new ReceivingState() + { + Channels = reconnect.Channels, + ChannelGroups = reconnect.ChannelGroups, + Cursor = reconnect.Cursor + }.With(); + case Events.SubscriptionRestoredEvent subscriptionRestored: + return new ReceivingState() + { + Channels = subscriptionRestored.Channels, + ChannelGroups = subscriptionRestored.ChannelGroups, + Cursor = subscriptionRestored.Cursor + }.With(); + default: return null; + } + } + } +} \ No newline at end of file diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceivingState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceivingState.cs index 06bee3d54..4609f468e 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceivingState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceivingState.cs @@ -3,89 +3,67 @@ using PubnubApi.PubnubEventEngine.Core; using PubnubApi.PubnubEventEngine.Subscribe.Invocations; -namespace PubnubApi.PubnubEventEngine.Subscribe.States { - internal class ReceivingState : Core.IState { +namespace PubnubApi.PubnubEventEngine.Subscribe.States +{ + internal class ReceivingState : Core.IState + { + public IEnumerable Channels; + public IEnumerable ChannelGroups; + public SubscriptionCursor Cursor; - public IEnumerable Channels; - public IEnumerable ChannelGroups; - public SubscriptionCursor Cursor; + public IEnumerable OnEntry => new ReceiveMessagesInvocation() + { Channels = this.Channels, ChannelGroups = this.ChannelGroups, Cursor = this.Cursor }.AsArray(); - public IEnumerable OnEntry { get; } - public IEnumerable OnExit { get; } - public Tuple> Transition(IEvent e) { - switch (e) { - case Events.ReceiveSuccessEvent receiveSuccess: - return new Tuple>( - new ReceivingState() { - Channels = receiveSuccess.Channels, - ChannelGroups = receiveSuccess.ChannelGroups, - Cursor = receiveSuccess.Cursor - }, - new[] { - new ReceiveMessagesInvocation() { - Channels = receiveSuccess.Channels, - ChannelGroups = receiveSuccess.ChannelGroups, - Cursor = receiveSuccess.Cursor - }, - } - ); - case Events.SubscriptionChangedEvent subscriptionChanged: - return new Tuple>( - new ReceivingState() { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - Cursor = subscriptionChanged.Cursor - }, - new[] { - new ReceiveMessagesInvocation() { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - Cursor = subscriptionChanged.Cursor - }, - } - ); - case Events.SubscriptionRestoredEvent subscriptionRestored: - return new Tuple>( - new ReceivingState() { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups, - Cursor = subscriptionRestored.Cursor - }, - new[] { - new ReceiveMessagesInvocation() { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups, - Cursor = subscriptionRestored.Cursor - }, - } - ); - case Events.DisconnectEvent disconnect: - return new Tuple>( - new ReceiveStoppedState() { - Channels = disconnect.Channels, - ChannelGroups = disconnect.ChannelGroups, - Cursor = disconnect.Cursor - }, - new[] { - new EmitStatusInvocation() { - Channels = disconnect.Channels, - ChannelGroups = disconnect.ChannelGroups, - }, - } - ); - case Events.ReceiveFailureEvent receiveFailure: - return new Tuple>( - new ReceiveReconnectingState() { - Channels = receiveFailure.Channels, - ChannelGroups = receiveFailure.ChannelGroups, - Cursor = receiveFailure.Cursor - }, - null - ); - - default: return null; - } - } - } -} + public IEnumerable OnExit { get; } = new CancelReceiveMessagesInvocation().AsArray(); + public Tuple> Transition(IEvent e) + { + switch (e) + { + case Events.ReceiveSuccessEvent receiveSuccess: + return new ReceivingState() + { + Channels = receiveSuccess.Channels, + ChannelGroups = receiveSuccess.ChannelGroups, + Cursor = receiveSuccess.Cursor + }.With(null); + case Events.SubscriptionChangedEvent subscriptionChanged: + return new ReceivingState() + { + Channels = subscriptionChanged.Channels, + ChannelGroups = subscriptionChanged.ChannelGroups, + Cursor = this.Cursor + }.With(); + case Events.SubscriptionRestoredEvent subscriptionRestored: + return new ReceivingState() + { + Channels = subscriptionRestored.Channels, + ChannelGroups = subscriptionRestored.ChannelGroups, + Cursor = subscriptionRestored.Cursor + }.With(); + case Events.DisconnectEvent disconnect: + return new ReceiveStoppedState() + { + Channels = disconnect.Channels, + ChannelGroups = disconnect.ChannelGroups, + Cursor = disconnect.Cursor + }.With( + new EmitStatusInvocation() + { + Channels = disconnect.Channels, + ChannelGroups = disconnect.ChannelGroups, + StatusCategory = PNStatusCategory.PNDisconnectedCategory + } + ); + case Events.ReceiveFailureEvent receiveFailure: + return new ReceiveReconnectingState() + { + Channels = receiveFailure.Channels, + ChannelGroups = receiveFailure.ChannelGroups, + Cursor = receiveFailure.Cursor + }.With(); + default: return null; + } + } + } +} \ No newline at end of file diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/SubscribedState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/SubscribedState.cs deleted file mode 100644 index 39de39a23..000000000 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/SubscribedState.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using PubnubApi.PubnubEventEngine.Core; - -namespace PubnubApi.PubnubEventEngine.Subscribe.States { - internal class SubscribedState : Core.IState { - public IEnumerable OnEntry { get; } - public IEnumerable OnExit { get; } - public Tuple> Transition(IEvent e) { - throw new NotImplementedException(); - } - } -} \ No newline at end of file diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/UnsubscribedState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/UnsubscribedState.cs index 7c9a7c386..b66ae792c 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/UnsubscribedState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/UnsubscribedState.cs @@ -3,28 +3,32 @@ using PubnubApi.PubnubEventEngine.Core; using PubnubApi.PubnubEventEngine.Subscribe.Invocations; -namespace PubnubApi.PubnubEventEngine.Subscribe.States { - internal class UnsubscribedState : Core.IState { - public IEnumerable OnEntry { get; } - public IEnumerable OnExit { get; } - public Tuple> Transition(Core.IEvent e) { - switch (e) { - case Events.SubscriptionChangedEvent subscriptionChanged: - return new Tuple>( - new HandshakingState() { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - }, - new[] { - new HandshakeInvocation() { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - }, - } - ); +namespace PubnubApi.PubnubEventEngine.Subscribe.States +{ + internal class UnsubscribedState : Core.IState + { + public IEnumerable OnEntry { get; } + public IEnumerable OnExit { get; } - default: return null; - } - } - } + public Tuple> Transition(Core.IEvent e) + { + switch (e) + { + case Events.SubscriptionChangedEvent subscriptionChanged: + return new HandshakingState() + { + Channels = subscriptionChanged.Channels, + ChannelGroups = subscriptionChanged.ChannelGroups, + }.With(); + case Events.SubscriptionRestoredEvent subscriptionRestored: + return new States.ReceivingState() + { + Channels = subscriptionRestored.Channels, + ChannelGroups = subscriptionRestored.ChannelGroups, + Cursor = subscriptionRestored.Cursor + }.With(); + default: return null; + } + } + } } \ No newline at end of file From caef6234fbee191f396b7fd40fe3d02d5a8f8414 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dobrza=C5=84ski?= Date: Mon, 26 Jun 2023 18:53:08 +0200 Subject: [PATCH 03/10] fix --- src/Api/PubnubApi/EventEngine/Subscribe/SubscribeEventEngine.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/SubscribeEventEngine.cs b/src/Api/PubnubApi/EventEngine/Subscribe/SubscribeEventEngine.cs index d0756455a..f71bd366f 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/SubscribeEventEngine.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/SubscribeEventEngine.cs @@ -13,7 +13,7 @@ public SubscribeEventEngine(SubscribeManager2 subscribeManager) { // initialize the handler, pass dependencies var handshakeHandler = new Effects.HandshakeEffectHandler(subscribeManager, eventQueue); dispatcher.Register(handshakeHandler); - dispatcher.Register(handshakeHandler); + dispatcher.Register(handshakeHandler); currentState = new UnsubscribedState(); } From e954d74f4d19c788a47faf20647a1a439e96d35c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dobrza=C5=84ski?= Date: Mon, 26 Jun 2023 19:14:39 +0200 Subject: [PATCH 04/10] Switch expression --- .../Subscribe/States/HandshakeFailedState.cs | 42 +++--- .../States/HandshakeReconnectingState.cs | 77 +++++----- .../Subscribe/States/HandshakeStoppedState.cs | 91 +++++------- .../Subscribe/States/HandshakingState.cs | 78 ++++------- .../Subscribe/States/ReceiveFailedState.cs | 48 +++---- .../States/ReceiveReconnectingState.cs | 131 ++++++++---------- .../Subscribe/States/ReceiveStoppedState.cs | 48 +++---- .../Subscribe/States/ReceivingState.cs | 88 ++++++------ .../Subscribe/States/UnsubscribedState.cs | 31 ++--- 9 files changed, 287 insertions(+), 347 deletions(-) diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeFailedState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeFailedState.cs index 74b2c2c79..b21ca1c8b 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeFailedState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeFailedState.cs @@ -15,29 +15,27 @@ internal class HandshakeFailedState : Core.IState public Tuple> Transition(IEvent e) { - switch (e) + return e switch { - case Events.SubscriptionChangedEvent subscriptionChanged: - return new HandshakingState() - { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - }.With(); - case Events.ReconnectEvent reconnect: - return new HandshakingState() - { - Channels = reconnect.Channels, - ChannelGroups = reconnect.ChannelGroups, - }.With(); - case Events.SubscriptionRestoredEvent subscriptionRestored: - return new ReceivingState() - { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups, - Cursor = subscriptionRestored.Cursor - }.With(); - default: return null; - } + Events.SubscriptionChangedEvent subscriptionChanged => new HandshakingState() + { + Channels = subscriptionChanged.Channels, ChannelGroups = subscriptionChanged.ChannelGroups, + }.With(), + + Events.ReconnectEvent reconnect => new HandshakingState() + { + Channels = reconnect.Channels, ChannelGroups = reconnect.ChannelGroups, + }.With(), + + Events.SubscriptionRestoredEvent subscriptionRestored => new ReceivingState() + { + Channels = subscriptionRestored.Channels, + ChannelGroups = subscriptionRestored.ChannelGroups, + Cursor = subscriptionRestored.Cursor + }.With(), + + _ => null + }; } } } \ No newline at end of file diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeReconnectingState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeReconnectingState.cs index d9dbd4269..2d9f2e6d8 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeReconnectingState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeReconnectingState.cs @@ -15,48 +15,43 @@ internal class HandshakeReconnectingState : Core.IState public Tuple> Transition(IEvent e) { - switch (e) + return e switch { - case Events.SubscriptionChangedEvent subscriptionChanged: - return new HandshakingState() - { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - }.With(null); - case Events.DisconnectEvent disconnect: - return new HandshakeStoppedState() - { - Channels = disconnect.Channels, - ChannelGroups = disconnect.ChannelGroups - }.With(null); - case Events.HandshakeReconnectGiveUpEvent handshakeReconnectGiveUp: - return new HandshakeFailedState() - { - Channels = handshakeReconnectGiveUp.Channels, - ChannelGroups = handshakeReconnectGiveUp.ChannelGroups - }.With(); - case Events.HandshakeReconnectSuccessEvent handshakeReconnectSuccess: - return new ReceivingState() - { - Channels = handshakeReconnectSuccess.Channels, - ChannelGroups = handshakeReconnectSuccess.ChannelGroups, - Cursor = handshakeReconnectSuccess.Cursor - }.With( - new EmitStatusInvocation() - { - Channels = handshakeReconnectSuccess.Channels, - ChannelGroups = handshakeReconnectSuccess.ChannelGroups, - StatusCategory = PNStatusCategory.PNReconnectedCategory - } - ); - case Events.SubscriptionRestoredEvent subscriptionRestored: - return new HandshakeFailedState() - { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups - }.With(); - default: return null; - } + Events.SubscriptionChangedEvent subscriptionChanged => new HandshakingState() + { + Channels = subscriptionChanged.Channels, ChannelGroups = subscriptionChanged.ChannelGroups, + }.With(null), + + Events.DisconnectEvent disconnect => new HandshakeStoppedState() + { + Channels = disconnect.Channels, ChannelGroups = disconnect.ChannelGroups + }.With(null), + + Events.HandshakeReconnectGiveUpEvent handshakeReconnectGiveUp => new HandshakeFailedState() + { + Channels = handshakeReconnectGiveUp.Channels, + ChannelGroups = handshakeReconnectGiveUp.ChannelGroups + }.With(), + + Events.HandshakeReconnectSuccessEvent handshakeReconnectSuccess => new ReceivingState() + { + Channels = handshakeReconnectSuccess.Channels, + ChannelGroups = handshakeReconnectSuccess.ChannelGroups, + Cursor = handshakeReconnectSuccess.Cursor + }.With(new EmitStatusInvocation() + { + Channels = handshakeReconnectSuccess.Channels, + ChannelGroups = handshakeReconnectSuccess.ChannelGroups, + StatusCategory = PNStatusCategory.PNReconnectedCategory + }), + + Events.SubscriptionRestoredEvent subscriptionRestored => new HandshakeFailedState() + { + Channels = subscriptionRestored.Channels, ChannelGroups = subscriptionRestored.ChannelGroups + }.With(), + + _ => null + }; } } } \ No newline at end of file diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeStoppedState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeStoppedState.cs index b1b205148..970c7924d 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeStoppedState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeStoppedState.cs @@ -15,63 +15,42 @@ internal class HandshakeStoppedState : Core.IState public Tuple> Transition(IEvent e) { - switch (e) + return e switch { - case Events.SubscriptionChangedEvent subscriptionChanged: - return new HandshakingState() - { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - }.With(null); - case Events.ReconnectEvent reconnect: - return new HandshakingState() - { - Channels = reconnect.Channels, - ChannelGroups = reconnect.ChannelGroups, - }.With(null); - case Events.SubscriptionRestoredEvent subscriptionRestored: - return new ReceivingState() - { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups, - Cursor = subscriptionRestored.Cursor - }.With(null); - case Events.HandshakeFailureEvent handshakeFailure: - return new HandshakeReconnectingState() - { - Channels = this.Channels, - ChannelGroups = this.ChannelGroups, - }.With( - new EmitStatusInvocation() - { - Status = handshakeFailure.Status - } - ); - case Events.DisconnectEvent disconnect: - return new HandshakeStoppedState() - { - Channels = disconnect.Channels, - ChannelGroups = disconnect.ChannelGroups, - }.With( - new EmitStatusInvocation() - { - StatusCategory = PNStatusCategory.PNDisconnectedCategory - } - ); - case Events.HandshakeSuccessEvent handshakeSuccess: - return new ReceivingState() - { - Channels = this.Channels, - ChannelGroups = this.ChannelGroups, - Cursor = handshakeSuccess.cursor - }.With( - new EmitStatusInvocation() - { - StatusCategory = PNStatusCategory.PNConnectedCategory - } - ); - default: return null; - } + Events.SubscriptionChangedEvent subscriptionChanged => new HandshakingState() + { + Channels = subscriptionChanged.Channels, ChannelGroups = subscriptionChanged.ChannelGroups, + }.With(null), + + Events.ReconnectEvent reconnect => new HandshakingState() + { + Channels = reconnect.Channels, ChannelGroups = reconnect.ChannelGroups, + }.With(null), + + Events.SubscriptionRestoredEvent subscriptionRestored => new ReceivingState() + { + Channels = subscriptionRestored.Channels, + ChannelGroups = subscriptionRestored.ChannelGroups, + Cursor = subscriptionRestored.Cursor + }.With(null), + + Events.HandshakeFailureEvent handshakeFailure => new HandshakeReconnectingState() + { + Channels = this.Channels, ChannelGroups = this.ChannelGroups, + }.With(new EmitStatusInvocation() { Status = handshakeFailure.Status }), + + Events.DisconnectEvent disconnect => new HandshakeStoppedState() + { + Channels = disconnect.Channels, ChannelGroups = disconnect.ChannelGroups, + }.With(new EmitStatusInvocation() { StatusCategory = PNStatusCategory.PNDisconnectedCategory }), + + Events.HandshakeSuccessEvent handshakeSuccess => new ReceivingState() + { + Channels = this.Channels, ChannelGroups = this.ChannelGroups, Cursor = handshakeSuccess.cursor + }.With(new EmitStatusInvocation() { StatusCategory = PNStatusCategory.PNConnectedCategory }), + + _ => null + }; } } } \ No newline at end of file diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakingState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakingState.cs index 36f341b9e..6a8c56935 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakingState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakingState.cs @@ -17,55 +17,37 @@ internal class HandshakingState : Core.IState public Tuple> Transition(IEvent e) { - switch (e) + return e switch { - case Events.SubscriptionChangedEvent subscriptionChanged: - return new States.HandshakingState() - { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups - }.With(); - case Events.SubscriptionRestoredEvent subscriptionRestored: - return new States.ReceivingState() - { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups, - Cursor = subscriptionRestored.Cursor - }.With(); - case Events.HandshakeFailureEvent handshakeFailure: - return new States.HandshakeFailedState() - { - Channels = this.Channels, - ChannelGroups = this.ChannelGroups - }.With( - new EmitStatusInvocation() - { - Status = handshakeFailure.Status - } - ); - case Events.DisconnectEvent disconnect: - return new States.HandshakeStoppedState() - { - Channels = disconnect.Channels, - ChannelGroups = disconnect.ChannelGroups, - }.With( - new EmitStatusInvocation() - { - StatusCategory = PNStatusCategory.PNDisconnectedCategory - } - ); - case Events.HandshakeSuccessEvent success: - return new ReceivingState() - { - Channels = this.Channels, - ChannelGroups = this.ChannelGroups, - Cursor = success.cursor - }.With(new EmitStatusInvocation() - { - StatusCategory = PNStatusCategory.PNConnectedCategory - }); - default: return null; - } + Events.SubscriptionChangedEvent subscriptionChanged => new States.HandshakingState() + { + Channels = subscriptionChanged.Channels, ChannelGroups = subscriptionChanged.ChannelGroups + }.With(), + + Events.SubscriptionRestoredEvent subscriptionRestored => new States.ReceivingState() + { + Channels = subscriptionRestored.Channels, + ChannelGroups = subscriptionRestored.ChannelGroups, + Cursor = subscriptionRestored.Cursor + }.With(), + + Events.HandshakeFailureEvent handshakeFailure => new States.HandshakeFailedState() + { + Channels = this.Channels, ChannelGroups = this.ChannelGroups + }.With(new EmitStatusInvocation() { Status = handshakeFailure.Status }), + + Events.DisconnectEvent disconnect => new States.HandshakeStoppedState() + { + Channels = disconnect.Channels, ChannelGroups = disconnect.ChannelGroups, + }.With(new EmitStatusInvocation() { StatusCategory = PNStatusCategory.PNDisconnectedCategory }), + + Events.HandshakeSuccessEvent success => new ReceivingState() + { + Channels = this.Channels, ChannelGroups = this.ChannelGroups, Cursor = success.cursor + }.With(new EmitStatusInvocation() { StatusCategory = PNStatusCategory.PNConnectedCategory }), + + _ => null + }; } } } \ No newline at end of file diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveFailedState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveFailedState.cs index 5e7edbe5c..135df318e 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveFailedState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveFailedState.cs @@ -16,31 +16,31 @@ internal class ReceiveFailedState : Core.IState public Tuple> Transition(IEvent e) { - switch (e) + return e switch { - case Events.SubscriptionChangedEvent subscriptionChanged: - return new ReceivingState() - { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - Cursor = this.Cursor - }.With(null); - case Events.ReconnectEvent reconnect: - return new ReceivingState() - { - Channels = reconnect.Channels, - ChannelGroups = reconnect.ChannelGroups, - Cursor = reconnect.Cursor - }.With(null); - case Events.SubscriptionRestoredEvent subscriptionRestored: - return new ReceivingState() - { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups, - Cursor = subscriptionRestored.Cursor - }.With(null); - default: return null; - } + Events.SubscriptionChangedEvent subscriptionChanged => new ReceivingState() + { + Channels = subscriptionChanged.Channels, + ChannelGroups = subscriptionChanged.ChannelGroups, + Cursor = this.Cursor + }.With(null), + + Events.ReconnectEvent reconnect => new ReceivingState() + { + Channels = reconnect.Channels, + ChannelGroups = reconnect.ChannelGroups, + Cursor = reconnect.Cursor + }.With(null), + + Events.SubscriptionRestoredEvent subscriptionRestored => new ReceivingState() + { + Channels = subscriptionRestored.Channels, + ChannelGroups = subscriptionRestored.ChannelGroups, + Cursor = subscriptionRestored.Cursor + }.With(null), + + _ => null + }; } } } \ No newline at end of file diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveReconnectingState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveReconnectingState.cs index d0bb19898..f1f2fbda3 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveReconnectingState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveReconnectingState.cs @@ -19,78 +19,67 @@ internal class ReceiveReconnectingState : Core.IState public Tuple> Transition(IEvent e) { - switch (e) + return e switch { - case Events.SubscriptionChangedEvent subscriptionChanged: - return new ReceivingState() - { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - Cursor = this.Cursor - }.With( - new ReceiveMessagesInvocation() - { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - Cursor = this.Cursor - } - ); - case Events.DisconnectEvent disconnect: - return new ReceiveStoppedState() - { - Channels = disconnect.Channels, - ChannelGroups = disconnect.ChannelGroups, - Cursor = disconnect.Cursor - }.With( - new EmitStatusInvocation() - { - Channels = disconnect.Channels, - ChannelGroups = disconnect.ChannelGroups, - StatusCategory = PNStatusCategory.PNDisconnectedCategory - } - ); - case Events.SubscriptionRestoredEvent subscriptionRestored: - return new ReceivingState() - { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups, - Cursor = subscriptionRestored.Cursor - }.With(null); - case Events.ReceiveReconnectSuccessEvent receiveReconnectSuccess: - return new ReceivingState() - { - Channels = receiveReconnectSuccess.Channels, - ChannelGroups = receiveReconnectSuccess.ChannelGroups, - Cursor = receiveReconnectSuccess.Cursor - }.With( - new EmitStatusInvocation() - { - Channels = receiveReconnectSuccess.Channels, - ChannelGroups = receiveReconnectSuccess.ChannelGroups, - StatusCategory = PNStatusCategory.PNReconnectedCategory - } - ); - case Events.ReceiveReconnectFailureEvent receiveReconnectFailure: - return new ReceiveReconnectingState() - { - Channels = receiveReconnectFailure.Channels, - ChannelGroups = receiveReconnectFailure.ChannelGroups, - Cursor = receiveReconnectFailure.Cursor - }.With(null); - case Events.ReceiveReconnectGiveUpEvent receiveReconnectGiveUp: - return new ReceiveFailedState() - { - Channels = receiveReconnectGiveUp.Channels, - ChannelGroups = receiveReconnectGiveUp.ChannelGroups, - Cursor = receiveReconnectGiveUp.Cursor - }.With( - new EmitStatusInvocation() - { - Status = receiveReconnectGiveUp.Status - } - ); - default: return null; - } + Events.SubscriptionChangedEvent subscriptionChanged => new ReceivingState() + { + Channels = subscriptionChanged.Channels, + ChannelGroups = subscriptionChanged.ChannelGroups, + Cursor = this.Cursor + }.With(new ReceiveMessagesInvocation() + { + Channels = subscriptionChanged.Channels, + ChannelGroups = subscriptionChanged.ChannelGroups, + Cursor = this.Cursor + }), + + Events.DisconnectEvent disconnect => new ReceiveStoppedState() + { + Channels = disconnect.Channels, + ChannelGroups = disconnect.ChannelGroups, + Cursor = disconnect.Cursor + }.With(new EmitStatusInvocation() + { + Channels = disconnect.Channels, + ChannelGroups = disconnect.ChannelGroups, + StatusCategory = PNStatusCategory.PNDisconnectedCategory + }), + + Events.SubscriptionRestoredEvent subscriptionRestored => new ReceivingState() + { + Channels = subscriptionRestored.Channels, + ChannelGroups = subscriptionRestored.ChannelGroups, + Cursor = subscriptionRestored.Cursor + }.With(null), + + Events.ReceiveReconnectSuccessEvent receiveReconnectSuccess => new ReceivingState() + { + Channels = receiveReconnectSuccess.Channels, + ChannelGroups = receiveReconnectSuccess.ChannelGroups, + Cursor = receiveReconnectSuccess.Cursor + }.With(new EmitStatusInvocation() + { + Channels = receiveReconnectSuccess.Channels, + ChannelGroups = receiveReconnectSuccess.ChannelGroups, + StatusCategory = PNStatusCategory.PNReconnectedCategory + }), + + Events.ReceiveReconnectFailureEvent receiveReconnectFailure => new ReceiveReconnectingState() + { + Channels = receiveReconnectFailure.Channels, + ChannelGroups = receiveReconnectFailure.ChannelGroups, + Cursor = receiveReconnectFailure.Cursor + }.With(null), + + Events.ReceiveReconnectGiveUpEvent receiveReconnectGiveUp => new ReceiveFailedState() + { + Channels = receiveReconnectGiveUp.Channels, + ChannelGroups = receiveReconnectGiveUp.ChannelGroups, + Cursor = receiveReconnectGiveUp.Cursor + }.With(new EmitStatusInvocation() { Status = receiveReconnectGiveUp.Status }), + + _ => null + }; } } } \ No newline at end of file diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveStoppedState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveStoppedState.cs index 16119744c..9684a0ad0 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveStoppedState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveStoppedState.cs @@ -17,31 +17,31 @@ internal class ReceiveStoppedState : Core.IState public Tuple> Transition(IEvent e) { - switch (e) + return e switch { - case Events.SubscriptionChangedEvent subscriptionChanged: - return new ReceivingState() - { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - Cursor = this.Cursor, - }.With(); - case Events.ReconnectEvent reconnect: - return new ReceivingState() - { - Channels = reconnect.Channels, - ChannelGroups = reconnect.ChannelGroups, - Cursor = reconnect.Cursor - }.With(); - case Events.SubscriptionRestoredEvent subscriptionRestored: - return new ReceivingState() - { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups, - Cursor = subscriptionRestored.Cursor - }.With(); - default: return null; - } + Events.SubscriptionChangedEvent subscriptionChanged => new ReceivingState() + { + Channels = subscriptionChanged.Channels, + ChannelGroups = subscriptionChanged.ChannelGroups, + Cursor = this.Cursor, + }.With(), + + Events.ReconnectEvent reconnect => new ReceivingState() + { + Channels = reconnect.Channels, + ChannelGroups = reconnect.ChannelGroups, + Cursor = reconnect.Cursor + }.With(), + + Events.SubscriptionRestoredEvent subscriptionRestored => new ReceivingState() + { + Channels = subscriptionRestored.Channels, + ChannelGroups = subscriptionRestored.ChannelGroups, + Cursor = subscriptionRestored.Cursor + }.With(), + + _ => null + }; } } } \ No newline at end of file diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceivingState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceivingState.cs index 4609f468e..cc467587c 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceivingState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceivingState.cs @@ -18,52 +18,50 @@ internal class ReceivingState : Core.IState public Tuple> Transition(IEvent e) { - switch (e) + return e switch { - case Events.ReceiveSuccessEvent receiveSuccess: - return new ReceivingState() - { - Channels = receiveSuccess.Channels, - ChannelGroups = receiveSuccess.ChannelGroups, - Cursor = receiveSuccess.Cursor - }.With(null); - case Events.SubscriptionChangedEvent subscriptionChanged: - return new ReceivingState() - { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - Cursor = this.Cursor - }.With(); - case Events.SubscriptionRestoredEvent subscriptionRestored: - return new ReceivingState() - { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups, - Cursor = subscriptionRestored.Cursor - }.With(); - case Events.DisconnectEvent disconnect: - return new ReceiveStoppedState() - { - Channels = disconnect.Channels, - ChannelGroups = disconnect.ChannelGroups, - Cursor = disconnect.Cursor - }.With( - new EmitStatusInvocation() - { - Channels = disconnect.Channels, - ChannelGroups = disconnect.ChannelGroups, - StatusCategory = PNStatusCategory.PNDisconnectedCategory - } - ); - case Events.ReceiveFailureEvent receiveFailure: - return new ReceiveReconnectingState() - { - Channels = receiveFailure.Channels, - ChannelGroups = receiveFailure.ChannelGroups, - Cursor = receiveFailure.Cursor - }.With(); - default: return null; - } + Events.ReceiveSuccessEvent receiveSuccess => new ReceivingState() + { + Channels = receiveSuccess.Channels, + ChannelGroups = receiveSuccess.ChannelGroups, + Cursor = receiveSuccess.Cursor + }.With(null), + + Events.SubscriptionChangedEvent subscriptionChanged => new ReceivingState() + { + Channels = subscriptionChanged.Channels, + ChannelGroups = subscriptionChanged.ChannelGroups, + Cursor = this.Cursor + }.With(), + + Events.SubscriptionRestoredEvent subscriptionRestored => new ReceivingState() + { + Channels = subscriptionRestored.Channels, + ChannelGroups = subscriptionRestored.ChannelGroups, + Cursor = subscriptionRestored.Cursor + }.With(), + + Events.DisconnectEvent disconnect => new ReceiveStoppedState() + { + Channels = disconnect.Channels, + ChannelGroups = disconnect.ChannelGroups, + Cursor = disconnect.Cursor + }.With(new EmitStatusInvocation() + { + Channels = disconnect.Channels, + ChannelGroups = disconnect.ChannelGroups, + StatusCategory = PNStatusCategory.PNDisconnectedCategory + }), + + Events.ReceiveFailureEvent receiveFailure => new ReceiveReconnectingState() + { + Channels = receiveFailure.Channels, + ChannelGroups = receiveFailure.ChannelGroups, + Cursor = receiveFailure.Cursor + }.With(), + + _ => null + }; } } } \ No newline at end of file diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/UnsubscribedState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/UnsubscribedState.cs index b66ae792c..757ce04ce 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/UnsubscribedState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/UnsubscribedState.cs @@ -12,23 +12,22 @@ internal class UnsubscribedState : Core.IState public Tuple> Transition(Core.IEvent e) { - switch (e) + return e switch { - case Events.SubscriptionChangedEvent subscriptionChanged: - return new HandshakingState() - { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - }.With(); - case Events.SubscriptionRestoredEvent subscriptionRestored: - return new States.ReceivingState() - { - Channels = subscriptionRestored.Channels, - ChannelGroups = subscriptionRestored.ChannelGroups, - Cursor = subscriptionRestored.Cursor - }.With(); - default: return null; - } + Events.SubscriptionChangedEvent subscriptionChanged => new HandshakingState() + { + Channels = subscriptionChanged.Channels, ChannelGroups = subscriptionChanged.ChannelGroups, + }.With(), + + Events.SubscriptionRestoredEvent subscriptionRestored => new States.ReceivingState() + { + Channels = subscriptionRestored.Channels, + ChannelGroups = subscriptionRestored.ChannelGroups, + Cursor = subscriptionRestored.Cursor + }.With(), + + _ => null + }; } } } \ No newline at end of file From 7303da16ca319d9abf2b3f69476ddec988b961e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dobrza=C5=84ski?= Date: Tue, 27 Jun 2023 11:09:25 +0200 Subject: [PATCH 05/10] WIP --- .../PubnubApi/EventEngine/Subscribe/States/ReceivingState.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceivingState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceivingState.cs index cc467587c..fc5cea68c 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceivingState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceivingState.cs @@ -25,7 +25,7 @@ internal class ReceivingState : Core.IState Channels = receiveSuccess.Channels, ChannelGroups = receiveSuccess.ChannelGroups, Cursor = receiveSuccess.Cursor - }.With(null), + }.With(), Events.SubscriptionChangedEvent subscriptionChanged => new ReceivingState() { From 0c893f350460d0baaaebbb67aaa5b89a21c5fc8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dobrza=C5=84ski?= Date: Tue, 27 Jun 2023 11:09:57 +0200 Subject: [PATCH 06/10] WIP --- .../Subscribe/States/HandshakeReconnectingState.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeReconnectingState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeReconnectingState.cs index 2d9f2e6d8..1b1ff757a 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeReconnectingState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeReconnectingState.cs @@ -20,12 +20,12 @@ internal class HandshakeReconnectingState : Core.IState Events.SubscriptionChangedEvent subscriptionChanged => new HandshakingState() { Channels = subscriptionChanged.Channels, ChannelGroups = subscriptionChanged.ChannelGroups, - }.With(null), + }.With(), Events.DisconnectEvent disconnect => new HandshakeStoppedState() { Channels = disconnect.Channels, ChannelGroups = disconnect.ChannelGroups - }.With(null), + }.With(), Events.HandshakeReconnectGiveUpEvent handshakeReconnectGiveUp => new HandshakeFailedState() { From 6a995f89d544eed8a1b620939a0db5439ce556be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dobrza=C5=84ski?= Date: Tue, 27 Jun 2023 11:10:48 +0200 Subject: [PATCH 07/10] WIP --- .../EventEngine/Subscribe/States/HandshakeStoppedState.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeStoppedState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeStoppedState.cs index 970c7924d..82135d1bc 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeStoppedState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeStoppedState.cs @@ -20,19 +20,19 @@ internal class HandshakeStoppedState : Core.IState Events.SubscriptionChangedEvent subscriptionChanged => new HandshakingState() { Channels = subscriptionChanged.Channels, ChannelGroups = subscriptionChanged.ChannelGroups, - }.With(null), + }.With(), Events.ReconnectEvent reconnect => new HandshakingState() { Channels = reconnect.Channels, ChannelGroups = reconnect.ChannelGroups, - }.With(null), + }.With(), Events.SubscriptionRestoredEvent subscriptionRestored => new ReceivingState() { Channels = subscriptionRestored.Channels, ChannelGroups = subscriptionRestored.ChannelGroups, Cursor = subscriptionRestored.Cursor - }.With(null), + }.With(), Events.HandshakeFailureEvent handshakeFailure => new HandshakeReconnectingState() { From 28eca5644076bcdf12651ae96862c7d871d440a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dobrza=C5=84ski?= Date: Tue, 27 Jun 2023 12:51:57 +0200 Subject: [PATCH 08/10] Review fixes --- .../Subscribe/Events/SubscriptionEvents.cs | 21 ++++-------- .../Invocations/SubscriptionInvocations.cs | 25 +++++++++++--- .../States/HandshakeReconnectingState.cs | 28 ++++++++-------- .../Subscribe/States/HandshakeStoppedState.cs | 15 --------- .../Subscribe/States/HandshakingState.cs | 6 ++-- .../States/ReceiveReconnectingState.cs | 33 +++++-------------- .../Subscribe/States/ReceivingState.cs | 24 +++++++------- 7 files changed, 65 insertions(+), 87 deletions(-) diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/Events/SubscriptionEvents.cs b/src/Api/PubnubApi/EventEngine/Subscribe/Events/SubscriptionEvents.cs index fc71f0745..33cf2b96a 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/Events/SubscriptionEvents.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/Events/SubscriptionEvents.cs @@ -14,30 +14,27 @@ public class SubscriptionRestoredEvent : Core.IEvent { public class HandshakeSuccessEvent : Core.IEvent { public SubscriptionCursor cursor; + public PNStatus Status; } public class HandshakeFailureEvent : Core.IEvent { - // TODO status or reason? public PNStatus Status; } public class HandshakeReconnectSuccessEvent : HandshakeSuccessEvent { - public IEnumerable Channels; - public IEnumerable ChannelGroups; public SubscriptionCursor Cursor; } - public class HandshakeReconnectFailureEvent : HandshakeFailureEvent { + public class HandshakeReconnectFailureEvent : HandshakeFailureEvent + { + public PNStatus Status; } public class HandshakeReconnectRetryEvent : Core.IEvent { } public class HandshakeReconnectGiveUpEvent : Core.IEvent { - public IEnumerable Channels; - public IEnumerable ChannelGroups; - // TODO status or reason? - public PNStatus status; + public PNStatus Status; } public class ReceiveSuccessEvent : Core.IEvent { @@ -45,14 +42,11 @@ public class ReceiveSuccessEvent : Core.IEvent { public IEnumerable ChannelGroups; public List> Messages; public SubscriptionCursor Cursor; + public PNStatus Status; } public class ReceiveFailureEvent : Core.IEvent { - public IEnumerable Channels; - public IEnumerable ChannelGroups; - public SubscriptionCursor Cursor; - // TODO status or reason? - public PNStatus status; + public PNStatus Status; } public class ReceiveReconnectRetry : Core.IEvent { @@ -68,7 +62,6 @@ public class ReceiveReconnectGiveUpEvent : Core.IEvent { public IEnumerable Channels; public IEnumerable ChannelGroups; public SubscriptionCursor Cursor; - // TODO status or reason? public PNStatus Status; } diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/Invocations/SubscriptionInvocations.cs b/src/Api/PubnubApi/EventEngine/Subscribe/Invocations/SubscriptionInvocations.cs index 417e6aa82..7fc22eceb 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/Invocations/SubscriptionInvocations.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/Invocations/SubscriptionInvocations.cs @@ -3,15 +3,32 @@ namespace PubnubApi.PubnubEventEngine.Subscribe.Invocations { internal class EmitMessagesInvocation : Core.IEffectInvocation { - public List messages; + public IEnumerable> Messages; + + public EmitMessagesInvocation(IEnumerable> messages) + { + this.Messages = messages; + } } internal class EmitStatusInvocation : Core.IEffectInvocation { - public IEnumerable Channels; - public IEnumerable ChannelGroups; - // TODO merge status variables into one + // TODO merge status variables into one? public PNStatusCategory StatusCategory; public PNStatus Status; + + public EmitStatusInvocation(PNStatus status) + { + this.Status = status; + } + + public EmitStatusInvocation(PNStatusCategory category) + { + this.StatusCategory = category; + this.Status = new PNStatus() + { + Category = category, + }; + } } internal class HandshakeInvocation : Core.IEffectInvocation { diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeReconnectingState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeReconnectingState.cs index 1b1ff757a..89d8092f6 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeReconnectingState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeReconnectingState.cs @@ -10,7 +10,8 @@ internal class HandshakeReconnectingState : Core.IState public IEnumerable Channels; public IEnumerable ChannelGroups; - public IEnumerable OnEntry { get; } + public IEnumerable OnEntry => new HandshakeReconnectInvocation() + { Channels = this.Channels, ChannelGroups = this.ChannelGroups }.AsArray(); public IEnumerable OnExit { get; } = new CancelHandshakeReconnectInvocation().AsArray(); public Tuple> Transition(IEvent e) @@ -29,25 +30,24 @@ internal class HandshakeReconnectingState : Core.IState Events.HandshakeReconnectGiveUpEvent handshakeReconnectGiveUp => new HandshakeFailedState() { - Channels = handshakeReconnectGiveUp.Channels, - ChannelGroups = handshakeReconnectGiveUp.ChannelGroups - }.With(), + Channels = this.Channels, + ChannelGroups = this.ChannelGroups + }.With( + new EmitStatusInvocation(handshakeReconnectGiveUp.Status) + ), Events.HandshakeReconnectSuccessEvent handshakeReconnectSuccess => new ReceivingState() { - Channels = handshakeReconnectSuccess.Channels, - ChannelGroups = handshakeReconnectSuccess.ChannelGroups, + Channels = this.Channels, + ChannelGroups = this.ChannelGroups, Cursor = handshakeReconnectSuccess.Cursor - }.With(new EmitStatusInvocation() - { - Channels = handshakeReconnectSuccess.Channels, - ChannelGroups = handshakeReconnectSuccess.ChannelGroups, - StatusCategory = PNStatusCategory.PNReconnectedCategory - }), + }.With(new EmitStatusInvocation(handshakeReconnectSuccess.Status)), - Events.SubscriptionRestoredEvent subscriptionRestored => new HandshakeFailedState() + Events.SubscriptionRestoredEvent subscriptionRestored => new ReceivingState() { - Channels = subscriptionRestored.Channels, ChannelGroups = subscriptionRestored.ChannelGroups + Channels = subscriptionRestored.Channels, + ChannelGroups = subscriptionRestored.ChannelGroups, + Cursor = subscriptionRestored.Cursor }.With(), _ => null diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeStoppedState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeStoppedState.cs index 82135d1bc..91df0fca8 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeStoppedState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakeStoppedState.cs @@ -34,21 +34,6 @@ internal class HandshakeStoppedState : Core.IState Cursor = subscriptionRestored.Cursor }.With(), - Events.HandshakeFailureEvent handshakeFailure => new HandshakeReconnectingState() - { - Channels = this.Channels, ChannelGroups = this.ChannelGroups, - }.With(new EmitStatusInvocation() { Status = handshakeFailure.Status }), - - Events.DisconnectEvent disconnect => new HandshakeStoppedState() - { - Channels = disconnect.Channels, ChannelGroups = disconnect.ChannelGroups, - }.With(new EmitStatusInvocation() { StatusCategory = PNStatusCategory.PNDisconnectedCategory }), - - Events.HandshakeSuccessEvent handshakeSuccess => new ReceivingState() - { - Channels = this.Channels, ChannelGroups = this.ChannelGroups, Cursor = handshakeSuccess.cursor - }.With(new EmitStatusInvocation() { StatusCategory = PNStatusCategory.PNConnectedCategory }), - _ => null }; } diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakingState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakingState.cs index 6a8c56935..33ca844d1 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakingState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakingState.cs @@ -34,17 +34,17 @@ internal class HandshakingState : Core.IState Events.HandshakeFailureEvent handshakeFailure => new States.HandshakeFailedState() { Channels = this.Channels, ChannelGroups = this.ChannelGroups - }.With(new EmitStatusInvocation() { Status = handshakeFailure.Status }), + }.With(new EmitStatusInvocation(handshakeFailure.Status)), Events.DisconnectEvent disconnect => new States.HandshakeStoppedState() { Channels = disconnect.Channels, ChannelGroups = disconnect.ChannelGroups, - }.With(new EmitStatusInvocation() { StatusCategory = PNStatusCategory.PNDisconnectedCategory }), + }.With(new EmitStatusInvocation(PNStatusCategory.PNDisconnectedCategory)), Events.HandshakeSuccessEvent success => new ReceivingState() { Channels = this.Channels, ChannelGroups = this.ChannelGroups, Cursor = success.cursor - }.With(new EmitStatusInvocation() { StatusCategory = PNStatusCategory.PNConnectedCategory }), + }.With(new EmitStatusInvocation(success.Status)), _ => null }; diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveReconnectingState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveReconnectingState.cs index f1f2fbda3..159d229cc 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveReconnectingState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveReconnectingState.cs @@ -26,57 +26,42 @@ internal class ReceiveReconnectingState : Core.IState Channels = subscriptionChanged.Channels, ChannelGroups = subscriptionChanged.ChannelGroups, Cursor = this.Cursor - }.With(new ReceiveMessagesInvocation() - { - Channels = subscriptionChanged.Channels, - ChannelGroups = subscriptionChanged.ChannelGroups, - Cursor = this.Cursor - }), + }.With(), Events.DisconnectEvent disconnect => new ReceiveStoppedState() { Channels = disconnect.Channels, ChannelGroups = disconnect.ChannelGroups, Cursor = disconnect.Cursor - }.With(new EmitStatusInvocation() - { - Channels = disconnect.Channels, - ChannelGroups = disconnect.ChannelGroups, - StatusCategory = PNStatusCategory.PNDisconnectedCategory - }), + }.With(new EmitStatusInvocation(PNStatusCategory.PNDisconnectedCategory)), Events.SubscriptionRestoredEvent subscriptionRestored => new ReceivingState() { Channels = subscriptionRestored.Channels, ChannelGroups = subscriptionRestored.ChannelGroups, Cursor = subscriptionRestored.Cursor - }.With(null), + }.With(), Events.ReceiveReconnectSuccessEvent receiveReconnectSuccess => new ReceivingState() { Channels = receiveReconnectSuccess.Channels, ChannelGroups = receiveReconnectSuccess.ChannelGroups, Cursor = receiveReconnectSuccess.Cursor - }.With(new EmitStatusInvocation() - { - Channels = receiveReconnectSuccess.Channels, - ChannelGroups = receiveReconnectSuccess.ChannelGroups, - StatusCategory = PNStatusCategory.PNReconnectedCategory - }), + }.With(new EmitStatusInvocation(receiveReconnectSuccess.Status)), Events.ReceiveReconnectFailureEvent receiveReconnectFailure => new ReceiveReconnectingState() { - Channels = receiveReconnectFailure.Channels, - ChannelGroups = receiveReconnectFailure.ChannelGroups, - Cursor = receiveReconnectFailure.Cursor - }.With(null), + Channels = this.Channels, + ChannelGroups = this.ChannelGroups, + Cursor = this.Cursor + }.With(new EmitStatusInvocation(receiveReconnectFailure.Status)), Events.ReceiveReconnectGiveUpEvent receiveReconnectGiveUp => new ReceiveFailedState() { Channels = receiveReconnectGiveUp.Channels, ChannelGroups = receiveReconnectGiveUp.ChannelGroups, Cursor = receiveReconnectGiveUp.Cursor - }.With(new EmitStatusInvocation() { Status = receiveReconnectGiveUp.Status }), + }.With(new EmitStatusInvocation(receiveReconnectGiveUp.Status)), _ => null }; diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceivingState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceivingState.cs index fc5cea68c..882eb3987 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceivingState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceivingState.cs @@ -25,7 +25,10 @@ internal class ReceivingState : Core.IState Channels = receiveSuccess.Channels, ChannelGroups = receiveSuccess.ChannelGroups, Cursor = receiveSuccess.Cursor - }.With(), + }.With( + new EmitMessagesInvocation(receiveSuccess.Messages), + new EmitStatusInvocation(receiveSuccess.Status) + ), Events.SubscriptionChangedEvent subscriptionChanged => new ReceivingState() { @@ -43,21 +46,16 @@ internal class ReceivingState : Core.IState Events.DisconnectEvent disconnect => new ReceiveStoppedState() { - Channels = disconnect.Channels, - ChannelGroups = disconnect.ChannelGroups, - Cursor = disconnect.Cursor - }.With(new EmitStatusInvocation() - { - Channels = disconnect.Channels, - ChannelGroups = disconnect.ChannelGroups, - StatusCategory = PNStatusCategory.PNDisconnectedCategory - }), + Channels = this.Channels, + ChannelGroups = this.ChannelGroups, + Cursor = this.Cursor + }.With(new EmitStatusInvocation(PNStatusCategory.PNDisconnectedCategory)), Events.ReceiveFailureEvent receiveFailure => new ReceiveReconnectingState() { - Channels = receiveFailure.Channels, - ChannelGroups = receiveFailure.ChannelGroups, - Cursor = receiveFailure.Cursor + Channels = this.Channels, + ChannelGroups = this.ChannelGroups, + Cursor = this.Cursor }.With(), _ => null From 928e614291a4a0f7ce253f5c03efb9c6559767d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dobrza=C5=84ski?= Date: Tue, 27 Jun 2023 14:15:52 +0200 Subject: [PATCH 09/10] member case --- .../EventEngine/Subscribe/Effects/HandshakeEffectHandler.cs | 2 +- .../EventEngine/Subscribe/Events/SubscriptionEvents.cs | 2 +- .../PubnubApi/EventEngine/Subscribe/States/HandshakingState.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/Effects/HandshakeEffectHandler.cs b/src/Api/PubnubApi/EventEngine/Subscribe/Effects/HandshakeEffectHandler.cs index a28db2be5..72a15f8ce 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/Effects/HandshakeEffectHandler.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/Effects/HandshakeEffectHandler.cs @@ -37,7 +37,7 @@ public async Task Run(HandshakeInvocation invocation) { Timetoken = handshakeResponse.Timetoken.Timestamp }; - eventQueue.Enqueue(new Events.HandshakeSuccessEvent() {cursor = c}); + eventQueue.Enqueue(new Events.HandshakeSuccessEvent() {Cursor = c}); } } diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/Events/SubscriptionEvents.cs b/src/Api/PubnubApi/EventEngine/Subscribe/Events/SubscriptionEvents.cs index 33cf2b96a..42739802d 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/Events/SubscriptionEvents.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/Events/SubscriptionEvents.cs @@ -13,7 +13,7 @@ public class SubscriptionRestoredEvent : Core.IEvent { } public class HandshakeSuccessEvent : Core.IEvent { - public SubscriptionCursor cursor; + public SubscriptionCursor Cursor; public PNStatus Status; } diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakingState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakingState.cs index 33ca844d1..154282a13 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakingState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/HandshakingState.cs @@ -43,7 +43,7 @@ internal class HandshakingState : Core.IState Events.HandshakeSuccessEvent success => new ReceivingState() { - Channels = this.Channels, ChannelGroups = this.ChannelGroups, Cursor = success.cursor + Channels = this.Channels, ChannelGroups = this.ChannelGroups, Cursor = success.Cursor }.With(new EmitStatusInvocation(success.Status)), _ => null From 08a3dc28068bc8e7c6aaf7c45d134c5961ef966a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Dobrza=C5=84ski?= Date: Tue, 27 Jun 2023 14:31:35 +0200 Subject: [PATCH 10/10] removed With(null) --- .../EventEngine/Subscribe/States/ReceiveFailedState.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveFailedState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveFailedState.cs index 135df318e..3bb0c7c3a 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveFailedState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceiveFailedState.cs @@ -23,21 +23,21 @@ internal class ReceiveFailedState : Core.IState Channels = subscriptionChanged.Channels, ChannelGroups = subscriptionChanged.ChannelGroups, Cursor = this.Cursor - }.With(null), + }.With(), Events.ReconnectEvent reconnect => new ReceivingState() { Channels = reconnect.Channels, ChannelGroups = reconnect.ChannelGroups, Cursor = reconnect.Cursor - }.With(null), + }.With(), Events.SubscriptionRestoredEvent subscriptionRestored => new ReceivingState() { Channels = subscriptionRestored.Channels, ChannelGroups = subscriptionRestored.ChannelGroups, Cursor = subscriptionRestored.Cursor - }.With(null), + }.With(), _ => null };