-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathStateProjectionGAgentBase.cs
More file actions
106 lines (91 loc) · 4.15 KB
/
Copy pathStateProjectionGAgentBase.cs
File metadata and controls
106 lines (91 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System.Reflection;
using Aevatar.Core.Abstractions;
using Aevatar.Agent.Abstractions;
using Orleans.Streams;
namespace Aevatar.Core;
public abstract class StateProjectionGAgentBase<TProjectionState, TState, TStateLogEvent> :
StateProjectionGAgentBase<TProjectionState, TState, TStateLogEvent, EventBase>
where TProjectionState : StateBase, new()
where TState : StateBase, new()
where TStateLogEvent : StateLogEventBase<TStateLogEvent>;
public abstract class StateProjectionGAgentBase<TProjectionState, TState, TStateLogEvent, TEvent> :
StateProjectionGAgentBase<TProjectionState, TState, TStateLogEvent, TEvent, ConfigurationBase>
where TProjectionState : StateBase, new()
where TState : StateBase, new()
where TStateLogEvent : StateLogEventBase<TStateLogEvent>
where TEvent : EventBase;
public abstract class StateProjectionGAgentBase<TProjectionState, TState, TStateLogEvent, TEvent, TConfiguration> :
GAgentBase<TState, TStateLogEvent, TEvent, TConfiguration>
where TProjectionState : StateBase, new()
where TState : StateBase, new()
where TStateLogEvent : StateLogEventBase<TStateLogEvent>
where TEvent : EventBase
where TConfiguration : ConfigurationBase
{
private readonly List<StateBaseAsyncObserver> _stateObservers = [];
private readonly List<StreamSubscriptionHandle<StateWrapper<TProjectionState>>> _subscription = [];
protected override async Task OnGAgentActivateAsync(CancellationToken cancellationToken)
{
await UpdateStateLogEventObserverListAsync();
await SubscribeStateProjectionStreamAsync();
}
public override async Task OnDeactivateAsync(DeactivationReason reason, CancellationToken cancellationToken)
{
foreach (var subscription in _subscription)
{
await subscription.UnsubscribeAsync();
}
await base.OnDeactivateAsync(reason, cancellationToken);
}
protected abstract Task HandleStateAsync(StateWrapper<TProjectionState> projectionStateWrapper);
protected int ProjectStateVersion { get; set; }
private async Task UpdateStateLogEventObserverListAsync()
{
var stateHandlerMethods = GetStateHandlerMethods(GetType());
foreach (var stateLogEventHandlerMethod in stateHandlerMethods)
{
var observer = new StateBaseAsyncObserver(async item =>
{
var version = (int)item.GetType().GetProperty(nameof(StateWrapper<TProjectionState>.Version))?.GetValue(item)!;
if (ProjectStateVersion >= version)
{
return;
}
ProjectStateVersion = version;
var result = stateLogEventHandlerMethod.Invoke(this, [item]);
await (Task)result!;
})
{
GAgentGuid = this.GetPrimaryKey()
};
_stateObservers.Add(observer);
}
}
private async Task SubscribeStateProjectionStreamAsync()
{
var projectionStream = GetStateProjectionStream();
foreach (var stateObserver in _stateObservers)
{
var subscription = await projectionStream.SubscribeAsync(stateObserver);
_subscription.Add(subscription);
}
}
private IEnumerable<MethodInfo> GetStateHandlerMethods(Type type)
{
return type
.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
.Where(IsStateHandlerMethod);
}
private bool IsStateHandlerMethod(MethodInfo methodInfo)
{
return methodInfo.GetParameters().Length == 1 &&
(methodInfo.GetCustomAttribute<StateLogEventHandlerAttribute>() != null ||
methodInfo.Name == AevatarGAgentConstants.StateHandlerDefaultMethodName) &&
methodInfo.GetParameters()[0].ParameterType == typeof(StateWrapper<TProjectionState>);
}
private IAsyncStream<StateWrapper<TProjectionState>> GetStateProjectionStream()
{
var streamId = StreamId.Create(AevatarOptions.StreamNamespace, typeof(StateWrapper<TProjectionState>).FullName!);
return StreamProvider.GetStream<StateWrapper<TProjectionState>>(streamId);
}
}