using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; namespace Connected { /// /// This class enables calling events as Non-rendering /// https://learn.microsoft.com/en-us/aspnet/core/blazor/performance?view=aspnetcore-6.0#avoid-rerendering-after-handling-events-without-state-changes /// public static class EventUtil { public static Action AsNonRenderingEventHandler(Action callback) => new SyncReceiver(callback).Invoke; public static Action AsNonRenderingEventHandler( Action callback) => new SyncReceiver(callback).Invoke; public static Func AsNonRenderingEventHandler(Func callback) => new AsyncReceiver(callback).Invoke; public static Func AsNonRenderingEventHandler( Func callback) => new AsyncReceiver(callback).Invoke; private record SyncReceiver(Action callback) : ReceiverBase { public void Invoke() => callback(); } private record SyncReceiver(Action callback) : ReceiverBase { public void Invoke(T arg) => callback(arg); } private record AsyncReceiver(Func callback) : ReceiverBase { public Task Invoke() => callback(); } private record AsyncReceiver(Func callback) : ReceiverBase { public Task Invoke(T arg) => callback(arg); } private record ReceiverBase : IHandleEvent { public Task HandleEventAsync(EventCallbackWorkItem item, object arg) => item.InvokeAsync(arg); } } }