Skip to content

Commit

Permalink
#496 Remove EventAggregator.HandlerResultProcessing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nigel Sampson committed Jan 8, 2018
1 parent b0d788e commit e91bfde
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 173 deletions.
5 changes: 0 additions & 5 deletions src/Caliburn.Micro.Core/EventAggregator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ namespace Caliburn.Micro
/// <inheritdoc />
public class EventAggregator : IEventAggregator
{
/// <summary>
/// Processing of handler results on publication thread.
/// </summary>
public static Action<object, object> HandlerResultProcessing = (target, result) => { };

private readonly List<Handler> _handlers = new List<Handler>();

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Android.App;
using Android.Runtime;

Expand Down Expand Up @@ -45,25 +44,6 @@ protected virtual void StartDesignTime()
/// </summary>B
protected virtual void StartRuntime()
{
EventAggregator.HandlerResultProcessing = (target, result) =>
{
var task = result as Task;
if (task != null)
{
result = new IResult[] { task.AsResult() };
}
var coroutine = result as IEnumerable<IResult>;
if (coroutine != null)
{
var viewAware = target as IViewAware;
var view = viewAware != null ? viewAware.GetView() : null;
var context = new CoroutineExecutionContext { Target = target, View = view };
Coroutine.BeginExecute(coroutine.GetEnumerator(), context);
}
};

AssemblySourceCache.Install();
AssemblySource.Instance.AddRange(SelectAssemblies());

Expand Down Expand Up @@ -152,4 +132,4 @@ protected virtual void BuildUp(object instance)
{
}
}
}
}
117 changes: 64 additions & 53 deletions src/Caliburn.Micro.Platform/Platforms/UWP/CaliburnApplication.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
namespace Caliburn.Micro {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Windows.ApplicationModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Windows.ApplicationModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Caliburn.Micro
{
/// <summary>
/// Encapsulates the app and its available services.
/// </summary>
public abstract class CaliburnApplication : Application {
public abstract class CaliburnApplication : Application
{
private bool isInitialized;

/// <summary>
Expand All @@ -21,7 +23,8 @@ public abstract class CaliburnApplication : Application {
/// <summary>
/// Called by the bootstrapper's constructor at design time to start the framework.
/// </summary>
protected virtual void StartDesignTime() {
protected virtual void StartDesignTime()
{
AssemblySource.Instance.Clear();
AssemblySource.Instance.AddRange(SelectAssemblies());

Expand All @@ -34,23 +37,8 @@ protected virtual void StartDesignTime() {
/// <summary>
/// Called by the bootstrapper's constructor at runtime to start the framework.
/// </summary>
protected virtual void StartRuntime() {
EventAggregator.HandlerResultProcessing = (target, result) => {
var task = result as System.Threading.Tasks.Task;
if (task != null) {
result = new IResult[] {task.AsResult()};
}
var coroutine = result as IEnumerable<IResult>;
if (coroutine != null) {
var viewAware = target as IViewAware;
var view = viewAware != null ? viewAware.GetView() : null;
var context = new CoroutineExecutionContext {Target = target, View = view};
Coroutine.BeginExecute(coroutine.GetEnumerator(), context);
}
};

protected virtual void StartRuntime()
{
AssemblySourceCache.Install();
AssemblySource.Instance.AddRange(SelectAssemblies());

Expand All @@ -65,8 +53,10 @@ protected virtual void StartRuntime() {
/// <summary>
/// Start the framework.
/// </summary>
protected void Initialize() {
if (isInitialized) {
protected void Initialize()
{
if (isInitialized)
{
return;
}

Expand All @@ -88,17 +78,21 @@ protected void Initialize() {
AssemblySource.Instance.Refresh();


if (Execute.InDesignMode) {
try {
if (Execute.InDesignMode)
{
try
{
StartDesignTime();
}
catch {
catch
{
//if something fails at design-time, there's really nothing we can do...
isInitialized = false;
throw;
}
}
else {
else
{
StartRuntime();
}
}
Expand All @@ -107,7 +101,8 @@ protected void Initialize() {
/// Invoked when the application creates a window.
/// </summary>
/// <param name="args">Event data for the event.</param>
protected override void OnWindowCreated(WindowCreatedEventArgs args) {
protected override void OnWindowCreated(WindowCreatedEventArgs args)
{
base.OnWindowCreated(args);

// Because dispatchers are tied to windows Execute will fail in
Expand All @@ -120,7 +115,8 @@ protected override void OnWindowCreated(WindowCreatedEventArgs args) {
/// <summary>
/// Provides an opportunity to hook into the application object.
/// </summary>
protected virtual void PrepareApplication() {
protected virtual void PrepareApplication()
{
Resuming += OnResuming;
Suspending += OnSuspending;
UnhandledException += OnUnhandledException;
Expand All @@ -129,14 +125,16 @@ protected virtual void PrepareApplication() {
/// <summary>
/// Override to configure the framework and setup your IoC container.
/// </summary>
protected virtual void Configure() {
protected virtual void Configure()
{
}

/// <summary>
/// Override to tell the framework where to find assemblies to inspect for views, etc.
/// </summary>
/// <returns>A list of assemblies to inspect.</returns>
protected virtual IEnumerable<Assembly> SelectAssemblies() {
protected virtual IEnumerable<Assembly> SelectAssemblies()
{
return new[] {GetType().GetTypeInfo().Assembly};
}

Expand All @@ -146,7 +144,8 @@ protected virtual IEnumerable<Assembly> SelectAssemblies() {
/// <param name="service">The service to locate.</param>
/// <param name="key">The key to locate.</param>
/// <returns>The located service.</returns>
protected virtual object GetInstance(Type service, string key) {
protected virtual object GetInstance(Type service, string key)
{
return System.Activator.CreateInstance(service);
}

Expand All @@ -155,53 +154,60 @@ protected virtual object GetInstance(Type service, string key) {
/// </summary>
/// <param name="service">The service to locate.</param>
/// <returns>The located services.</returns>
protected virtual IEnumerable<object> GetAllInstances(Type service) {
protected virtual IEnumerable<object> GetAllInstances(Type service)
{
return new[] {System.Activator.CreateInstance(service)};
}

/// <summary>
/// Override this to provide an IoC specific implementation.
/// </summary>
/// <param name="instance">The instance to perform injection on.</param>
protected virtual void BuildUp(object instance) {
protected virtual void BuildUp(object instance)
{
}

/// <summary>
/// Override this to add custom behavior when the application transitions from Suspended state to Running state.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The event args.</param>
protected virtual void OnResuming(object sender, object e) {
protected virtual void OnResuming(object sender, object e)
{
}

/// <summary>
/// Override this to add custom behavior when the application transitions to Suspended state from some other state.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The event args.</param>
protected virtual void OnSuspending(object sender, SuspendingEventArgs e) {
protected virtual void OnSuspending(object sender, SuspendingEventArgs e)
{
}

/// <summary>
/// Override this to add custom behavior for unhandled exceptions.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The event args.</param>
protected virtual void OnUnhandledException(object sender, UnhandledExceptionEventArgs e) {
protected virtual void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
}

/// <summary>
/// Creates the root frame used by the application.
/// </summary>
/// <returns>The frame.</returns>
protected virtual Frame CreateApplicationFrame() {
protected virtual Frame CreateApplicationFrame()
{
return new Frame();
}

/// <summary>
/// Allows you to trigger the creation of the RootFrame from Configure if necessary.
/// </summary>
protected virtual void PrepareViewFirst() {
protected virtual void PrepareViewFirst()
{
if (RootFrame != null)
return;

Expand All @@ -213,15 +219,17 @@ protected virtual void PrepareViewFirst() {
/// Override this to register a navigation service.
/// </summary>
/// <param name="rootFrame">The root frame of the application.</param>
protected virtual void PrepareViewFirst(Frame rootFrame) {
protected virtual void PrepareViewFirst(Frame rootFrame)
{
}

/// <summary>
/// Creates the root frame and navigates to the specified view.
/// </summary>
/// <param name="viewType">The view type to navigate to.</param>
/// <param name="paramter">The object parameter to pass to the target.</param>
protected void DisplayRootView(Type viewType, object paramter = null) {
protected void DisplayRootView(Type viewType, object paramter = null)
{
Initialize();

PrepareViewFirst();
Expand All @@ -240,15 +248,17 @@ protected void DisplayRootView(Type viewType, object paramter = null) {
/// </summary>
/// <typeparam name="T">The view type to navigate to.</typeparam>
/// <param name="parameter">The object parameter to pass to the target.</param>
protected void DisplayRootView<T>(object parameter = null) {
DisplayRootView(typeof (T), parameter);
protected void DisplayRootView<T>(object parameter = null)
{
DisplayRootView(typeof(T), parameter);
}

/// <summary>
/// Locates the view model, locates the associate view, binds them and shows it as the root view.
/// </summary>
/// <param name="viewModelType">The view model type.</param>
protected void DisplayRootViewFor(Type viewModelType) {
protected void DisplayRootViewFor(Type viewModelType)
{
Initialize();

var viewModel = IoC.GetInstance(viewModelType, null);
Expand All @@ -268,8 +278,9 @@ protected void DisplayRootViewFor(Type viewModelType) {
/// Locates the view model, locates the associate view, binds them and shows it as the root view.
/// </summary>
/// <typeparam name="T">The view model type.</typeparam>
protected void DisplayRootViewFor<T>() {
DisplayRootViewFor(typeof (T));
protected void DisplayRootViewFor<T>()
{
DisplayRootViewFor(typeof(T));
}
}
}
Loading

0 comments on commit e91bfde

Please sign in to comment.