Skip to content

Commit

Permalink
refactor(components): utilize UnsafeAccessor to get JSRuntime in Elem…
Browse files Browse the repository at this point in the history
…entReferenceExtensions
  • Loading branch information
desmondinho committed Jul 6, 2024
1 parent a4896e7 commit e0f5d5b
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/LumexUI/Extensions/ElementReferenceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// See the license here https://github.com/LumexUI/lumexui/blob/main/LICENSE

using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;

using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
Expand All @@ -13,8 +13,8 @@ namespace LumexUI.Extensions;
[ExcludeFromCodeCoverage]
public static class ElementReferenceExtensions
{
private static readonly PropertyInfo? _jsRuntimeProp = typeof( WebElementReferenceContext )
.GetProperty( "JSRuntime", BindingFlags.Instance | BindingFlags.NonPublic );
[UnsafeAccessor( UnsafeAccessorKind.Field, Name = "<JSRuntime>k__BackingField" )]
private static extern ref IJSRuntime GetJSRuntime( WebElementReferenceContext context );

public static ValueTask<int> GetScrollHeightAsync( this ElementReference elementReference )
{
Expand All @@ -29,7 +29,6 @@ private static IJSRuntime GetJSRuntime( this ElementReference elementReference )
throw new InvalidOperationException( "ElementReference has not been configured correctly." );
}

var jsRuntime = (IJSRuntime?)_jsRuntimeProp?.GetValue( context );
return jsRuntime ?? throw new InvalidOperationException( "No JavaScript runtime found." );
return GetJSRuntime( context ) ?? throw new InvalidOperationException( "No JavaScript runtime found." );
}
}

0 comments on commit e0f5d5b

Please sign in to comment.