/// <summary> /// Invokes the delegate associated with this binding. /// </summary> /// <param name="e">The <see cref="UIEventArgs"/>.</param> /// <returns></returns> public Task Invoke(UIEventArgs e) { switch (_delegate) { case Action action: action.Invoke(); return(Task.CompletedTask); case Action <UIEventArgs> actionEventArgs: actionEventArgs.Invoke(e); return(Task.CompletedTask); case Func <Task> func: return(func.Invoke()); case Func <UIEventArgs, Task> funcEventArgs: return(funcEventArgs.Invoke(e)); case MulticastDelegate @delegate: return(@delegate.DynamicInvoke(e) as Task ?? Task.CompletedTask); case null: return(Task.CompletedTask); } }
void IHandleEvent.HandleEvent(EventHandlerInvoker binding, UIEventArgs args) { var task = binding.Invoke(args); ContinueAfterLifecycleTask(task); // After each event, we synchronously re-render (unless !ShouldRender()) // This just saves the developer the trouble of putting "StateHasChanged();" // at the end of every event callback. StateHasChanged(); }
public async Task EventCallback_FuncTTask_ArgMismatch() { // Arrange var component = new EventCountingComponent(); int runCount = 0; UIEventArgs arg = null; var callback = new EventCallback(component, (Func <UIEventArgs, Task>)((e) => { arg = e; runCount++; return(Task.CompletedTask); })); // Act & Assert await Assert.ThrowsAsync <ArgumentException>(() => { return(callback.InvokeAsync(new StringBuilder())); }); }
Task IHandleEvent.HandleEventAsync(EventHandlerInvoker binding, UIEventArgs args) { var task = binding.Invoke(args); var shouldAwaitTask = task.Status != TaskStatus.RanToCompletion && task.Status != TaskStatus.Canceled; // After each event, we synchronously re-render (unless !ShouldRender()) // This just saves the developer the trouble of putting "StateHasChanged();" // at the end of every event callback. StateHasChanged(); return(shouldAwaitTask ? CallStateHasChangedOnAsyncCompletion(task) : Task.CompletedTask); }
public async Task EventCallback_ActionT_Null() { // Arrange var component = new EventCountingComponent(); int runCount = 0; UIEventArgs arg = null; var callback = new EventCallback(component, (Action <UIEventArgs>)((e) => { arg = e; runCount++; })); // Act await callback.InvokeAsync(null); // Assert Assert.Null(arg); Assert.Equal(1, runCount); Assert.Equal(1, component.Count); }
public async Task EventCallbackOfT_FuncTTask_Arg() { // Arrange var component = new EventCountingComponent(); int runCount = 0; UIEventArgs arg = null; var callback = new EventCallback <UIEventArgs>(component, (Func <UIEventArgs, Task>)((e) => { arg = e; runCount++; return(Task.CompletedTask); })); // Act await callback.InvokeAsync(new UIEventArgs()); // Assert Assert.NotNull(arg); Assert.Equal(1, runCount); Assert.Equal(1, component.Count); }