Skip to content

Commit

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

[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/Gyrometer.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 Gyrometer
{
#if !NET7_0_OR_GREATER
private const string JsType = "Windows.Devices.Sensors.Gyrometer";
#endif

private DateTimeOffset _lastReading = DateTimeOffset.MinValue;

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

private static Gyrometer 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 Gyrometer();
}
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 a841dd2

Please sign in to comment.