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 17, 2024
2 parents 877dd99 + 61be54f commit 95068b7
Show file tree
Hide file tree
Showing 47 changed files with 709 additions and 329 deletions.
1 change: 1 addition & 0 deletions Build.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"src\\Shiny.Mediator.Contracts\\Shiny.Mediator.Contracts.csproj",
"src\\Shiny.Mediator.Blazor\\Shiny.Mediator.Blazor.csproj",
"src\\Shiny.Mediator.Maui\\Shiny.Mediator.Maui.csproj",
"src\\Shiny.Mediator.Resilience\\Shiny.Mediator.Resilience.csproj",
"src\\Shiny.Mediator.SourceGenerators\\Shiny.Mediator.SourceGenerators.csproj"
]
}
Expand Down
1 change: 1 addition & 0 deletions Sample/AnotherViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Sample.Contracts;
using Shiny.Mediator.Middleware;

namespace Sample;
Expand Down
4 changes: 2 additions & 2 deletions Sample/Components/Pages/Home.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@page "/"
@using Sample.Contracts
@implements IEventHandler<MyMessageEvent>
@inject AppSqliteConnection conn
@implements IEventHandler<MyMessageEvent>


<h1>Hello, world!</h1>

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

public record ErrorRequest : IRequest;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Sample;
namespace Sample.Contracts;

public record MyMessageRequest(string Arg, bool FireAndForgetEvents) : IRequest<MyMessageResponse>;

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

public record TickerRequest(int Repeat, int Multiplier, int GapSeconds) : IStreamRequest<string>;
13 changes: 13 additions & 0 deletions Sample/Handlers/ErrorRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Sample.Contracts;

namespace Sample.Handlers;


[RegisterHandler]
public class ErrorRequestHandler : IRequestHandler<ErrorRequest>
{
public Task Handle(ErrorRequest request, CancellationToken cancellationToken)
{
throw new InvalidOperationException("Why you call me?");
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Diagnostics;
using Sample.Contracts;

namespace Sample;
namespace Sample.Handlers;


[RegisterMiddleware]
public class MyRequestMiddleware(AppSqliteConnection conn) : IRequestMiddleware<MyMessageRequest, MyMessageResponse>
{
public async Task<MyMessageResponse> Process(MyMessageRequest request, RequestHandlerDelegate<MyMessageResponse> next, IRequestHandler<MyMessageRequest, MyMessageResponse> requestHandler, CancellationToken cancellationToken)
public async Task<MyMessageResponse> Process(MyMessageRequest request, RequestHandlerDelegate<MyMessageResponse> next, IRequestHandler requestHandler, CancellationToken cancellationToken)
{
var sw = Stopwatch.StartNew();
var result = await next().ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Sample;
using Sample.Contracts;

namespace Sample.Handlers;


[RegisterHandler]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Sample;
using Sample.Contracts;

namespace Sample.Handlers;


[RegisterHandler]
Expand Down
18 changes: 18 additions & 0 deletions Sample/Handlers/TickerStreamRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Sample.Contracts;

namespace Sample.Handlers;


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

namespace Sample;


public static class MauiProgram
Expand Down Expand Up @@ -28,12 +30,7 @@ public static MauiApp CreateMauiApp()
)
),
new(ErrorAlertType.FullError)
)
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
);

#if DEBUG
builder.Logging.SetMinimumLevel(LogLevel.Trace);
Expand All @@ -43,8 +40,16 @@ public static MauiApp CreateMauiApp()
builder.Services.AddShinyMediator(x => x
.UseMaui()
.UseBlazor()
.AddUserNotificationExceptionMiddleware(new UserExceptionRequestMiddlewareConfig
{
ErrorConfirm = "OK",
ErrorTitle = "OOOPS",
ErrorMessage = "You did something wrong",
ShowFullException = false
})
);
builder.Services.AddDiscoveredMediatorHandlersFromSample();
// builder.Services.AddSingletonAsImplementedInterfaces<ErrorRequestHandler>();

builder.Services.AddSingleton<AppSqliteConnection>();
builder.Services.AddMauiBlazorWebView();
Expand Down
1 change: 1 addition & 0 deletions Sample/Services/AppSqliteConnection.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Sample.Contracts;
using SQLite;

namespace Sample.Services;
Expand Down
25 changes: 25 additions & 0 deletions Sample/TriggerPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,31 @@
<TextCell Text="Cancel Request"
Command="{Binding CancelCommand}"/>
</TableSection>

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

<TableSection Title="Streaming">
<EntryCell Label="Repeat"
Text="{Binding StreamRepeat}"
Keyboard="Numeric" />

<EntryCell Label="Multiplier"
Text="{Binding StreamMultiplier}"
Keyboard="Numeric" />

<EntryCell Label="Gap Seconds"
Text="{Binding StreamGapSeconds}"
Keyboard="Numeric" />

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

<TextCell Text="Last Response"
Detail="{Binding StreamLastResponse}" />
</TableSection>
</TableRoot>
</TableView>
</ContentPage>
21 changes: 20 additions & 1 deletion Sample/TriggerViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Sample.Contracts;

namespace Sample;


Expand Down Expand Up @@ -52,7 +54,17 @@ await data.Log(
busy => busy.GetValue()
)
);


this.ErrorTrap = ReactiveCommand.CreateFromTask(() => mediator.Send(new ErrorRequest()));

this.Stream = ReactiveCommand.CreateFromTask(async () =>
{
var stream = mediator.Request(new TickerRequest(this.StreamRepeat, this.StreamMultiplier, this.StreamGapSeconds));
await foreach (var item in stream)
{
this.StreamLastResponse = item;
}
});
this.sub = mediator.Subscribe((MyMessageEvent @event, CancellationToken _) =>
data.Log("TriggerViewModel-Subscribe", @event)
);
Expand All @@ -67,11 +79,18 @@ public Task Handle(MyMessageEvent @event, CancellationToken cancellationToken)
return Task.CompletedTask;
}

public ICommand ErrorTrap { get; }
public ICommand TriggerCommand { get; }
public ICommand CancelCommand { get; }
[Reactive] public string Arg { get; set; }
[Reactive] public bool FireAndForgetEvents { get; set; }

public ICommand Stream { get; }
[Reactive] public int StreamGapSeconds { get; set; } = 1;
[Reactive] public int StreamRepeat { get; set; } = 5;
[Reactive] public int StreamMultiplier { get; set; } = 2;
[Reactive] public string? StreamLastResponse { get; private set; }

public override void Destroy()
{
base.Destroy();
Expand Down
12 changes: 12 additions & 0 deletions ShinyMediator.sln
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ 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
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -62,5 +66,13 @@ 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
{1DDF878B-0EDF-4C32-813C-7995427D3387}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
4 changes: 2 additions & 2 deletions src/Directory.build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
<EnableDefaultCompileItems>true</EnableDefaultCompileItems>
<Description>Shiny Mediator - a mediator pattern but for apps</Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/shinyorg/mediator</PackageProjectUrl>
<PackageProjectUrl>https://shinylib.net/client/mediator</PackageProjectUrl>
<PackageIcon>icon.png</PackageIcon>
<PackageReadmeFile>readme.md</PackageReadmeFile>
<PackageReleaseNotes>https://github.com/shinyorg/mediator</PackageReleaseNotes>
<PackageReleaseNotes>https://shinylib.net/release-notes/mediator/v10/</PackageReleaseNotes>
<PackageTags>mediator maui blazor</PackageTags>
<RepositoryUrl>https://github.com/shinyorg/mediator</RepositoryUrl>
<RepositoryType>git</RepositoryType>
Expand Down
10 changes: 3 additions & 7 deletions src/Shiny.Mediator.Contracts/IRequest.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
namespace Shiny.Mediator;

public interface IRequest : IRequest<Unit>
{
}


public interface IRequest<out TResponse>
{
}
public interface IRequest;
public interface IRequest<out TResult>;
public interface IStreamRequest<out TResult>;
38 changes: 5 additions & 33 deletions src/Shiny.Mediator.Maui/MauiMediatorExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Reflection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Shiny.Mediator.Maui;
using Shiny.Mediator.Maui.Services;
using Shiny.Mediator.Middleware;

namespace Shiny.Mediator;
Expand All @@ -17,38 +18,6 @@ public void Initialize(IServiceProvider services)

public static class MauiMediatorExtensions
{
internal static TAttribute? GetHandlerHandleMethodAttribute<TRequest, TResult, TAttribute>(this IRequestHandler<TRequest, TResult> handler)
where TRequest : IRequest<TResult>
where TAttribute : Attribute
=> handler
.GetType()
.GetMethod(
"Handle",
BindingFlags.Public | BindingFlags.Instance,
null,
CallingConventions.Any,
[ typeof(TRequest), typeof(CancellationToken) ],
null
)!
.GetCustomAttribute<TAttribute>();


internal static TAttribute? GetHandlerHandleMethodAttribute<TEvent, TAttribute>(this IEventHandler<TEvent> handler)
where TEvent : IEvent
where TAttribute : Attribute
=> handler
.GetType()
.GetMethod(
"Handle",
BindingFlags.Public | BindingFlags.Instance,
null,
CallingConventions.Any,
[ typeof(TEvent), typeof(CancellationToken) ],
null
)!
.GetCustomAttribute<TAttribute>();


public static ShinyConfigurator UseMaui(this ShinyConfigurator cfg, bool includeStandardMiddleware = true)
{
cfg.AddEventCollector<MauiEventCollector>();
Expand All @@ -63,6 +32,7 @@ public static ShinyConfigurator UseMaui(this ShinyConfigurator cfg, bool include
return cfg;
}


public static ShinyConfigurator AddTimedMiddleware(this ShinyConfigurator cfg)
=> cfg.AddOpenRequestMiddleware(typeof(TimedLoggingRequestMiddleware<,>));

Expand All @@ -83,6 +53,8 @@ public static ShinyConfigurator AddMainThreadEventMiddleware(this ShinyConfigura

public static ShinyConfigurator AddCacheMiddleware(this ShinyConfigurator cfg)
{
cfg.Services.TryAddSingleton<CacheManager>();
cfg.Services.AddSingletonAsImplementedInterfaces<CacheHandlers>();
cfg.AddOpenRequestMiddleware(typeof(CacheRequestMiddleware<,>));
return cfg;
}
Expand Down
18 changes: 18 additions & 0 deletions src/Shiny.Mediator.Maui/Middleware/CacheHandlers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Shiny.Mediator.Maui.Services;

namespace Shiny.Mediator.Middleware;

public class CacheHandlers(CacheManager cacheManager) : IRequestHandler<FlushAllCacheRequest>, IRequestHandler<FlushCacheItemRequest>
{
public Task Handle(FlushAllCacheRequest request, CancellationToken cancellationToken)
{
cacheManager.FlushAllCache();
return Task.CompletedTask;
}

public Task Handle(FlushCacheItemRequest request, CancellationToken cancellationToken)
{
cacheManager.RemoveCacheItem(request.Request);
return Task.CompletedTask;
}
}
Loading

0 comments on commit 95068b7

Please sign in to comment.