Skip to content
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

Feat/events reworked: 0.15.0-alpha #33

Merged
merged 19 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
46 changes: 21 additions & 25 deletions Core/Stateflows.Common/Activities/Classes/ActivityWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Stateflows.Activities;
using Stateflows.Activities.Events;
Expand All @@ -16,39 +18,33 @@ public ActivityWrapper(IBehavior consumer)
Behavior = consumer;
}

public Task<RequestResult<ExecutionResponse>> ExecuteAsync(Event initializationEvent, Action<IInputContainer> inputBuilder = null)
public Task<SendResult> SendInputAsync(Action<ITokensInput> tokensAction)
{
var request = new ExecutionRequest() { InitializationEvent = initializationEvent };

inputBuilder?.Invoke(request);

return Behavior.RequestAsync(request);
var stream = new TokensInput();
tokensAction(stream);
return SendAsync(stream);
}

public Task<RequestResult<ExecutionResponse>> ExecuteAsync(Action<IInputContainer> inputBuilder = null)
public Task<SendResult> SendInputAsync<TToken>(params TToken[] tokens)
{
var request = new ExecutionRequest();

inputBuilder?.Invoke(request);

return Behavior.RequestAsync(request);
var stream = new TokensInput<TToken>()
{
Tokens = tokens
.Select(token => new TokenHolder<TToken>() { Payload = token } as TokenHolder)
.ToList()
};

return SendAsync(stream);
}

public Task<SendResult> SendAsync<TEvent>(TEvent @event)
where TEvent : Event, new()
=> Behavior.SendAsync(@event);

public Task<RequestResult<TResponse>> RequestAsync<TResponse>(Request<TResponse> request)
where TResponse : Response, new()
=> Behavior.RequestAsync(request);
public Task<SendResult> SendAsync<TEvent>(TEvent @event, IEnumerable<EventHeader> headers = null)
=> Behavior.SendAsync(@event, headers);

public Task WatchAsync<TNotification>(Action<TNotification> handler)
where TNotification : Notification, new()
=> Behavior.WatchAsync<TNotification>(handler);
public Task<RequestResult<TResponse>> RequestAsync<TResponse>(IRequest<TResponse> request, IEnumerable<EventHeader> headers = null)
=> Behavior.RequestAsync(request, headers);

public Task UnwatchAsync<TNotification>()
where TNotification : Notification, new()
=> Behavior.UnwatchAsync<TNotification>();
public Task<IWatcher> WatchAsync<TNotificationEvent>(Action<TNotificationEvent> handler)
=> Behavior.WatchAsync(handler);

public void Dispose()
{
Expand Down
16 changes: 12 additions & 4 deletions Core/Stateflows.Common/Activities/Classes/TokenHolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,32 @@ public abstract class TokenHolder
public object BoxedPayload { get; }

protected abstract object GetBoxedPayload();

[JsonIgnore]
public Type PayloadType => GetPayloadType();

protected abstract Type GetPayloadType();
}

public class TokenHolder<T> : TokenHolder
public class TokenHolder<TToken> : TokenHolder
{
public TokenHolder()
{
Payload = default;
}

public override string Name => name ??= typeof(T).GetReadableName();
public override string Name => name ??= typeof(TToken).GetReadableName();

public T Payload { get; set; }
public TToken Payload { get; set; }

protected override object GetBoxedPayload()
=> Payload;

protected override Type GetPayloadType()
=> typeof(TToken);

public override bool Equals(object obj)
=> obj is TokenHolder token && token.Id == Id;
=> obj is TokenHolder holder && holder.Id == Id;

public override int GetHashCode()
=> Id.GetHashCode();
Expand Down
29 changes: 0 additions & 29 deletions Core/Stateflows.Common/Activities/Events/ExecutionRequest.cs

This file was deleted.

12 changes: 0 additions & 12 deletions Core/Stateflows.Common/Activities/Events/ExecutionResponse.cs

This file was deleted.

93 changes: 93 additions & 0 deletions Core/Stateflows.Common/Activities/Events/TokensTransfer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System.Linq;
using System.Collections.Generic;
using Newtonsoft.Json;
using Stateflows.Common;

namespace Stateflows.Activities.Events
{
public interface ITokensInput
{
ITokensInput Add<TToken>(TToken token);
ITokensInput AddRange<TToken>(params TToken[] tokens);
}

public interface ITokensOutput
{
IEnumerable<TToken> GetAllOfType<TToken>();
}

public interface ITokensOutput<out TToken>
{
IEnumerable<TToken> GetAll();
}

public class TokensTransferEvent
{
[JsonProperty(TypeNameHandling = TypeNameHandling.None)]
public List<TokenHolder> Tokens { get; set; } = new List<TokenHolder>();
}

public class TokensInputEvent : TokensTransferEvent
{ }

public class TokensOutputEvent : TokensTransferEvent
{ }

public sealed class TokensInput : TokensInputEvent, ITokensInput, IRequest<TokensOutput>
{
ITokensInput ITokensInput.Add<TToken>(TToken token)
=> Add(token);

public TokensInput Add<TToken>(TToken token)
{
Tokens.Add(new TokenHolder<TToken>() { Payload = token });

return this;
}

ITokensInput ITokensInput.AddRange<TToken>(params TToken[] tokens)
=> AddRange(tokens);

public TokensInput AddRange<TToken>(params TToken[] tokens)
{
Tokens.AddRange(tokens.Select(token => new TokenHolder<TToken>() { Payload = token }));

return this;
}
}

public sealed class TokensOutput : TokensOutputEvent, ITokensOutput
{
IEnumerable<TToken> ITokensOutput.GetAllOfType<TToken>()
=> GetOfType<TToken>();

public IEnumerable<TToken> GetOfType<TToken>()
=> Tokens.OfType<TokenHolder<TToken>>().Select(holder => holder.Payload);
}

public sealed class TokensInput<TToken> : TokensInputEvent, IRequest<TokensOutput>
{
public TokensInput<TToken> Add(TToken token)
{
Tokens.Add(new TokenHolder<TToken>() { Payload = token });

return this;
}

public TokensInput<TToken> AddRange(params TToken[] tokens)
{
Tokens.AddRange(tokens.Select(token => new TokenHolder<TToken>() { Payload = token }));

return this;
}
}

public sealed class TokensOutput<TToken> : TokensOutputEvent, ITokensOutput<TToken>
{
IEnumerable<TToken> ITokensOutput<TToken>.GetAll()
=> GetAll();

public IEnumerable<TToken> GetAll()
=> Tokens.OfType<TokenHolder<TToken>>().Select(holder => holder.Payload);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

namespace Stateflows.Activities
{
public static class ExecutionResponseExtensions
{
public static IEnumerable<T> GetOutputTokensOfType<T>(this ExecutionResponse response)
=> response != null
? response.OutputTokens.OfType<TokenHolder<T>>().Select(t => t.Payload).ToArray()
: new T[0];
//public static class ExecutionResponseExtensions
//{
// public static IEnumerable<T> GetOutputTokensOfType<T>(this ExecutionResponse response)
// => response != null
// ? response.OutputTokens.OfType<TokenHolder<T>>().Select(t => t.Payload).ToArray()
// : new T[0];

public static bool TryGetOutputTokenOfType<T>(this ExecutionResponse response, out T token)
{
var valueTokens = response.GetOutputTokensOfType<T>();
// public static bool TryGetOutputTokenOfType<T>(this ExecutionResponse response, out T token)
// {
// var valueTokens = response.GetOutputTokensOfType<T>();

token = valueTokens.FirstOrDefault();
// token = valueTokens.FirstOrDefault();

return valueTokens.Any();
}
}
// return valueTokens.Any();
// }
//}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
using System;
using System.Threading.Tasks;
using Stateflows.Common;
using Stateflows.Activities.Events;
using Stateflows.Common;
using Stateflows.Common.Interfaces;

namespace Stateflows.Activities
{
public interface IActivityBehavior : IBehavior
{
Task<RequestResult<ExecutionResponse>> ExecuteAsync(Event initializationEvent, Action<IInputContainer> inputBuilder = null);
Task<RequestResult<ExecutionResponse>> ExecuteAsync(Action<IInputContainer> inputBuilder = null);
}
public interface IActivityBehavior : IBehavior, IInputOutput
{ }
}
8 changes: 0 additions & 8 deletions Core/Stateflows.Common/Attributes/DoNotTraceAttribute.cs

This file was deleted.

15 changes: 15 additions & 0 deletions Core/Stateflows.Common/Attributes/EventAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace Stateflows.Common
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Struct, AllowMultiple = false)]
public sealed class EventAttribute : Attribute
{
public string Name { get; set; }

public EventAttribute(string name)
{
Name = name;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Stateflows.Common
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Struct, AllowMultiple = false)]
public sealed class NoImplicitInitializationAttribute : Attribute
{ }
}
8 changes: 8 additions & 0 deletions Core/Stateflows.Common/Attributes/NoTracingAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace Stateflows.Common
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Struct, AllowMultiple = false)]
public sealed class NoTracingAttribute : Attribute
{ }
}
Loading
Loading