Skip to content

Commit

Permalink
fix: Adjust nullability of ApiExtensibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dr1rrb committed Apr 20, 2023
1 parent a7be3df commit 715e676
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 12 deletions.
22 changes: 20 additions & 2 deletions src/Uno.Foundation/Extensibility/ApiExtensibility.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
namespace Uno.Foundation.Extensibility;
#nullable enable

using System.Diagnostics.CodeAnalysis;

namespace Uno.Foundation.Extensibility;

/// <summary>
/// Registry for API existensibility providers, used to provide optional
Expand Down Expand Up @@ -61,7 +65,8 @@ public static void Register<TOwner>(Type type, Func<TOwner, object> builder)
/// <param name="owner">An optional owner to be passed to the extension constructor</param>
/// <param name="instance">The instance if the creation was successful</param>
/// <returns>True if the creation suceeded, otherwise False.</returns>
public static bool CreateInstance<T>(object owner, out T instance) where T : class
public static bool CreateInstance<T>(object owner, [NotNullWhen(true)] out T? instance)
where T : class
{
lock (_gate)
{
Expand All @@ -76,4 +81,17 @@ public static bool CreateInstance<T>(object owner, out T instance) where T : cla

return false;
}

internal static T CreateInstance<T>(object owner)
where T : class
{
if (CreateInstance<T>(owner, out var instance))
{
return instance;
}
else
{
throw new InvalidOperationException($"Unable to find {typeof(T)} extension");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public partial class DataTransferManager
{
private static readonly Lazy<IDataTransferManagerExtension?> _dataTransferManagerExtension = new Lazy<IDataTransferManagerExtension?>(() =>
{
if (ApiExtensibility.CreateInstance(typeof(DataTransferManager), out IDataTransferManagerExtension dataTransferManagerExtension))
if (ApiExtensibility.CreateInstance(typeof(DataTransferManager), out IDataTransferManagerExtension? dataTransferManagerExtension))
{
return dataTransferManagerExtension;
}
Expand Down
5 changes: 1 addition & 4 deletions src/Uno.UWP/Devices/Haptics/SimpleHapticsController.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ public partial class SimpleHapticsController

partial void InitPlatform()
{
if (!ApiExtensibility.CreateInstance(typeof(SimpleHapticsController), out _simpleHapticsControllerExtension))
{
throw new InvalidOperationException($"Unable to find ISimpleHapticsControllerExtension extension");
}
_simpleHapticsControllerExtension = ApiExtensibility.CreateInstance<ISimpleHapticsControllerExtension>(typeof(SimpleHapticsController));
}

public IReadOnlyList<SimpleHapticsControllerFeedback> SupportedFeedback =>
Expand Down
2 changes: 2 additions & 0 deletions src/Uno.UWP/Media/Playback/MediaPlayer.others.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public void Initialize()
{
this.Log().Error("Unable to create an instance of IMediaPlayerExtension. MediaPlayer will not work.");
}

return;
}

_extension.Events = new MediaPlayerEvents(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Windows.Networking.Connectivity
{
public partial class ConnectionProfile
{
private IConnectionProfileExtension _connectionProfileExtension;
private IConnectionProfileExtension? _connectionProfileExtension;

internal static ConnectionProfile GetInternetConnectionProfile() =>
new ConnectionProfile();
Expand Down
5 changes: 1 addition & 4 deletions src/Uno.UWP/UI/ViewManagement/ApplicationView.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ partial class ApplicationView : IApplicationViewEvents

public ApplicationView()
{
if (!ApiExtensibility.CreateInstance(this, out _applicationViewExtension))
{
throw new InvalidOperationException($"Unable to find IApplicationViewExtension extension");
}
_applicationViewExtension = ApiExtensibility.CreateInstance<IApplicationViewExtension>(this);
}

public string Title
Expand Down

0 comments on commit 715e676

Please sign in to comment.