Skip to content

Commit

Permalink
perf: Add MidiDeviceConnectionWatcher bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
ebariche committed Jun 8, 2023
1 parent a16faba commit 0da6c21
Showing 1 changed file with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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
}
}
}
Expand All @@ -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
}
}
}
Expand Down Expand Up @@ -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
}
}

0 comments on commit 0da6c21

Please sign in to comment.