Skip to content

Commit

Permalink
perf: Add Magnetometer bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
ebariche committed May 31, 2023
1 parent 3de29ad commit 8911155
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Uno.UWP/Devices/Sensors/Magnetometer.Interop.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@ internal partial class Magnetometer
{
internal static partial class NativeMethods
{
private const string JsType = "globalThis.Windows.Devices.Sensors.Magnetometer";

[JSImport($"{JsType}.initialize")]
internal static partial bool Initialize();

[JSImport($"{JsType}.startReading")]
internal static partial void StartReading();

[JSImport($"{JsType}.stopReading")]
internal static partial void StopReading();

}
}
}
#endif
#endif
14 changes: 14 additions & 0 deletions src/Uno.UWP/Devices/Sensors/Magnetometer.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ namespace Windows.Devices.Sensors
{
public partial class Magnetometer
{
#if !NET7_0_OR_GREATER
private const string JsType = "Windows.Devices.Sensors.Magnetometer";
#endif

private DateTimeOffset _lastReading = DateTimeOffset.MinValue;

Expand All @@ -24,25 +26,37 @@ private Magnetometer()

private static Magnetometer TryCreateInstance()
{
#if NET7_0_OR_GREATER
return NativeMethods.Initialize() ? new() : null;
#else
var command = $"{JsType}.initialize()";
var initialized = Uno.Foundation.WebAssemblyRuntime.InvokeJS(command);
if (bool.Parse(initialized) == true)
{
return new Magnetometer();
}
return null;
#endif
}

private void StartReading()
{
#if NET7_0_OR_GREATER
NativeMethods.StartReading();
#else
var command = $"{JsType}.startReading()";
Uno.Foundation.WebAssemblyRuntime.InvokeJS(command);
#endif
}

private void StopReading()
{
#if NET7_0_OR_GREATER
NativeMethods.StopReading();
#else
var command = $"{JsType}.stopReading()";
Uno.Foundation.WebAssemblyRuntime.InvokeJS(command);
#endif
}

/// <summary>
Expand Down

0 comments on commit 8911155

Please sign in to comment.