Skip to content

Commit

Permalink
chore: use MulticastExceptionHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Aug 4, 2023
1 parent b4f2f46 commit a2538ea
Showing 1 changed file with 76 additions and 31 deletions.
107 changes: 76 additions & 31 deletions src/Prism.Core/Dialogs/DialogCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Prism.Common;

namespace Prism.Dialogs;

Expand All @@ -13,8 +14,8 @@ namespace Prism.Dialogs;
public readonly struct DialogCallback
{
private readonly bool _empty = false;
private readonly List<MulticastDelegate> _callbacks = new List<MulticastDelegate>();
private readonly List<MulticastDelegate> _errorCallbacks = new List<MulticastDelegate>();
private readonly List<MulticastDelegate> _callbacks = new ();
private readonly MulticastExceptionHandler _errorCallbacks = new ();

/// <summary>
/// Creates a new instance of a DialogCallback
Expand All @@ -24,7 +25,10 @@ public DialogCallback()
{
}

private DialogCallback(bool empty) => _empty = empty;
private DialogCallback(bool empty)
{
_empty = empty;
}

/// <summary>
/// Invokes the Delegates based on a specific Exception that was encountered.
Expand All @@ -43,26 +47,21 @@ public Task Invoke(Exception ex) =>
[EditorBrowsable(EditorBrowsableState.Never)]
public async Task Invoke(IDialogResult result)
{
if (_empty || (!_callbacks.Any() && !_errorCallbacks.Any()))
if (_empty)
{
return;

if(result.Exception is not null)
}
else if (result.Exception is not null && _errorCallbacks.CanHandle(result.Exception))
{
// process error callbacks
if (_errorCallbacks.Any())
{
foreach(MulticastDelegate errorCallback in _errorCallbacks)
{
await Process(errorCallback, result);
}
return;
}
await _errorCallbacks.HandleAsync(result.Exception, result);
return;
}


foreach(var callback in _callbacks)
else if(_callbacks.Any())
{
await Process(callback, result);
foreach(var callback in _callbacks)
{
await Process(callback, result);
}
}
}

Expand All @@ -72,14 +71,10 @@ private static async Task Process(MulticastDelegate @delegate, IDialogResult res
action();
else if (@delegate is Action<IDialogResult> actionResult)
actionResult(result);
else if (@delegate is Action<Exception> actionException && result.Exception is not null)
actionException(result.Exception);
else if (@delegate is Func<Task> func)
await func();
else if (@delegate is Func<IDialogResult, Task> funcResult)
await funcResult(result);
else if (@delegate is Func<Exception, Task> taskException && result.Exception is not null)
await taskException(result.Exception);
}

/// <summary>
Expand Down Expand Up @@ -117,7 +112,19 @@ public DialogCallback OnClose(Action<IDialogResult> action)
/// <returns></returns>
public DialogCallback OnError(Action action)
{
_errorCallbacks.Add(action);
_errorCallbacks.Register<Exception>(action);
return this;
}

/// <summary>
/// Provides a delegate callback method when an Exception is encountered
/// </summary>
/// <param name="action">The callback</param>
/// <returns></returns>
public DialogCallback OnError<TException>(Action action)
where TException : Exception
{
_errorCallbacks.Register<TException>(action);
return this;
}

Expand All @@ -128,7 +135,19 @@ public DialogCallback OnError(Action action)
/// <returns></returns>
public DialogCallback OnError(Action<Exception> action)
{
_errorCallbacks.Add(action);
_errorCallbacks.Register<Exception>(action);
return this;
}

/// <summary>
/// Provides a delegate callback method when an Exception is encountered
/// </summary>
/// <param name="action">The callback</param>
/// <returns></returns>
public DialogCallback OnError<TException>(Action<TException> action)
where TException : Exception
{
_errorCallbacks.Register<TException>(action);
return this;
}

Expand All @@ -137,9 +156,10 @@ public DialogCallback OnError(Action<Exception> action)
/// </summary>
/// <param name="action">The callback</param>
/// <returns></returns>
public DialogCallback OnError(Action<IDialogResult> action)
public DialogCallback OnError<TException>(Action<TException, IDialogResult> action)
where TException : Exception
{
_errorCallbacks.Add(action);
_errorCallbacks.Register<TException>(action);
return this;
}

Expand Down Expand Up @@ -172,7 +192,19 @@ public DialogCallback OnCloseAsync(Func<IDialogResult, Task> func)
/// <returns></returns>
public DialogCallback OnErrorAsync(Func<Task> func)
{
_errorCallbacks.Add(func);
_errorCallbacks.Register<Exception>(func);
return this;
}

/// <summary>
/// Provides an asynchronous delegate callback method when an Exception is encountered
/// </summary>
/// <param name="func">The callback</param>
/// <returns></returns>
public DialogCallback OnErrorAsync<TException>(Func<Task> func)
where TException : Exception
{
_errorCallbacks.Register<TException>(func);
return this;
}

Expand All @@ -183,7 +215,19 @@ public DialogCallback OnErrorAsync(Func<Task> func)
/// <returns></returns>
public DialogCallback OnErrorAsync(Func<Exception, Task> func)
{
_errorCallbacks.Add(func);
_errorCallbacks.Register<Exception>(func);
return this;
}

/// <summary>
/// Provides an asynchronous delegate callback method when an Exception is encountered
/// </summary>
/// <param name="func">The callback</param>
/// <returns></returns>
public DialogCallback OnErrorAsync<TException>(Func<TException, Task> func)
where TException : Exception
{
_errorCallbacks.Register<TException>(func);
return this;
}

Expand All @@ -192,9 +236,10 @@ public DialogCallback OnErrorAsync(Func<Exception, Task> func)
/// </summary>
/// <param name="func">The callback</param>
/// <returns></returns>
public DialogCallback OnErrorAsync(Func<IDialogResult, Task> func)
public DialogCallback OnErrorAsync<TException>(Func<TException, IDialogResult, Task> func)
where TException : Exception
{
_errorCallbacks.Add(func);
_errorCallbacks.Register<TException>(func);
return this;
}
}

0 comments on commit a2538ea

Please sign in to comment.