From 05c087eddd689940bf58efcea6b98d959d5a198c Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Mon, 20 Feb 2023 21:04:40 -0600 Subject: [PATCH] fix: issue with WASM not loading --- .../DependencyObjectExtensions.Uno.cs | 49 ++++++++++--------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/src/Uno/Prism.Uno/Extensions/DependencyObjectExtensions.Uno.cs b/src/Uno/Prism.Uno/Extensions/DependencyObjectExtensions.Uno.cs index 462a89c988..7d6541f1f7 100644 --- a/src/Uno/Prism.Uno/Extensions/DependencyObjectExtensions.Uno.cs +++ b/src/Uno/Prism.Uno/Extensions/DependencyObjectExtensions.Uno.cs @@ -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 + /// /// Compatibility method to determine if the current thread can access a /// /// The instance to check /// true if the current thread has access to the instance, otherwise false 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 } }