-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
146 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Shiny.Mediator.Middleware; | ||
|
||
namespace Shiny.Mediator; | ||
|
||
public static class MauiMediatorExtensions | ||
{ | ||
public static ShinyConfigurator UseMaui(this ShinyConfigurator cfg) => cfg.AddEventCollector<MauiEventCollector>(); | ||
|
||
public static ShinyConfigurator AddTimedMiddleware(this ShinyConfigurator cfg, TimedLoggingMiddlewareConfig config) | ||
{ | ||
cfg.Services.AddSingleton(config); | ||
return cfg.AddOpenRequestMiddleware(typeof(TimedLoggingRequestMiddleware<,>)); | ||
} | ||
|
||
|
||
|
||
public static ShinyConfigurator AddConnectivityCacheMiddleware(this ShinyConfigurator cfg) | ||
{ | ||
cfg.AddOpenRequestMiddleware(typeof(ConnectivityCacheRequestMiddleware<,>)); | ||
return cfg; | ||
} | ||
|
||
|
||
public static ShinyConfigurator AddUserNotificationExceptionMiddleware(this ShinyConfigurator cfg, UserExceptionRequestMiddlewareConfig config) | ||
{ | ||
cfg.Services.AddSingleton(config); | ||
cfg.AddOpenRequestMiddleware(typeof(UserExceptionRequestMiddleware<,>)); | ||
return cfg; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Shiny.Mediator.Maui/Middleware/ConnectivityCacheRequestMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Reflection; | ||
using System.Text.Json; | ||
|
||
namespace Shiny.Mediator.Middleware; | ||
|
||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = true, AllowMultiple = false)] | ||
public class CacheAttribute : Attribute | ||
{ | ||
// TODO: duration? configuration? | ||
} | ||
|
||
|
||
public class ConnectivityCacheRequestMiddleware<TRequest, TResult>(IConnectivity connectivity, IFileSystem fileSystem) : IRequestMiddleware<TRequest, TResult> where TRequest : IRequest<TResult> | ||
{ | ||
public async Task<TResult> Process(TRequest request, Func<Task<TResult>> next, CancellationToken cancellationToken) | ||
{ | ||
var config = typeof(TRequest).GetCustomAttribute<CacheAttribute>(); | ||
if (config == null) | ||
return await next().ConfigureAwait(false); | ||
|
||
var result = default(TResult); | ||
var storePath = Path.Combine(fileSystem.CacheDirectory, $"{typeof(TRequest).Name}.cache"); | ||
|
||
if (connectivity.NetworkAccess != NetworkAccess.Internet) | ||
{ | ||
result = JsonSerializer.Deserialize<TResult>(storePath); | ||
} | ||
else | ||
{ | ||
result = await next().ConfigureAwait(false); | ||
var json = JsonSerializer.Serialize(result); | ||
await File.WriteAllTextAsync(storePath, json, cancellationToken).ConfigureAwait(false); | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Diagnostics; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Shiny.Mediator.Middleware; | ||
|
||
|
||
public class TimedLoggingMiddlewareConfig | ||
{ | ||
public bool LogAll { get; set; } | ||
public TimeSpan? ErrorThreshold { get; set; } | ||
} | ||
|
||
public class TimedLoggingRequestMiddleware<TRequest, TResult>(ILogger<TRequest> logger, TimedLoggingMiddlewareConfig config) : IRequestMiddleware<TRequest, TResult> where TRequest : IRequest<TResult> | ||
{ | ||
public async Task<TResult> Process(TRequest request, Func<Task<TResult>> next, CancellationToken cancellationToken) | ||
{ | ||
var sw = new Stopwatch(); | ||
sw.Start(); | ||
var result = await next(); | ||
sw.Stop(); | ||
|
||
var errored = config.ErrorThreshold != null && config.ErrorThreshold < sw.Elapsed; | ||
var msg = $"{typeof(TRequest)} pipeline execution took ${sw.Elapsed}"; | ||
|
||
if (!errored && config.LogAll) | ||
{ | ||
logger.LogDebug(msg); | ||
} | ||
if (errored) | ||
{ | ||
logger.LogError(msg); | ||
} | ||
return result; | ||
} | ||
} | ||
|
39 changes: 39 additions & 0 deletions
39
src/Shiny.Mediator.Maui/Middleware/UserNotificationExceptionRequestMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Shiny.Mediator.Middleware; | ||
|
||
|
||
public class UserExceptionRequestMiddlewareConfig | ||
{ | ||
public bool ShowFullException { get; set; } | ||
public string ErrorMessage { get; set; } = "We're sorry. An error has occurred"; | ||
public string ErrorTitle { get; set; } = "Error"; | ||
public string ErrorConfirm { get; set; } = "OK"; | ||
} | ||
|
||
public class UserExceptionRequestMiddleware<TRequest, TResult>(ILogger<TRequest> logger, UserExceptionRequestMiddlewareConfig config) : IRequestMiddleware<TRequest, TResult> where TRequest : IRequest<TResult> | ||
{ | ||
public async Task<TResult> Process(TRequest request, Func<Task<TResult>> next, CancellationToken cancellationToken) | ||
{ | ||
var result = default(TResult); | ||
try | ||
{ | ||
result = await next().ConfigureAwait(false); | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.LogError(ex, $"Error executing pipeline for {typeof(TRequest).FullName}"); | ||
try | ||
{ | ||
var msg = config.ShowFullException ? ex.ToString() : config.ErrorMessage; | ||
await Application.Current!.MainPage!.DisplayAlert(config.ErrorTitle, msg, config.ErrorConfirm); | ||
} | ||
catch | ||
{ | ||
// throw away | ||
} | ||
} | ||
|
||
return result!; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 0 additions & 18 deletions
18
src/Shiny.Mediator/Middleware/ExceptionHandlerMiddleware.cs
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters