Skip to content

Commit

Permalink
fix: issue with WASM not loading
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Feb 21, 2023
1 parent a385e7c commit 05c087e
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions src/Uno/Prism.Uno/Extensions/DependencyObjectExtensions.Uno.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Data;
using Windows.Foundation.Metadata;

namespace Prism
{
internal static partial class DependencyObjectExtensions
{
#if HAS_UNO_WINUI
// Related to :
// https://github.com/dotnet/runtime/issues/76959
// https://github.com/unoplatform/uno/blob/56d3f6ece16b2dba51776e95a3c847c9c4b68e8b/src/Uno.UI.Dispatching/Core/CoreDispatcher.wasm.cs#L17
private static bool IsWebAssemblyThreadingSupported { get; }
= Environment.GetEnvironmentVariable("UNO_BOOTSTRAP_MONO_RUNTIME_FEATURES")
?.Split(',').Any(v => v.Equals("threads", StringComparison.OrdinalIgnoreCase)) ?? false;

private static bool IsWebAssembly { get; }
= RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER"));
#endif

/// <summary>
/// Compatibility method to determine if the current thread can access a <see cref="DependencyObject"/>
/// </summary>
/// <param name="instance">The instance to check</param>
/// <returns><c>true</c> if the current thread has access to the instance, otherwise <c>false</c></returns>
public static bool CheckAccess(this DependencyObject instance)
#if __WASM__
// This needs to evolve, once threading is supported
// See https://github.com/unoplatform/uno/issues/2302
=> System.Threading.Thread.CurrentThread.ManagedThreadId == 1;
#elif HAS_WINUI
#if NETCOREAPP
=> instance.DispatcherQueue.HasThreadAccess;
#else
#if HAS_WINUI
#if HAS_UNO_WINUI
{
if (IsWebAssembly && !IsWebAssemblyThreadingSupported)
{
// Dispatcher queue HasThreadAccess is not yet implemented in Uno, we can fall back on CoreDispatcher
// See https://github.com/unoplatform/uno/issues/5818
if(ApiInformation.IsPropertyPresent("Microsoft.UI.Dispatching.DispatcherQueue", nameof(Microsoft.UI.Dispatching.DispatcherQueue.HasThreadAccess)))
{
return instance.DispatcherQueue.HasThreadAccess;
}
else
{
return instance.Dispatcher.HasThreadAccess;
}
// When threading is disabled, we need to return true
return true;
}

return instance.DispatcherQueue.HasThreadAccess;
}
#else
=> instance.DispatcherQueue.HasThreadAccess;
#endif
#else
=> instance.Dispatcher.HasThreadAccess;
=> instance.Dispatcher.HasThreadAccess;
#endif
}
}

0 comments on commit 05c087e

Please sign in to comment.