Skip to content

Eventengine/state simplification #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Api/PubnubApi/EventEngine/Core/EventEngineInterfaces.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace PubnubApi.PubnubEventEngine.Core {
Expand All @@ -17,14 +18,14 @@ internal interface IEffectCancelInvocation : IEffectInvocation { }
internal interface IEvent { };

internal interface IState {
public abstract IEnumerable<IEffectInvocation> OnEntry { get; }
public abstract IEnumerable<IEffectInvocation> OnExit { get; }
public IEnumerable<IEffectInvocation> OnEntry { get; }
public IEnumerable<IEffectInvocation> OnExit { get; }

/// <summary>
/// The EE transition pure function.
/// </summary>
/// <param name="e">Input event</param>
/// <returns>Target state and invocation list, or null for no-transition</returns>
public abstract System.Tuple<IState, IEnumerable<IEffectInvocation>> Transition(IEvent e);
public System.Tuple<IState, IEnumerable<IEffectInvocation>> Transition(IEvent e);
}
}
19 changes: 19 additions & 0 deletions src/Api/PubnubApi/EventEngine/Core/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace PubnubApi.PubnubEventEngine.Core
{
internal static class Utils
{
internal static Tuple<IState, IEnumerable<IEffectInvocation>> With(this IState state, params IEffectInvocation[] invocations)
{
return new Tuple<IState, IEnumerable<IEffectInvocation>>(state, invocations);
}

internal static IEffectInvocation[] AsArray(this IEffectInvocation invocation)
{
return new IEffectInvocation[] { invocation };
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace PubnubApi.PubnubEventEngine.Subscribe.Events {
public class SubscriptionChangedEvent : Core.IEvent {
public IEnumerable<string> Channels;
public IEnumerable<string> ChannelGroups;
public SubscriptionCursor Cursor;
}

public class SubscriptionRestoredEvent : Core.IEvent {
Expand All @@ -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 {
Expand Down Expand Up @@ -70,7 +69,7 @@ public class ReceiveReconnectGiveUpEvent : Core.IEvent {
public IEnumerable<string> ChannelGroups;
public SubscriptionCursor Cursor;
// TODO status or reason?
public PNStatus status;
public PNStatus Status;
}

public class DisconnectEvent : Core.IEvent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ internal class EmitMessagesInvocation : Core.IEffectInvocation {
internal class EmitStatusInvocation : Core.IEffectInvocation {
public IEnumerable<string> Channels;
public IEnumerable<string> ChannelGroups;
// TODO merge status variables into one
public PNStatusCategory StatusCategory;
public PNStatus Status;
}

internal class HandshakeInvocation : Core.IEffectInvocation {
public IEnumerable<string> Channels;
public IEnumerable<string> ChannelGroups;
// TODO if we need these, figure out how to pass them.
public Dictionary<string, string> InitialSubscribeQueryParams = new Dictionary<string, string>();
public Dictionary<string, object> ExternalQueryParams = new Dictionary<string, object>();
}
Expand All @@ -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<string> Channels;
Expand All @@ -46,5 +49,4 @@ internal class ReceiveReconnectInvocation: Core.IEffectInvocation
}

internal class CancelReceiveReconnectInvocation: ReceiveReconnectInvocation, Core.IEffectCancelInvocation { }
//internal class CancelReconnectInvocation : ReconnectInvocation, Core.IEffectCancelInvocation { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,39 @@
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<string> Channels;
public IEnumerable<string> ChannelGroups;

public IEnumerable<string> Channels;
public IEnumerable<string> ChannelGroups;
public SubscriptionCursor Cursor;
public IEnumerable<IEffectInvocation> OnEntry { get; }
public IEnumerable<IEffectInvocation> OnExit { get; }

public IEnumerable<IEffectInvocation> OnEntry { get; }
public IEnumerable<IEffectInvocation> OnExit { get; }
public Tuple<Core.IState, IEnumerable<IEffectInvocation>> Transition(IEvent e) {
switch (e) {
case Events.SubscriptionChangedEvent subscriptionChanged:
return new Tuple<Core.IState, IEnumerable<IEffectInvocation>>(
new HandshakingState() {
Channels = subscriptionChanged.Channels,
ChannelGroups = subscriptionChanged.ChannelGroups,
},
new[] {
new HandshakeInvocation() {
Channels = subscriptionChanged.Channels,
ChannelGroups = subscriptionChanged.ChannelGroups,
},
}
);
case Events.ReconnectEvent reconnect:
return new Tuple<Core.IState, IEnumerable<IEffectInvocation>>(
new HandshakingState() {
Channels = reconnect.Channels,
ChannelGroups = reconnect.ChannelGroups,
},
new[] {
new HandshakeInvocation() {
Channels = reconnect.Channels,
ChannelGroups = reconnect.ChannelGroups,
},
}
);
case Events.SubscriptionRestoredEvent subscriptionRestored:
return new Tuple<IState, IEnumerable<IEffectInvocation>>(
new ReceivingState() {
Channels = subscriptionRestored.Channels,
ChannelGroups = subscriptionRestored.ChannelGroups,
Cursor = subscriptionRestored.Cursor
},
new[] {
new ReceiveMessagesInvocation() {
Channels = subscriptionRestored.Channels,
ChannelGroups = subscriptionRestored.ChannelGroups,
Cursor = subscriptionRestored.Cursor
},
}
);
public Tuple<Core.IState, IEnumerable<IEffectInvocation>> Transition(IEvent e)
{
return e switch
{
Events.SubscriptionChangedEvent subscriptionChanged => new HandshakingState()
{
Channels = subscriptionChanged.Channels, ChannelGroups = subscriptionChanged.ChannelGroups,
}.With(),

default: return null;
}
}
}
}
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
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,55 @@
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<string> Channels;
public IEnumerable<string> ChannelGroups;

public IEnumerable<string> Channels;
public IEnumerable<string> ChannelGroups;
public IEnumerable<IEffectInvocation> OnEntry { get; }
public IEnumerable<IEffectInvocation> OnExit { get; } = new CancelHandshakeReconnectInvocation().AsArray();

public IEnumerable<IEffectInvocation> OnEntry { get; }
public IEnumerable<IEffectInvocation> OnExit { get; }
public Tuple<Core.IState, IEnumerable<IEffectInvocation>> Transition(IEvent e) {
switch (e) {
case Events.SubscriptionChangedEvent subscriptionChanged:
return new Tuple<Core.IState, IEnumerable<IEffectInvocation>>(
new HandshakingState() {
Channels = subscriptionChanged.Channels,
ChannelGroups = subscriptionChanged.ChannelGroups,
},
new[] {
new HandshakeInvocation() {
Channels = subscriptionChanged.Channels,
ChannelGroups = subscriptionChanged.ChannelGroups,
},
}
);
case Events.DisconnectEvent disconnect:
return new Tuple<Core.IState, IEnumerable<IEffectInvocation>>(
new HandshakeStoppedState() {
Channels = disconnect.Channels,
ChannelGroups = disconnect.ChannelGroups
},
null
);
case Events.HandshakeReconnectGiveUpEvent handshakeReconnectGiveUp:
return new Tuple<IState, IEnumerable<IEffectInvocation>>(
new HandshakeFailedState() {
Channels = handshakeReconnectGiveUp.Channels,
ChannelGroups = handshakeReconnectGiveUp.ChannelGroups
},
null
);
case Events.HandshakeReconnectSuccessEvent handshakeReconnectSuccess:
return new Tuple<IState, IEnumerable<IEffectInvocation>>(
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<IState, IEnumerable<IEffectInvocation>>(
new HandshakeFailedState() {
Channels = subscriptionRestored.Channels,
ChannelGroups = subscriptionRestored.ChannelGroups,
Cursor = subscriptionRestored.Cursor
},
null
);
public Tuple<Core.IState, IEnumerable<IEffectInvocation>> Transition(IEvent e)
{
return e switch
{
Events.SubscriptionChangedEvent subscriptionChanged => new HandshakingState()
{
Channels = subscriptionChanged.Channels, ChannelGroups = subscriptionChanged.ChannelGroups,
}.With(null),

default: return 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
};
}
}
}
Loading