// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; namespace AntDesign { /// /// avoid component re-rendering caused by events to Blazor components.(pure event handlers) /// /// author: SteveSandersonMS, from . /// /// /// issue: . /// /// public static class EventUtil { // The repetition in here is because of the four combinations of handlers (sync/async * with/without arg) 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; // By implementing IHandleEvent, we can override the event handling logic on a per-handler basis // The logic here just calls the callback without triggering any re-rendering class ReceiverBase : IHandleEvent { public Task HandleEventAsync(EventCallbackWorkItem item, object arg) => item.InvokeAsync(arg); } class SyncReceiver : ReceiverBase { private Action callback; public SyncReceiver(Action callback) { this.callback = callback; } public void Invoke() => callback(); } class SyncReceiver : ReceiverBase { private Action callback; public SyncReceiver(Action callback) { this.callback = callback; } public void Invoke(T arg) => callback(arg); } class AsyncReceiver : ReceiverBase { private Func callback; public AsyncReceiver(Func callback) { this.callback = callback; } public Task Invoke() => callback(); } class AsyncReceiver: ReceiverBase { private Func callback; public AsyncReceiver(Func callback) { this.callback = callback; } public Task Invoke(T arg) => callback(arg); } } }