diff --git a/src/Uno.UWP/Devices/Enumeration/Internal/Providers/Midi/MidiDeviceConnectionWatcher.wasm.cs b/src/Uno.UWP/Devices/Enumeration/Internal/Providers/Midi/MidiDeviceConnectionWatcher.wasm.cs index 26dbd5a580d6..f89c6edbf524 100644 --- a/src/Uno.UWP/Devices/Enumeration/Internal/Providers/Midi/MidiDeviceConnectionWatcher.wasm.cs +++ b/src/Uno.UWP/Devices/Enumeration/Internal/Providers/Midi/MidiDeviceConnectionWatcher.wasm.cs @@ -5,11 +5,17 @@ using Uno.Extensions.Specialized; using Uno.Foundation; +#if NET7_0_OR_GREATER +using System.Runtime.InteropServices.JavaScript; +#endif + namespace Uno.Devices.Enumeration.Internal.Providers.Midi { - public static class MidiDeviceConnectionWatcher + public static partial class MidiDeviceConnectionWatcher { +#if !NET7_0_OR_GREATER private const string JsType = "Uno.Devices.Enumeration.Internal.Providers.Midi.MidiDeviceConnectionWatcher"; +#endif private static readonly object _syncLock = new object(); @@ -22,9 +28,13 @@ internal static void AddObserver(MidiDeviceClassProviderBase provider) _observers.Add(provider); if (_observers.Count == 1) { +#if NET7_0_OR_GREATER + NativeMethods.StartStateChanged(); +#else // start WASM device event subscription var command = $"{JsType}.startStateChanged()"; WebAssemblyRuntime.InvokeJS(command); +#endif } } } @@ -36,9 +46,13 @@ internal static void RemoveObserver(MidiDeviceClassProviderBase provider) _observers.Remove(provider); if (_observers.Count == 0) { +#if NET7_0_OR_GREATER + NativeMethods.StopStateChanged(); +#else // stop WASM device event subscription var command = $"{JsType}.stopStateChanged()"; WebAssemblyRuntime.InvokeJS(command); +#endif } } } @@ -72,5 +86,16 @@ public static int DispatchStateChanged(string id, string name, bool isInput, boo return 0; } + +#if NET7_0_OR_GREATER + internal static partial class NativeMethods + { + [JSImport($"globalThis.Uno.Devices.Enumeration.Internal.Providers.Midi.MidiDeviceConnectionWatcher.startStateChanged")] + internal static partial void StartStateChanged(); + + [JSImport($"globalThis.Uno.Devices.Enumeration.Internal.Providers.Midi.MidiDeviceConnectionWatcher.stopStateChanged")] + internal static partial void StopStateChanged(); + } +#endif } }