| File: Virtualization\VirtualizeJsInterop.cs | Web Access |
| Project: src\aspnetcore\src\Components\Web\src\Microsoft.AspNetCore.Components.Web.csproj (Microsoft.AspNetCore.Components.Web) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics.CodeAnalysis; using Microsoft.JSInterop; namespace Microsoft.AspNetCore.Components.Web.Virtualization; internal sealed class VirtualizeJsInterop : IAsyncDisposable { private const string JsFunctionsPrefix = "Blazor._internal.Virtualize"; private readonly IVirtualizeJsCallbacks _owner; private readonly IJSRuntime _jsRuntime; private DotNetObjectReference<VirtualizeJsInterop>? _selfReference; [DynamicDependency(nameof(OnSpacerBeforeVisible))] [DynamicDependency(nameof(OnSpacerAfterVisible))] public VirtualizeJsInterop(IVirtualizeJsCallbacks owner, IJSRuntime jsRuntime) { _owner = owner; _jsRuntime = jsRuntime; } public async ValueTask InitializeAsync(ElementReference spacerBefore, ElementReference spacerAfter, int anchorMode) { _selfReference = DotNetObjectReference.Create(this); await _jsRuntime.InvokeVoidAsync($"{JsFunctionsPrefix}.init", _selfReference, spacerBefore, spacerAfter, anchorMode); } [JSInvokable] public void OnSpacerBeforeVisible(float spacerSize, float spacerSeparation, float containerSize) { _owner.OnBeforeSpacerVisible(spacerSize, spacerSeparation, containerSize); } [JSInvokable] public void OnSpacerAfterVisible(float spacerSize, float spacerSeparation, float containerSize) { _owner.OnAfterSpacerVisible(spacerSize, spacerSeparation, containerSize); } public ValueTask ScrollToBottomAsync() { return _jsRuntime.InvokeVoidAsync($"{JsFunctionsPrefix}.scrollToBottom", _selfReference); } public ValueTask RefreshObserversAsync() { return _jsRuntime.InvokeVoidAsync($"{JsFunctionsPrefix}.refreshObservers", _selfReference); } public ValueTask SetAnchorModeAsync(int anchorMode) { return _jsRuntime.InvokeVoidAsync($"{JsFunctionsPrefix}.setAnchorMode", _selfReference, anchorMode); } public ValueTask RestoreAnchorAsync() { return _jsRuntime.InvokeVoidAsync($"{JsFunctionsPrefix}.restoreAnchor", _selfReference); } public ValueTask AlignToItemAsync(int localIndex, CancellationToken cancellationToken = default) { return _jsRuntime.InvokeVoidAsync($"{JsFunctionsPrefix}.alignToItem", cancellationToken, _selfReference, localIndex); } public ValueTask BeginProgrammaticScrollAsync() { return _jsRuntime.InvokeVoidAsync($"{JsFunctionsPrefix}.beginProgrammaticScroll", _selfReference); } public ValueTask<bool> IsFollowingBottomAsync() { return _jsRuntime.InvokeAsync<bool>($"{JsFunctionsPrefix}.isFollowingBottom", _selfReference); } public async ValueTask DisposeAsync() { if (_selfReference != null) { try { await _jsRuntime.InvokeVoidAsync($"{JsFunctionsPrefix}.dispose", _selfReference); } catch (JSDisconnectedException) { // If the browser is gone, we don't need it to clean up any browser-side state } } } }