From 1a6e5984e4a4e6441eb294779f9750471a596fed Mon Sep 17 00:00:00 2001 From: Elie Bariche <33458222+ebariche@users.noreply.github.com> Date: Fri, 9 Jun 2023 03:39:37 -0400 Subject: [PATCH] perf: Add RequestAsync binding --- .../Midi/Internal/WasmMidiAccess.wasm.cs | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Uno.UWP/Devices/Midi/Internal/WasmMidiAccess.wasm.cs b/src/Uno.UWP/Devices/Midi/Internal/WasmMidiAccess.wasm.cs index 0f07586bf0be..943fc1faf7f3 100644 --- a/src/Uno.UWP/Devices/Midi/Internal/WasmMidiAccess.wasm.cs +++ b/src/Uno.UWP/Devices/Midi/Internal/WasmMidiAccess.wasm.cs @@ -2,16 +2,23 @@ using System.Collections.Generic; using System.Text; using System.Threading.Tasks; + +#if NET7_0_OR_GREATER +using System.Runtime.InteropServices.JavaScript; +#else using static Uno.Foundation.WebAssemblyRuntime; +#endif namespace Uno.Devices.Midi.Internal { /// /// Handles WASM MIDI access permission request /// - public static class WasmMidiAccess + public static partial class WasmMidiAccess { +#if !NET7_0_OR_GREATER private const string JsType = "Uno.Devices.Midi.Internal.WasmMidiAccess"; +#endif private static bool _webMidiAccessible; @@ -23,11 +30,26 @@ public static async Task RequestAsync() } var systemExclusiveRequested = WinRTFeatureConfiguration.Midi.RequestSystemExclusiveAccess; + +#if NET7_0_OR_GREATER + var result = await NativeMethods.RequestAsync(systemExclusiveRequested); +#else var serializedRequest = systemExclusiveRequested.ToString().ToLowerInvariant(); var command = $"{JsType}.request({serializedRequest})"; var result = await InvokeAsync(command); +#endif + _webMidiAccessible = bool.Parse(result); + return _webMidiAccessible; } + +#if NET7_0_OR_GREATER + internal static partial class NativeMethods + { + [JSImport("globalThis.Uno.Devices.Midi.Internal.WasmMidiAccess.request")] + internal static partial Task RequestAsync(bool exclusive); + } +#endif } }