|
| 1 | +#if NET5_0 |
| 2 | +using System; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Reflection; |
| 5 | +using Microsoft.AspNetCore.Components.Web.Virtualization; |
| 6 | +using Microsoft.JSInterop; |
| 7 | + |
| 8 | +namespace Bunit.JSInterop.ComponentSupport.Virtualize |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Represents an JSInterop handler for the <see cref="Virtualize{TItem}"/> component. |
| 12 | + /// </summary> |
| 13 | + public class VirtualizeJSRuntimeInvocationHandler : JSRuntimeInvocationHandler |
| 14 | + { |
| 15 | + 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() |
| 22 | + { |
| 23 | + // Get <Virtualize> types needed to emulate the <Virtualize>'s JavaScript |
| 24 | + VirtualizeJsInteropType = typeof(Virtualize<>) |
| 25 | + .Assembly |
| 26 | + .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 |
| 30 | + .GetProperty("Value", BindingFlags.Public | BindingFlags.Instance) |
| 31 | + ?? throw new InvalidOperationException("Did not find the Value property on the DotNetObjectReference<VirtualizeJsInterop> type."); |
| 32 | + OnSpacerBeforeVisibleMethodInfo = VirtualizeJsInteropType?.GetMethod("OnSpacerBeforeVisible") |
| 33 | + ?? throw new InvalidOperationException("Did not find the OnSpacerBeforeVisible method on the VirtualizeJsInterop type."); |
| 34 | + } |
| 35 | + |
| 36 | + internal VirtualizeJSRuntimeInvocationHandler() |
| 37 | + : base(CatchAllIdentifier, i => i.Identifier.StartsWith(JsFunctionsPrefix, StringComparison.Ordinal)) |
| 38 | + { } |
| 39 | + |
| 40 | + /// <inheritdoc/> |
| 41 | + protected override void OnInvocation(JSRuntimeInvocation invocation) |
| 42 | + { |
| 43 | + if (invocation.Identifier.Equals(JsFunctionsPrefix + "dispose", StringComparison.Ordinal)) |
| 44 | + return; |
| 45 | + |
| 46 | + // Assert expectations about the internals of the <Virtualize> component |
| 47 | + Debug.Assert(invocation.Identifier.Equals(JsFunctionsPrefix + "init", StringComparison.Ordinal), "Received an unexpected invocation identifier from the <Virtualize> component."); |
| 48 | + Debug.Assert(invocation.Arguments.Count == 3, "Received an unexpected amount of arguments from the <Virtualize> component."); |
| 49 | + Debug.Assert(invocation.Arguments[0] is not null, "Received an unexpected null argument, expected an DotNetObjectReference<VirtualizeJsInterop> instance."); |
| 50 | + |
| 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 | + ); |
| 58 | + |
| 59 | + SetVoidResult(); |
| 60 | + } |
| 61 | + |
| 62 | + private static Action<float, float, float> GetOnSpacerBeforeVisibleCallback(object dotNetObjectReference) |
| 63 | + { |
| 64 | + var virtualizeJsInterop = DotNetObjectReferenceValuePropertyInfo?.GetValue(dotNetObjectReference); |
| 65 | + |
| 66 | + return (spacerSize, spacerSeparation, containerSize) |
| 67 | + => OnSpacerBeforeVisibleMethodInfo.Invoke(virtualizeJsInterop, new object[] { spacerSize, spacerSeparation, containerSize }); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | +#endif |
0 commit comments