Skip to content

Commit

Permalink
feat: Moving navigation to background thread
Browse files Browse the repository at this point in the history
  • Loading branch information
nickrandolph committed Oct 26, 2022
1 parent 4cca53f commit 6de6477
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/Uno.Extensions.Core/IDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ public interface IDispatcher
/// <param name="cancellation">An cancellation token to cancel the async operation.</param>
/// <returns>A ValueTask to asynchronously get the result of the operation.</returns>
ValueTask<TResult> ExecuteAsync<TResult>(AsyncFunc<TResult> func, CancellationToken cancellation);

/// <summary>
/// Gets a value that specifies whether the current execution context is on the UI thread.
/// </summary>
bool HasThreadAccess { get; }
}
11 changes: 10 additions & 1 deletion src/Uno.Extensions.Navigation.UI/Dispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,14 @@ public Dispatcher(FrameworkElement element)

/// <inheritdoc />
public async ValueTask<TResult> ExecuteAsync<TResult>(AsyncFunc<TResult> func, CancellationToken cancellation)
=> await _dispatcher.ExecuteAsync(func, cancellation);
{
if (HasThreadAccess)
{
return await func(cancellation);
}
return await _dispatcher.ExecuteAsync(func, cancellation);
}

/// <inheritdoc />
public bool HasThreadAccess => _dispatcher.HasThreadAccess;
}
8 changes: 7 additions & 1 deletion src/Uno.Extensions.Navigation.UI/Navigator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,17 @@ protected Navigator(
var regionUpdateId = RouteUpdater?.StartNavigation(Region) ?? Guid.Empty;
try
{

if (Dispatcher.HasThreadAccess)
{
if (Logger.IsEnabled(LogLevel.Information)) Logger.LogInformationMessage($"Navigation started on UI thread, so moving to background thread");
return await Task.Run(() => NavigateAsync(request));
}

if (request.Source is null)
{
if (Logger.IsEnabled(LogLevel.Information)) Logger.LogInformationMessage($"Starting Navigation - Navigator: {this.GetType().Name} Request: {request.Route}");
request = request with { Source = this };
return await Task.Run(() => NavigateAsync(request));
}


Expand Down
4 changes: 1 addition & 3 deletions src/Uno.Extensions.Navigation/NavigationRequest.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
namespace Uno.Extensions.Navigation;

#pragma warning disable SA1313 // Parameter names should begin with lower-case letter
public record NavigationRequest(object Sender, Route Route, CancellationToken? Cancellation = default, Type? Result = null)
public record NavigationRequest(object Sender, Route Route, CancellationToken? Cancellation = default, Type? Result = null, INavigator? Source = null)
#pragma warning restore SA1313 // Parameter names should begin with lower-case letter
{
internal INavigator? Source { get; init; }

public override string ToString() => $"Request [Sender: {Sender.GetType().Name}, Route:{Route}, Result: {Result?.Name ?? "N/A"}]";

internal virtual IResponseNavigator? GetResponseNavigator(IResponseNavigatorFactory responseFactory, INavigator navigator) => default;
Expand Down
2 changes: 1 addition & 1 deletion testing/TestHarness/TestHarness.UITest/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace TestHarness.UITest;

public class Constants
{
public readonly static string WebAssemblyDefaultUri = "https://localhost:51571";
public readonly static string WebAssemblyDefaultUri = "https://localhost:64052";
public readonly static string iOSAppName = "uno.platform.extensions.demo";
public readonly static string AndroidAppName = "uno.platform.extensions.demo";
public readonly static string iOSDeviceNameOrId = "iPad Pro (12.9-inch) (4th generation)";
Expand Down

0 comments on commit 6de6477

Please sign in to comment.