Split IEventAggregator methods for dispatching into extension methods

This commit is contained in:
Antony Male 2014-05-26 18:45:45 +01:00
parent bfd9ac3f06
commit 2f4424a85d
1 changed files with 26 additions and 22 deletions

View File

@ -47,18 +47,6 @@ namespace Stylet
/// <param name="message">Event to publish</param> /// <param name="message">Event to publish</param>
/// <param name="dispatcher">Dispatcher to use to call each subscriber's handle method(s)</param> /// <param name="dispatcher">Dispatcher to use to call each subscriber's handle method(s)</param>
void PublishWithDispatcher(object message, Action<Action> dispatcher); void PublishWithDispatcher(object message, Action<Action> dispatcher);
/// <summary>
/// Publish an event to all subscribers, calling the handle methods on the UI thread
/// </summary>
/// <param name="message">Event to publish</param>
void PublishOnUIThread(object message);
/// <summary>
/// Publish an event to all subscribers, calling the handle methods synchronously on the current thread
/// </summary>
/// <param name="message">Event to publish</param>
void Publish(object message);
} }
public class EventAggregator : IEventAggregator public class EventAggregator : IEventAggregator
@ -101,16 +89,6 @@ namespace Stylet
} }
} }
public void PublishOnUIThread(object message)
{
this.PublishWithDispatcher(message, Execute.OnUIThread);
}
public void Publish(object message)
{
this.PublishWithDispatcher(message, x => x());
}
private class Handler private class Handler
{ {
private readonly WeakReference target; private readonly WeakReference target;
@ -171,4 +149,30 @@ namespace Stylet
} }
} }
} }
/// <summary>
/// Extension methods on IEventAggregator, to give more dispatching options
/// </summary>
public static class EventAggregatorExtensions
{
/// <summary>
/// Publish an event to all subscribers, calling the handle methods on the UI thread
/// </summary>
/// <param name="eventAggregator">EventAggregator to publish the message with</param>
/// <param name="message">Event to publish</param>
public static void PublishOnUIThread(this IEventAggregator eventAggregator, object message)
{
eventAggregator.PublishWithDispatcher(message, Execute.OnUIThread);
}
/// <summary>
/// Publish an event to all subscribers, calling the handle methods synchronously on the current thread
/// </summary>
/// <param name="eventAggregator">EventAggregator to publish the message with</param>
/// <param name="message">Event to publish</param>
public static void Publish(this IEventAggregator eventAggregator, object message)
{
eventAggregator.PublishWithDispatcher(message, a => a());
}
}
} }