Skip to content

Commit

Permalink
Stream context now gets set
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Jan 23, 2025
1 parent 9edad71 commit d587a8c
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 19 deletions.
6 changes: 5 additions & 1 deletion samples/Sample.Api/Handlers/TheStreamHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ public record TestStreamRequest(int SecondsBetween) : IStreamRequest<string>;
[MediatorHttpPost("GetStream", "/stream")]
public class TheStreamHandler(ILogger<TheStreamHandler> logger) : IStreamRequestHandler<TestStreamRequest, string>
{
public async IAsyncEnumerable<string> Handle(TestStreamRequest request, [EnumeratorCancellation] CancellationToken cancellationToken)
public async IAsyncEnumerable<string> Handle(
TestStreamRequest request,
RequestContext<TestStreamRequest> context,
[EnumeratorCancellation] CancellationToken cancellationToken
)
{
while (!cancellationToken.IsCancellationRequested)
{
Expand Down
6 changes: 5 additions & 1 deletion samples/Sample/Handlers/AutoRefreshStreamRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ namespace Sample.Handlers;
public class AutoRefreshStreamRequestHandler : IStreamRequestHandler<AutoRefreshRequest, string>
{
[TimerRefresh(3000)]
public async IAsyncEnumerable<string> Handle(AutoRefreshRequest request, [EnumeratorCancellation] CancellationToken cancellationToken)
public async IAsyncEnumerable<string> Handle(
AutoRefreshRequest request,
RequestContext<AutoRefreshRequest> context,
[EnumeratorCancellation] CancellationToken cancellationToken
)
{
yield return DateTimeOffset.Now.ToString("h:mm:ss tt");
}
Expand Down
1 change: 0 additions & 1 deletion samples/Sample/Handlers/MyRequestMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace Sample.Handlers;


[SingletonMiddleware]
public class MyRequestMiddleware(AppSqliteConnection conn) : IRequestMiddleware<MyMessageRequest, MyMessageResponse>
{
public async Task<MyMessageResponse> Process(
Expand Down
6 changes: 5 additions & 1 deletion samples/Sample/Handlers/TickerStreamRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ namespace Sample.Handlers;
[SingletonHandler]
public class TickerStreamRequestHandler : IStreamRequestHandler<TickerRequest, string>
{
public async IAsyncEnumerable<string> Handle(TickerRequest request, [EnumeratorCancellation] CancellationToken cancellationToken)
public async IAsyncEnumerable<string> Handle(
TickerRequest request,
RequestContext<TickerRequest> context,
[EnumeratorCancellation] CancellationToken cancellationToken
)
{
for (var i = 0; i < request.Repeat; i++)
{
Expand Down
16 changes: 7 additions & 9 deletions samples/Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Configuration;
using Polly;
using Sample.Handlers;

namespace Sample;

Expand Down Expand Up @@ -39,21 +40,17 @@ public static MauiApp CreateMauiApp()
builder.Logging.AddDebug();
builder.Services.AddBlazorWebViewDeveloperTools();
#endif
builder.Configuration.AddJsonStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("Sample.appsettings.json")!);
builder.Configuration.AddJsonStream(
Assembly
.GetExecutingAssembly()
.GetManifestResourceStream("Sample.appsettings.json")!
);

builder.Services.AddShinyMediator(x => x
.UseMaui()
.UseBlazor()

.AddOfflineAvailabilityMiddleware()
.AddPersistentCache()
.AddUserErrorNotificationsMiddleware()
.AddPerformanceLoggingMiddleware()
.AddTimerRefreshStreamMiddleware()
.AddEventExceptionHandlingMiddleware()
.AddDataAnnotations()

.AddHttpClient()
.AddMauiHttpDecorator()
.AddPrismSupport()

Expand All @@ -64,6 +61,7 @@ public static MauiApp CreateMauiApp()
y.ExpirationScanFrequency = TimeSpan.FromSeconds(5);
})
);
builder.Services.AddSingletonAsImplementedInterfaces<MyRequestMiddleware>();
builder.Services.AddDiscoveredMediatorHandlersFromSample();

builder.Services.AddSingleton<AppSqliteConnection>();
Expand Down
15 changes: 12 additions & 3 deletions src/Shiny.Mediator.AppSupport/Middleware/ReplayStreamMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ CancellationToken cancellationToken

logger.LogDebug("ReplayStream Enabled - {Request}", context.Request);
return this.Iterate(
context.Request,
context.Request,
context,
next,
cancellationToken
);
Expand All @@ -55,9 +56,9 @@ protected bool IsEnabled(TRequest request, IRequestHandler requestHandler)
}


// TODO: add cache
protected virtual async IAsyncEnumerable<TResult> Iterate(
TRequest request,
RequestContext<TRequest> context,
StreamRequestHandlerDelegate<TResult> next,
[EnumeratorCancellation] CancellationToken ct
)
Expand All @@ -68,13 +69,21 @@ [EnumeratorCancellation] CancellationToken ct
{
var item = await cache.Get<TResult>(requestKey).ConfigureAwait(false);
if (item != null)
{
// TODO: need cache date
logger.LogDebug("ReplayStream Cache Hit - {Request}", context.Request);
context.Cache(new CacheContext(requestKey, true, DateTimeOffset.UtcNow));
yield return item.Value;
}
}
else if (offline != null)
{
var store = await offline.Get<TResult>(request);
if (store != null) // TODO: I need context here to ship out date
if (store != null)
{
context.Offline(new OfflineAvailableContext(requestKey, store.Timestamp));
yield return store.Value;
}
}

if (!internet.IsAvailable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ CancellationToken cancellationToken
logger.LogDebug("Cache Forced Refresh - {Request}", context.Request);
result = await next().ConfigureAwait(false);
if (result != null)
cacheService.Set(cacheKey, result, config);
await cacheService.Set(cacheKey, result, config).ConfigureAwait(false);
}
else
{
Expand Down
6 changes: 5 additions & 1 deletion src/Shiny.Mediator/IStreamRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ namespace Shiny.Mediator;

public interface IStreamRequestHandler<TRequest, TResult> : IRequestHandler where TRequest : IStreamRequest<TResult>
{
IAsyncEnumerable<TResult> Handle(TRequest request, CancellationToken cancellationToken);
IAsyncEnumerable<TResult> Handle(
TRequest request,
RequestContext<TRequest> context,
CancellationToken cancellationToken
);
}
2 changes: 1 addition & 1 deletion src/Shiny.Mediator/Infrastructure/Impl/Mediator_Streams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public RequestResult<IAsyncEnumerable<TResult>> Handle()
"Executing streaming request handler {RequestHandlerType}",
requestHandler.GetType().FullName
);
return requestHandler.Handle(request, cancellationToken);
return requestHandler.Handle(request, context, cancellationToken);
});

var middlewares = scope.GetServices<IStreamRequestMiddleware<TRequest, TResult>>();
Expand Down

0 comments on commit d587a8c

Please sign in to comment.