55using Microsoft . AspNetCore . Components . Web . Virtualization ;
66using Microsoft . JSInterop ;
77
8- namespace Bunit . JSInterop . ComponentSupport . Virtualize
8+ namespace Bunit . JSInterop . InvocationHandlers
99{
1010 /// <summary>
1111 /// Represents an JSInterop handler for the <see cref="Virtualize{TItem}"/> component.
1212 /// </summary>
1313 public class VirtualizeJSRuntimeInvocationHandler : JSRuntimeInvocationHandler
1414 {
1515 private const string JsFunctionsPrefix = "Blazor._internal.Virtualize." ;
16- private static readonly Type VirtualizeJsInteropType ;
17- private static readonly Type DotNetObjectReferenceVirtualizeJsInteropType ;
18- private static readonly PropertyInfo DotNetObjectReferenceValuePropertyInfo ;
19- private static readonly MethodInfo OnSpacerBeforeVisibleMethodInfo ;
20-
21- static VirtualizeJSRuntimeInvocationHandler ( )
16+ private static readonly Lazy < ( PropertyInfo , MethodInfo ) > VirtualizeReflection = new Lazy < ( PropertyInfo , MethodInfo ) > ( ( ) =>
2217 {
23- // Get <Virtualize> types needed to emulate the <Virtualize>'s JavaScript
24- VirtualizeJsInteropType = typeof ( Virtualize < > )
18+ var VirtualizeJsInteropType = typeof ( Virtualize < > )
2519 . Assembly
2620 . GetType ( "Microsoft.AspNetCore.Components.Web.Virtualization.VirtualizeJsInterop" )
27- ?? throw new InvalidOperationException ( "Did not find the VirtualizeJsInterop in the expected namespace/assembly." ) ;
28- DotNetObjectReferenceVirtualizeJsInteropType = typeof ( DotNetObjectReference < > ) . MakeGenericType ( VirtualizeJsInteropType ) ;
29- DotNetObjectReferenceValuePropertyInfo = DotNetObjectReferenceVirtualizeJsInteropType
21+ ?? throw new InvalidOperationException ( "Did not find the VirtualizeJsInterop in the expected namespace/assembly." ) ;
22+
23+ var DotNetObjectReferenceVirtualizeJsInteropType = typeof ( DotNetObjectReference < > ) . MakeGenericType ( VirtualizeJsInteropType ) ;
24+
25+ var dotNetObjectReferenceValuePropertyInfo = DotNetObjectReferenceVirtualizeJsInteropType
3026 . GetProperty ( "Value" , BindingFlags . Public | BindingFlags . Instance )
3127 ?? throw new InvalidOperationException ( "Did not find the Value property on the DotNetObjectReference<VirtualizeJsInterop> type." ) ;
32- OnSpacerBeforeVisibleMethodInfo = VirtualizeJsInteropType ? . GetMethod ( "OnSpacerBeforeVisible" )
28+
29+ var onSpacerBeforeVisibleMethodInfo = VirtualizeJsInteropType ? . GetMethod ( "OnSpacerBeforeVisible" )
3330 ?? throw new InvalidOperationException ( "Did not find the OnSpacerBeforeVisible method on the VirtualizeJsInterop type." ) ;
34- }
31+
32+ return ( dotNetObjectReferenceValuePropertyInfo , onSpacerBeforeVisibleMethodInfo ) ;
33+ } ) ;
3534
3635 internal VirtualizeJSRuntimeInvocationHandler ( )
3736 : base ( CatchAllIdentifier , i => i . Identifier . StartsWith ( JsFunctionsPrefix , StringComparison . Ordinal ) )
@@ -48,23 +47,22 @@ protected override void OnInvocation(JSRuntimeInvocation invocation)
4847 Debug . Assert ( invocation . Arguments . Count == 3 , "Received an unexpected amount of arguments from the <Virtualize> component." ) ;
4948 Debug . Assert ( invocation . Arguments [ 0 ] is not null , "Received an unexpected null argument, expected an DotNetObjectReference<VirtualizeJsInterop> instance." ) ;
5049
51- var onSpacerBeforeVisible = GetOnSpacerBeforeVisibleCallback ( invocation . Arguments [ 0 ] ! ) ;
52-
53- onSpacerBeforeVisible (
54- 0f /* spacerSize */ ,
55- 0f /* spacerSeparation */ ,
56- 10_000_000f /* containerSize - very large number to ensure all items are loaded at once */
57- ) ;
50+ InvokeOnSpacerBeforeVisible ( invocation . Arguments [ 0 ] ! ) ;
5851
5952 SetVoidResult ( ) ;
6053 }
6154
62- private static Action < float , float , float > GetOnSpacerBeforeVisibleCallback ( object dotNetObjectReference )
55+ private static void InvokeOnSpacerBeforeVisible ( object dotNetObjectReference )
6356 {
64- var virtualizeJsInterop = DotNetObjectReferenceValuePropertyInfo ? . GetValue ( dotNetObjectReference ) ;
65-
66- return ( spacerSize , spacerSeparation , containerSize )
67- => OnSpacerBeforeVisibleMethodInfo . Invoke ( virtualizeJsInterop , new object [ ] { spacerSize , spacerSeparation , containerSize } ) ;
57+ var ( dotNetObjectReferenceValuePropertyInfo , onSpacerBeforeVisibleMethodInfo ) = VirtualizeReflection . Value ;
58+ var virtualizeJsInterop = dotNetObjectReferenceValuePropertyInfo . GetValue ( dotNetObjectReference ) ;
59+ onSpacerBeforeVisibleMethodInfo . Invoke (
60+ virtualizeJsInterop ,
61+ new object [ ] {
62+ 0f /* spacerSize */ ,
63+ 0f /* spacerSeparation */ ,
64+ 1_000_000_000f /* containerSize - very large number to ensure all items are loaded at once */
65+ } ) ;
6866 }
6967 }
7068}
0 commit comments