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
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 @@ -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});
}
}

Expand Down
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 @@ -14,46 +13,40 @@ public class SubscriptionRestoredEvent : Core.IEvent {
}

public class HandshakeSuccessEvent : Core.IEvent {
public SubscriptionCursor cursor;
public SubscriptionCursor Cursor;
public PNStatus Status;
}

public class HandshakeFailureEvent : Core.IEvent {
// TODO status or reason?
public PNStatus status;
public PNStatus Status;
}

public class HandshakeReconnectSuccessEvent : HandshakeSuccessEvent {
public IEnumerable<string> Channels;
public IEnumerable<string> 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<string> Channels;
public IEnumerable<string> ChannelGroups;
// TODO status or reason?
public PNStatus status;
public PNStatus Status;
}

public class ReceiveSuccessEvent : Core.IEvent {
public IEnumerable<string> Channels;
public IEnumerable<string> ChannelGroups;
public List<PNMessageResult<object>> Messages;
public SubscriptionCursor Cursor;
public PNStatus Status;
}

public class ReceiveFailureEvent : Core.IEvent {
public IEnumerable<string> Channels;
public IEnumerable<string> ChannelGroups;
public SubscriptionCursor Cursor;
// TODO status or reason?
public PNStatus status;
public PNStatus Status;
}

public class ReceiveReconnectRetry : Core.IEvent {
Expand All @@ -69,8 +62,7 @@ public class ReceiveReconnectGiveUpEvent : Core.IEvent {
public IEnumerable<string> Channels;
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 @@ -3,17 +3,38 @@

namespace PubnubApi.PubnubEventEngine.Subscribe.Invocations {
internal class EmitMessagesInvocation : Core.IEffectInvocation {
public List<object> messages;
public IEnumerable<PNMessageResult<object>> Messages;

public EmitMessagesInvocation(IEnumerable<PNMessageResult<object>> messages)
{
this.Messages = messages;
}
}

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;

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 {
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 +48,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 +66,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
};
}
}
}
Loading