|
| 1 | +using System.Runtime.CompilerServices; |
| 2 | +using EventStore.Client; |
| 3 | + |
| 4 | +namespace Kurrent.Client.Streams.GettingState; |
| 5 | + |
| 6 | +public class ProjectStateOptions<TState> { |
| 7 | + public Func<ResolvedEvent, string>? GetProjectedId { get; set; } |
| 8 | + |
| 9 | + public IStateCache<TState>? StateCache { get; set; } |
| 10 | +} |
| 11 | + |
| 12 | +public static class KurrentClientProjectStateExtensions { |
| 13 | + public static async IAsyncEnumerable<StateAtPointInTime<TState>> ProjectState<TState>( |
| 14 | + this IAsyncEnumerable<ResolvedEvent> messages, |
| 15 | + TState initialState, |
| 16 | + Func<TState, ResolvedEvent, TState> evolve, |
| 17 | + ProjectStateOptions<TState>? options, |
| 18 | + [EnumeratorCancellation] CancellationToken ct |
| 19 | + ) where TState : notnull { |
| 20 | + if (messages is KurrentClient.ReadStreamResult readStreamResult) { |
| 21 | + if (await readStreamResult.ReadState.ConfigureAwait(false) == ReadState.StreamNotFound) { |
| 22 | + yield return new StateAtPointInTime<TState>(initialState); |
| 23 | + |
| 24 | + yield break; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + var getProjectedId = options?.GetProjectedId ?? (resolvedEvent => resolvedEvent.OriginalStreamId); |
| 29 | + var stateCache = options?.StateCache ?? new DictionaryStateCache<TState>(); |
| 30 | + |
| 31 | + await foreach (var resolvedEvent in messages.WithCancellation(ct)) { |
| 32 | + var projectedId = getProjectedId(resolvedEvent); |
| 33 | + |
| 34 | + var state = await stateCache.GetValueOrDefaultAsync(projectedId, initialState, ct).ConfigureAwait(false); |
| 35 | + |
| 36 | + state = evolve(state, resolvedEvent); |
| 37 | + |
| 38 | + await stateCache.SetValueAsync(projectedId, state, ct).ConfigureAwait(false); |
| 39 | + |
| 40 | + yield return new StateAtPointInTime<TState>( |
| 41 | + state, |
| 42 | + resolvedEvent.Event.EventNumber, |
| 43 | + resolvedEvent.Event.Position |
| 44 | + ); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public static IAsyncEnumerable<StateAtPointInTime<TState>> ProjectState<TState>( |
| 49 | + this IAsyncEnumerable<ResolvedEvent> messages, |
| 50 | + TState initialState, |
| 51 | + Func<TState, ResolvedEvent, TState> evolve, |
| 52 | + CancellationToken ct |
| 53 | + ) where TState : notnull => |
| 54 | + messages.ProjectState(initialState, evolve, null, ct); |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +public interface IStateCache<TState> { |
| 59 | + public ValueTask<TState> GetValueOrDefaultAsync(string key, TState defaultValue, CancellationToken ct = default); |
| 60 | + |
| 61 | + public ValueTask SetValueAsync(string key, TState state, CancellationToken ct = default); |
| 62 | +} |
| 63 | + |
| 64 | +public class DictionaryStateCache<TState> : IStateCache<TState> { |
| 65 | + readonly Dictionary<string, TState> _states = new Dictionary<string, TState>(); |
| 66 | + |
| 67 | + public ValueTask<TState> GetValueOrDefaultAsync(string key, TState defaultValue, CancellationToken ct = default) { |
| 68 | +#if NET48 |
| 69 | + var state = _states.TryGetValue(key, out TState? value) ? value : defaultValue; |
| 70 | +#else |
| 71 | + var state = _states.GetValueOrDefault(key, defaultValue); |
| 72 | +#endif |
| 73 | + return new ValueTask<TState>(state); |
| 74 | + } |
| 75 | + |
| 76 | + public ValueTask SetValueAsync(string key, TState state, CancellationToken ct = default) { |
| 77 | + _states[key] = state; |
| 78 | + |
| 79 | + return new ValueTask(); |
| 80 | + } |
| 81 | +} |
0 commit comments