Skip to content

Commit

Permalink
chore: Use static Lazy instance for all sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
nickodei committed Jan 18, 2025
1 parent 4c8b326 commit c1f2bfe
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 167 deletions.
2 changes: 1 addition & 1 deletion src/Uno.UWP/Devices/Sensors/Accelerometer.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public uint ReportInterval
return;
}

lock (_syncLock)
lock (_readingChangedWrapper.SyncLock)
{
_reportInterval = value;

Expand Down
30 changes: 6 additions & 24 deletions src/Uno.UWP/Devices/Sensors/Accelerometer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if __IOS__ || __ANDROID__ || __WASM__
#nullable enable

using System;
using System.Collections.Generic;
using System.Text;
Expand All @@ -12,10 +14,7 @@ namespace Windows.Devices.Sensors
/// </summary>
public partial class Accelerometer
{
private readonly static object _syncLock = new();

private static Accelerometer _instance;
private static bool _initializationAttempted;
private readonly static Lazy<Accelerometer?> _instance = new Lazy<Accelerometer?>(() => TryCreateInstance());

private readonly StartStopTypedEventWrapper<Accelerometer, AccelerometerReadingChangedEventArgs> _readingChangedWrapper;
private readonly StartStopTypedEventWrapper<Accelerometer, AccelerometerShakenEventArgs> _shakenWrapper;
Expand All @@ -36,34 +35,17 @@ private Accelerometer()
{
_readingChangedWrapper = new StartStopTypedEventWrapper<Accelerometer, AccelerometerReadingChangedEventArgs>(
() => StartReadingChanged(),
() => StopReadingChanged(),
_syncLock);
() => StopReadingChanged());
_shakenWrapper = new StartStopTypedEventWrapper<Accelerometer, AccelerometerShakenEventArgs>(
() => StartShaken(),
() => StopShaken(),
_syncLock);
() => StopShaken());
}

/// <summary>
/// Returns the default accelerometer.
/// </summary>
/// <returns>The default accelerometer or null if no integrated accelerometers are found.</returns>
public static Accelerometer GetDefault()
{
if (_initializationAttempted)
{
return _instance;
}
lock (_syncLock)
{
if (!_initializationAttempted)
{
_instance = TryCreateInstance();
_initializationAttempted = true;
}
return _instance;
}
}
public static Accelerometer? GetDefault() => _instance.Value;

/// <summary>
/// Occurs each time the accelerometer reports a new sensor reading.
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UWP/Devices/Sensors/Barometer.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public uint ReportInterval
return;
}

lock (_syncLock)
lock (_readingChangedWrapper.SyncLock)
{
_reportInterval = value;

Expand Down
27 changes: 5 additions & 22 deletions src/Uno.UWP/Devices/Sensors/Barometer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#if __ANDROID__ || __IOS__
#nullable enable

using System;
using Uno.Helpers;
using Windows.Foundation;

Expand All @@ -10,10 +12,7 @@ namespace Windows.Devices.Sensors
/// </summary>
public partial class Barometer
{
private static readonly object _syncLock = new();

private static bool _initializationAttempted;
private static Barometer _instance;
private readonly static Lazy<Barometer?> _instance = new Lazy<Barometer?>(() => TryCreateInstance());

private readonly StartStopTypedEventWrapper<Barometer, BarometerReadingChangedEventArgs> _readingChangedWrapper;

Expand All @@ -24,30 +23,14 @@ private Barometer()
{
_readingChangedWrapper = new StartStopTypedEventWrapper<Barometer, BarometerReadingChangedEventArgs>(
() => StartReading(),
() => StopReading(),
_syncLock);
() => StopReading());
}

/// <summary>
/// Returns the default barometer sensor.
/// </summary>
/// <returns>If no barometer sensor is available, this method will return null.</returns>
public static Barometer GetDefault()
{
if (_initializationAttempted)
{
return _instance;
}
lock (_syncLock)
{
if (!_initializationAttempted)
{
_instance = TryCreateInstance();
_initializationAttempted = true;
}
return _instance;
}
}
public static Barometer? GetDefault() => _instance.Value;

/// <summary>
/// Occurs each time the barometer reports a new sensor reading.
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UWP/Devices/Sensors/Compass.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public uint ReportInterval
get => _reportInterval;
set
{
lock (_syncLock)
lock (_readingChangedWrapper.SyncLock)
{
if (_reportInterval == value)
{
Expand Down
28 changes: 6 additions & 22 deletions src/Uno.UWP/Devices/Sensors/Compass.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#if __IOS__ || __ANDROID__ || __WASM__
#nullable enable

using System;
using Uno.Helpers;
using Windows.Foundation;

Expand All @@ -10,10 +13,7 @@ namespace Windows.Devices.Sensors;
/// </summary>
public partial class Compass
{
private readonly static object _syncLock = new();

private static Compass _instance;
private static bool _initializationAttempted;
private readonly static Lazy<Compass?> _instance = new Lazy<Compass?>(() => TryCreateInstance());

private readonly StartStopTypedEventWrapper<Compass, CompassReadingChangedEventArgs> _readingChangedWrapper;

Expand All @@ -24,30 +24,14 @@ private Compass()
{
_readingChangedWrapper = new StartStopTypedEventWrapper<Compass, CompassReadingChangedEventArgs>(
() => StartReadingChanged(),
() => StopReadingChanged(),
_syncLock);
() => StopReadingChanged());
}

/// <summary>
/// Returns the default compass.
/// </summary>
/// <returns>The default compass or null if no integrated compasses are found.</returns>
public static Compass GetDefault()
{
if (_initializationAttempted)
{
return _instance;
}
lock (_syncLock)
{
if (!_initializationAttempted)
{
_instance = TryCreateInstance();
_initializationAttempted = true;
}
return _instance;
}
}
public static Compass? GetDefault() => _instance.Value;

/// <summary>
/// Occurs each time the compass reports a new sensor reading.
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UWP/Devices/Sensors/Compass.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public uint ReportInterval
return;
}

lock (_syncLock)
lock (_readingChangedWrapper.SyncLock)
{
_reportInterval = value;

Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UWP/Devices/Sensors/Gyrometer.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public uint ReportInterval
return;
}

lock (_syncLock)
lock (_readingChangedWrapper.SyncLock)
{
_reportInterval = value;

Expand Down
28 changes: 6 additions & 22 deletions src/Uno.UWP/Devices/Sensors/Gyrometer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#if __IOS__ || __ANDROID__ || __WASM__
#nullable enable

using System;
using Uno.Extensions;
using Uno.Foundation.Logging;
using Uno.Helpers;
Expand All @@ -11,10 +14,7 @@ namespace Windows.Devices.Sensors
/// </summary>
public partial class Gyrometer
{
private readonly static object _syncLock = new();

private static Gyrometer _instance;
private static bool _initializationAttempted;
private readonly static Lazy<Gyrometer?> _instance = new Lazy<Gyrometer?>(() => TryCreateInstance());

private readonly StartStopTypedEventWrapper<Gyrometer, GyrometerReadingChangedEventArgs> _readingChangedWrapper;

Expand All @@ -25,30 +25,14 @@ private Gyrometer()
{
_readingChangedWrapper = new StartStopTypedEventWrapper<Gyrometer, GyrometerReadingChangedEventArgs>(
() => StartReading(),
() => StopReading(),
_syncLock);
() => StopReading());
}

/// <summary>
/// Returns the default gyrometer.
/// </summary>
/// <returns>Null if no integrated gyrometers are found.</returns>
public static Gyrometer GetDefault()
{
if (_initializationAttempted)
{
return _instance;
}
lock (_syncLock)
{
if (!_initializationAttempted)
{
_instance = TryCreateInstance();
_initializationAttempted = true;
}
return _instance;
}
}
public static Gyrometer? GetDefault() => _instance.Value;

/// <summary>
/// Occurs each time the gyrometer reports the current sensor reading.
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UWP/Devices/Sensors/Magnetometer.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public uint ReportInterval
return;
}

lock (_syncLock)
lock (_readingChangedWrapper.SyncLock)
{
_reportInterval = value;

Expand Down
27 changes: 5 additions & 22 deletions src/Uno.UWP/Devices/Sensors/Magnetometer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if __IOS__ || __ANDROID__ || __WASM__
#nullable enable

using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -14,10 +16,7 @@ namespace Windows.Devices.Sensors
/// </summary>
public partial class Magnetometer
{
private readonly static object _syncLock = new();

private static Magnetometer _instance;
private static bool _initializationAttempted;
private readonly static Lazy<Magnetometer?> _instance = new Lazy<Magnetometer?>(() => TryCreateInstance());

private readonly StartStopTypedEventWrapper<Magnetometer, MagnetometerReadingChangedEventArgs> _readingChangedWrapper;

Expand All @@ -28,30 +27,14 @@ private Magnetometer()
{
_readingChangedWrapper = new StartStopTypedEventWrapper<Magnetometer, MagnetometerReadingChangedEventArgs>(
() => StartReading(),
() => StopReading(),
_syncLock);
() => StopReading());
}

/// <summary>
/// Returns the default magnetometer.
/// </summary>
/// <returns>The default magnetometer.</returns>
public static Magnetometer GetDefault()
{
if (_initializationAttempted)
{
return _instance;
}
lock (_syncLock)
{
if (!_initializationAttempted)
{
_instance = TryCreateInstance();
_initializationAttempted = true;
}
return _instance;
}
}
public static Magnetometer? GetDefault() => _instance.Value;

/// <summary>
/// Occurs each time the compass reports a new sensor reading.
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UWP/Devices/Sensors/Pedometer.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public uint ReportInterval
return;
}

lock (_syncLock)
lock (_readingChangedWrapper.SyncLock)
{
_reportInterval = value;

Expand Down
29 changes: 6 additions & 23 deletions src/Uno.UWP/Devices/Sensors/Pedometer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if __IOS__ || __ANDROID__
#nullable enable

using System;
using System.Threading.Tasks;
using Uno.Helpers;
Expand All @@ -12,10 +14,7 @@ namespace Windows.Devices.Sensors
/// </summary>
public partial class Pedometer
{
private readonly static object _syncLock = new();

private static bool _initializationAttempted;
private static Task<Pedometer> _instanceTask;
private readonly static Lazy<Task<Pedometer?>> _instance = new Lazy<Task<Pedometer?>>(() => Task.Run(() => TryCreateInstance()));

private readonly StartStopTypedEventWrapper<Pedometer, PedometerReadingChangedEventArgs> _readingChangedWrapper;

Expand All @@ -26,28 +25,12 @@ private Pedometer()
{
_readingChangedWrapper = new StartStopTypedEventWrapper<Pedometer, PedometerReadingChangedEventArgs>(
() => StartReading(),
() => StopReading(),
_syncLock);
() => StopReading());
}

public static IAsyncOperation<Pedometer> GetDefaultAsync() => GetDefaultImplAsync().AsAsyncOperation();
public static IAsyncOperation<Pedometer?> GetDefaultAsync() => GetDefaultImplAsync().AsAsyncOperation();

private static async Task<Pedometer> GetDefaultImplAsync()
{
if (_initializationAttempted)
{
return await _instanceTask;
}
lock (_syncLock)
{
if (!_initializationAttempted)
{
_instanceTask = Task.Run(() => TryCreateInstance());
_initializationAttempted = true;
}
}
return await _instanceTask;
}
private static async Task<Pedometer?> GetDefaultImplAsync() => await _instance.Value;

public event TypedEventHandler<Pedometer, PedometerReadingChangedEventArgs> ReadingChanged
{
Expand Down
Loading

0 comments on commit c1f2bfe

Please sign in to comment.