Skip to content

Commit

Permalink
Merge branch 'main' into v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Jun 20, 2024
2 parents 95068b7 + a0762f6 commit 8592965
Show file tree
Hide file tree
Showing 37 changed files with 473 additions and 198 deletions.
3 changes: 3 additions & 0 deletions Sample/Contracts/AutoRefreshRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Sample.Contracts;

public record AutoRefreshRequest : IStreamRequest<string>;
3 changes: 3 additions & 0 deletions Sample/Contracts/CachedRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Sample.Contracts;

public record CachedRequest : IRequest<string>;
7 changes: 6 additions & 1 deletion Sample/Contracts/MyMessageContracts.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using Shiny.Mediator.Middleware;

namespace Sample.Contracts;

public record MyMessageRequest(string Arg, bool FireAndForgetEvents) : IRequest<MyMessageResponse>;
public record MyMessageRequest(string Arg, bool FireAndForgetEvents) : IRequest<MyMessageResponse>, ICacheItem
{
public string CacheKey { get; }
};

public record MyMessageResponse(string Response);

Expand Down
3 changes: 3 additions & 0 deletions Sample/Contracts/ResilientRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Sample.Contracts;

public record ResilientRequest : IRequest<string>;
14 changes: 14 additions & 0 deletions Sample/Handlers/AutoRefreshStreamRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Runtime.CompilerServices;
using Sample.Contracts;

namespace Sample.Handlers;

[RegisterHandler]
public class AutoRefreshStreamRequestHandler : IStreamRequestHandler<AutoRefreshRequest, string>
{
[TimerRefresh(3000)]
public async IAsyncEnumerable<string> Handle(AutoRefreshRequest request, [EnumeratorCancellation] CancellationToken cancellationToken)
{
yield return DateTimeOffset.Now.ToString("h:mm:ss tt");
}
}
15 changes: 15 additions & 0 deletions Sample/Handlers/CachedRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Sample.Contracts;

namespace Sample.Handlers;


[RegisterHandler]
public class CachedRequestHandler : IRequestHandler<CachedRequest, string>
{
[Cache(MaxAgeSeconds = 20)]
public Task<string> Handle(CachedRequest request, CancellationToken cancellationToken)
{
var r = DateTimeOffset.UtcNow.ToLocalTime().ToString("h:mm:ss tt");
return Task.FromResult(r);
}
}
16 changes: 16 additions & 0 deletions Sample/Handlers/MyStreamRequestMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Sample.Handlers;

// [RegisterMiddleware]
public class MyStreamRequestMiddleware<TRequest, TResult>(ILogger<MyStreamRequestMiddleware<TRequest, TResult>> logger) : IStreamRequestMiddleware<TRequest, TResult> where TRequest : IStreamRequest<TResult>
{
public IAsyncEnumerable<TResult> Process(
TRequest request,
StreamRequestHandlerDelegate<TResult> next,
IStreamRequestHandler<TRequest, TResult> requestHandler,
CancellationToken cancellationToken
)
{
logger.LogInformation($"MyStreamRequestMiddleware called for {typeof(TRequest).FullName}");
return next();
}
}
23 changes: 23 additions & 0 deletions Sample/Handlers/ResilientRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Sample.Contracts;
using Shiny.Mediator.Resilience;

namespace Sample.Handlers;


[RegisterHandler]
public class ResilientRequestHandler : IRequestHandler<ResilientRequest, string>
{
static bool timeoutRequest;

[Resilient("test")]
public async Task<string> Handle(ResilientRequest request, CancellationToken cancellationToken)
{
if (timeoutRequest)
{
await Task.Delay(3000, cancellationToken);
}

timeoutRequest = !timeoutRequest;
return DateTimeOffset.UtcNow.ToString("f");
}
}
2 changes: 2 additions & 0 deletions Sample/Handlers/SingletonRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ namespace Sample.Handlers;


[RegisterHandler]
[TimedLogging(3000)]
public class SingletonRequestHandler(IMediator mediator, AppSqliteConnection data) : IRequestHandler<MyMessageRequest, MyMessageResponse>
{
[Cache(Storage = StoreType.File, MaxAgeSeconds = 30, OnlyForOffline = true)]
public async Task<MyMessageResponse> Handle(MyMessageRequest request, CancellationToken cancellationToken)
{
var e = new MyMessageEvent(
Expand Down
5 changes: 3 additions & 2 deletions Sample/Handlers/TickerStreamRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;
using Sample.Contracts;

namespace Sample.Handlers;
Expand All @@ -6,11 +7,11 @@ namespace Sample.Handlers;
[RegisterHandler]
public class TickerStreamRequestHandler : IStreamRequestHandler<TickerRequest, string>
{
public async IAsyncEnumerable<string> Handle(TickerRequest request, CancellationToken cancellationToken)
public async IAsyncEnumerable<string> Handle(TickerRequest request, [EnumeratorCancellation] CancellationToken cancellationToken)
{
for (var i = 0; i < request.Repeat; i++)
{
await Task.Delay(TimeSpan.FromSeconds(request.GapSeconds));
await Task.Delay(TimeSpan.FromSeconds(request.GapSeconds), cancellationToken);
var value = i * request.Multiplier;
yield return ($"{i} : {value}");
}
Expand Down
17 changes: 14 additions & 3 deletions Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Sample.Handlers;
using Polly;
using Polly.Retry;
using Sample.Handlers;
using Shiny.Mediator.Resilience;

namespace Sample;

Expand Down Expand Up @@ -40,17 +43,25 @@ public static MauiApp CreateMauiApp()
builder.Services.AddShinyMediator(x => x
.UseMaui()
.UseBlazor()
.AddTimerRefreshStreamMiddleware()
// .AddReplayStreamMiddleware()
.AddUserNotificationExceptionMiddleware(new UserExceptionRequestMiddlewareConfig
{
ErrorConfirm = "OK",
ErrorTitle = "OOOPS",
ErrorMessage = "You did something wrong",
ShowFullException = false
})
// .AddResiliencyMiddleware(
// ("Test", builder =>
// {
// // builder.AddRetry(new RetryStrategyOptions());
// builder.AddTimeout(TimeSpan.FromSeconds(2.0));
// })
// )
);
builder.Services.AddDiscoveredMediatorHandlersFromSample();
// builder.Services.AddSingletonAsImplementedInterfaces<ErrorRequestHandler>();


builder.Services.AddSingleton<AppSqliteConnection>();
builder.Services.AddMauiBlazorWebView();

Expand Down
1 change: 1 addition & 0 deletions Sample/Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
<ProjectReference Include="..\src\Shiny.Mediator.Blazor\Shiny.Mediator.Blazor.csproj" />
<ProjectReference Include="..\src\Shiny.Mediator.Contracts\Shiny.Mediator.Contracts.csproj"/>
<ProjectReference Include="..\src\Shiny.Mediator.Maui\Shiny.Mediator.Maui.csproj"/>
<ProjectReference Include="..\src\Shiny.Mediator.Resilience\Shiny.Mediator.Resilience.csproj" />
<ProjectReference Include="..\src\Shiny.Mediator\Shiny.Mediator.csproj"/>
<ProjectReference Include="..\src\Shiny.Mediator.SourceGenerators\Shiny.Mediator.SourceGenerators.csproj"
OutputItemType="Analyzer"
Expand Down
34 changes: 31 additions & 3 deletions Sample/TriggerPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,26 @@
Command="{Binding CancelCommand}"/>
</TableSection>

<TableSection Title="Precanned">
<TableSection Title="Other Request Samples">
<TextCell Text="Error Trap"
Command="{Binding ErrorTrap}" />
</TableSection>

<TableSection Title="Streaming">
<TableSection Title="Cache">
<TextCell Text="Clear" Command="{Binding CacheClear}" />
<TextCell Text="Request" Command="{Binding CacheRequest}" />
<TextCell Text="Value" Detail="{Binding CacheValue}" />
<TextCell Text="Cache has a 20s expiry time" />
</TableSection>

<!--
<TableSection Title="Resiliency">
<TextCell Text="Request" Command="{Binding ResilientCommand}" />
<TextCell Text="Value" Detail="{Binding ResilientValue}" />
</TableSection>
-->

<TableSection Title="STREAMING - Standard">
<EntryCell Label="Repeat"
Text="{Binding StreamRepeat}"
Keyboard="Numeric" />
Expand All @@ -41,12 +55,26 @@
Text="{Binding StreamGapSeconds}"
Keyboard="Numeric" />

<TextCell Text="Run Stream"
<TextCell Text="Run"
Command="{Binding Stream}"/>

<TextCell Text="Stop"
Command="{Binding CancelStream}"/>

<TextCell Text="Last Response"
Detail="{Binding StreamLastResponse}" />
</TableSection>

<TableSection Title="STREAMING - Timer Refresh">
<TextCell Text="Start" Command="{Binding RefreshTimerStart}" />
<TextCell Text="Stop" Command="{Binding CancelStream}" />
<TextCell Text="Last Value" Detail="{Binding LastRefreshTimerValue}" />
</TableSection>

<!--
<TableSection Title="STREAMING - Replay">
</TableSection>
-->
</TableRoot>
</TableView>
</ContentPage>
53 changes: 50 additions & 3 deletions Sample/TriggerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ AppSqliteConnection data
)
: base(services)
{
// TODO: request without response
this.TriggerCommand = ReactiveCommand.CreateFromTask(
async () =>
{
Expand All @@ -25,6 +24,7 @@ AppSqliteConnection data
this.FireAndForgetEvents
);
var result = await mediator.Request(request, this.cancelSource.Token);

await data.Log(
"TriggerViewModel-Response",
new MyMessageEvent(
Expand All @@ -44,7 +44,7 @@ await data.Log(
await data.Log(
"TriggerViewModel-Cancel",
new MyMessageEvent(
this.Arg,
this.Arg!,
this.FireAndForgetEvents
)
);
Expand All @@ -59,7 +59,10 @@ await data.Log(

this.Stream = ReactiveCommand.CreateFromTask(async () =>
{
var stream = mediator.Request(new TickerRequest(this.StreamRepeat, this.StreamMultiplier, this.StreamGapSeconds));
var stream = mediator.Request(
new TickerRequest(this.StreamRepeat, this.StreamMultiplier, this.StreamGapSeconds),
this.DeactivateToken
);
await foreach (var item in stream)
{
this.StreamLastResponse = item;
Expand All @@ -68,6 +71,38 @@ await data.Log(
this.sub = mediator.Subscribe((MyMessageEvent @event, CancellationToken _) =>
data.Log("TriggerViewModel-Subscribe", @event)
);

this.CacheClear = ReactiveCommand.CreateFromTask(async () =>
{
await mediator.Send(new FlushAllCacheRequest());
await this.Dialogs.Alert("Cache Cleared");
});

this.CancelStream = ReactiveCommand.CreateFromTask(async () =>
{
this.Deactivate();
await this.Dialogs.Alert("All streams cancelled");
});
this.CacheRequest = ReactiveCommand.CreateFromTask(async () =>
{
this.CacheValue = await mediator.Request(new CachedRequest());
});

this.ResilientCommand = ReactiveCommand.CreateFromTask(async () =>
{
this.ResilientValue = await mediator.Request(new ResilientRequest());
});

this.RefreshTimerStart = ReactiveCommand.CreateFromTask(async () =>
{
var en = mediator.Request(new AutoRefreshRequest(), this.DeactivateToken).GetAsyncEnumerator(this.DeactivateToken);
while (await en.MoveNextAsync())
{
await MainThread.InvokeOnMainThreadAsync(
() => this.LastRefreshTimerValue = en.Current
);
}
});
}


Expand All @@ -79,9 +114,21 @@ public Task Handle(MyMessageEvent @event, CancellationToken cancellationToken)
return Task.CompletedTask;
}


public ICommand CancelStream { get; }
public ICommand ErrorTrap { get; }
public ICommand TriggerCommand { get; }
public ICommand CancelCommand { get; }
public ICommand CacheRequest { get; }
public ICommand CacheClear { get; }
[Reactive] public string CacheValue { get; private set; }

[Reactive] public string LastRefreshTimerValue { get; private set; }
public ICommand RefreshTimerStart { get; }

public ICommand ResilientCommand { get; }
[Reactive] public string ResilientValue { get; private set; }

[Reactive] public string Arg { get; set; }
[Reactive] public bool FireAndForgetEvents { get; set; }

Expand Down
6 changes: 0 additions & 6 deletions ShinyMediator.sln
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shiny.Mediator.Maui", "src\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shiny.Mediator.Blazor", "src\Shiny.Mediator.Blazor\Shiny.Mediator.Blazor.csproj", "{11E8B489-B401-45A1-B904-6D0AF368A567}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shiny.Mediator.Mvvm", "src\Shiny.Mediator.Mvvm\Shiny.Mediator.Mvvm.csproj", "{3E966F2E-5A86-467C-845C-E1E6BA70653B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shiny.Mediator.Resilience", "src\Shiny.Mediator.Resilience\Shiny.Mediator.Resilience.csproj", "{1DDF878B-0EDF-4C32-813C-7995427D3387}"
EndProject
Global
Expand Down Expand Up @@ -66,10 +64,6 @@ Global
{11E8B489-B401-45A1-B904-6D0AF368A567}.Debug|Any CPU.Build.0 = Debug|Any CPU
{11E8B489-B401-45A1-B904-6D0AF368A567}.Release|Any CPU.ActiveCfg = Release|Any CPU
{11E8B489-B401-45A1-B904-6D0AF368A567}.Release|Any CPU.Build.0 = Release|Any CPU
{3E966F2E-5A86-467C-845C-E1E6BA70653B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3E966F2E-5A86-467C-845C-E1E6BA70653B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E966F2E-5A86-467C-845C-E1E6BA70653B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E966F2E-5A86-467C-845C-E1E6BA70653B}.Release|Any CPU.Build.0 = Release|Any CPU
{1DDF878B-0EDF-4C32-813C-7995427D3387}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1DDF878B-0EDF-4C32-813C-7995427D3387}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1DDF878B-0EDF-4C32-813C-7995427D3387}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
Loading

0 comments on commit 8592965

Please sign in to comment.