Skip to content

Commit

Permalink
Updates for mediator 3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Jan 29, 2025
1 parent 1f698ec commit 6803656
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 84 deletions.
3 changes: 1 addition & 2 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export default defineConfig({
{ label: 'Commands', link: 'mediator/commands' },
{ label: 'Streams', link: 'mediator/streams' },
{ label: 'Events', link: 'mediator/events' },
{ label: 'Exception Handling', link: 'mediator/exceptionhandlers' },
{ label: 'Request Keys', link: 'mediator/requestkeys' },
{ label: 'Execution Contexts', link: 'mediator/context' },
{ label: 'Advanced', link: 'mediator/advanced' },
Expand All @@ -167,9 +168,7 @@ export default defineConfig({
{ label: 'Resiliency', link: 'mediator/middleware/resilience' },
{ label: 'Offline', link: 'mediator/middleware/offline' },
{ label: 'Performance Logging', link: 'mediator/middleware/performancelogging' },
{ label: 'User Notification Exception Handling', link: 'mediator/middleware/usererrornotifications' },
{ label: 'Main Thread', link: 'mediator/middleware/mainthread' },
{ label: 'Event Exception Handling', link: 'mediator/middleware/eventexceptions' },
{ label: 'Replay', link: 'mediator/middleware/replay' },
{ label: 'Refresh Timer', link: 'mediator/middleware/refresh' },
{ label: 'Command Scheduling', link: 'mediator/middleware/scheduling' }
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"astro": "astro"
},
"dependencies": {
"@astrojs/react": "^4.1.6",
"astro": "^5.1.8",
"@astrojs/react": "^4.2.0",
"astro": "^5.1.10",
"astro-expressive-code": "^0.40.1",
"prismjs": "^1.29.0",
"react-tabs": "^6.1.0",
Expand Down
89 changes: 89 additions & 0 deletions src/content/docs/mediator/exceptionhandlers.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
title: Exception Handler
---
import { Steps } from '@astrojs/starlight/components';
import NugetBadge from '/src/components/NugetBadge.tsx';

## Exception Handling

Exception handling across all handler types is easier than ever with a exception handler. Global exception handling is nothing more than
middleware installing middleware for all handler types, but providing one interface to rule them. Best of all, you don't have to use 1 exception handler
to cover all cases. You can register multiple and just return true if your handler managed the exception.

```csharp
public class MyExceptionHandler : Shiny.Mediator.IExceptionHandler
{
public Task<bool> Handle(Exception ex, EventContext context)
{
// do something
return Task.FromResult(true);
}
}

services.AddShinyMediator(x => x.AddExceptionHandler<MyExceptionHandler>) // or scoped
```

:::warn
Global Exception Handling middleware is added automatically by the library
This middleware does not apply to [Streams](./streams) at this time
:::


## Prevent Event Exceptions

Events can throw exceptions. You can trap them at a global level by awaiting the Publish call. However, there may be cases
where you may want other events to complete and not be blocked by one misbehaving piece of code. Thus, we have middleware to help
deal with this in a very simple/straight forward manor.

This middleware does nothing more than log an error and `moves on`. This is applied to all event handlers and does not require any attributes
or configuration like other middleware

:::note
This middleware is installed by default with Shiny.Mediator
:::

## User Notification Exception Handling
:::note
User notification middleware is applied to [Commands](./commands) and [Requests](./requests)
:::

User notification exception handling is great for scenarios where the user initiates an action, but an issue occurs
such as the network failing, data issues, etc. This middleware does the standard try/execute/catch/log & notify user
that something went wrong

:::note
This middleware is installed by default with Maui & Blazor
:::

## Usage

To use it on with your request handlers, simply configured the `UserErrorNotifications` section in your `appsettings.json` file:

```json
{
"Mediator": {
"UserErrorNotifications": {
"My.Contracts.*": {
"*": {
"Title": "ERROR",
"Message" : "Failed to do something"
},
"fr-CA": {
"Title": "ERREUR",
"Message" : "Échec de faire quelque chose"
}
}
}
}
}
```

:::danger
Configuration does allow you to specify wildcard/globs `*` to specify all handlers in a namespace use user error notifications.
WE STRONGLY DISCOURAGE THIS. This feature is something you want to be very explicit about.
:::


:::note
The `*` is a wildcard for the default/fallback values. You can also specify a specific handler or language
:::
46 changes: 42 additions & 4 deletions src/content/docs/mediator/extensions/maui.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,46 @@
title: MAUI
---

TODO
## Setup

* Event Collector
* Prism & Shell Navigation
* Middleware Support
The easiest way to get going with Shiny Mediator in your .NET MAUI app is to use the `UseMaui` extension method. This will install all the necessary services.

```csharp
// MauiProgram.cs
builder.Services.AddShinyMediator(cfg => cfg.UseMaui());

```

## Event Collector
The MAUI event collector is installed by default by `UseMaui`. Event collectors allow event handlers that may not be registered with dependency injection/Mediator
to still participate in `IMediator.Publish` calls. The MAUI event collector will navigate the navigation stack, any page or viewmodel on that page that implements
the event handler type for the event contract can now participate in the subscription.


## Middleware Support
The Mediator MAUI extension comes with a lot of preinstalled default middleware (that you can opt out of). If you choose to opt-out of this middleware being installed,
simply pass false to the first argument in `UseMaui`

* [Main Thread](../middleware/mainthread)
* [User Notification Errors](../exceptionhandlers)
* [Offline](../middleware/offline)
* [Replay Stream](../middleware/replay)

## Connectivity Broadcaster
Connectivity is used on almost every page or viewmodel in an app. You almost always have to inject the .NET MAUI IConnectivity service and hook the StateChanged event
to do things like disabling buttons, showing errors, or putting up an banner to let you users know why your app isn't showing fresh data. That's a lot of extra
code, cleanup, hooks, etc. This is where connectivity broadcaster comes in

```csharp
builder.Services.AddShinyMediator(cfg => cfg.AddConnectivityBroadcaster());

// or Page
public class MyViewModel : Shiny.Mediator.IEventHandler<Shiny.Mediator.ConnectivityChanged>
{
public async Task Handle(ConnectivityChanged @event, EventContext<ConnectivityChanged> context, CancellationToken ct)
{
// that's it
SomeString = @event.Connected ? "Connected" : "Disconnected";
}
}
```
18 changes: 0 additions & 18 deletions src/content/docs/mediator/middleware/eventexceptions.mdx

This file was deleted.

50 changes: 0 additions & 50 deletions src/content/docs/mediator/middleware/usererrornotifications.mdx

This file was deleted.

0 comments on commit 6803656

Please sign in to comment.