I got a javascript observer in my Blazor-App. (while scrolling it is detecting a GUI-item on the lower end of the scrolled list to load following items. -> paging, lazy load) This listener is subscribed one time, I checked by breakpoint. But each time the observer is activated it is firing two times instead of one time. I can't figure out why I get the event two times. Could it be it is firing when it gets into the view and again when it is leaving the view?
Do you have a idea, why OnIntersectionSearch() is called always two times and how to avoid it?
JavaScript:
window.ObserverSearch = {
observer: null,
Initialize: function (component, observerId) {
this.observer = new IntersectionObserver(e => {
component.invokeMethodAsync('OnIntersectionSearch');
});
let element = document.getElementById(observerId);
if (element == null) throw new Error("Target was not found");
this.observer.observe(element);
}
};
Blazor component InfiniteScrollSearch.razor
@inject IJSRuntime JSRuntime
@ChildContent
InfiniteScrollSearch.razor.cs
public partial class InfiniteScrollSearch
{
[Parameter]
public RenderFragment? ChildContent { get; set; }
[Parameter]
public string? ObserverSearchTarget { get; set; }
[Parameter]
public EventCallback<bool> ObservableSearchEndReached { get; set; }
private DotNetObjectReference<InfiniteScrollSearch>? _objectRef;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
_objectRef = DotNetObjectReference.Create(this);
await JSRuntime.InvokeAsync<dynamic>("ObserverSearch.Initialize", _objectRef, ObserverSearchTarget);
}
}
[JSInvokable]
public async Task OnIntersectionSearch()
{
// this is always to times called
await ObservableSearchEndReached.InvokeAsync(true);
}
}