Ejemplo n.º 1
0
        /// <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);
            }
        }
Ejemplo n.º 2
0
        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();
        }
Ejemplo n.º 3
0
        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()));
            });
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        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);
        }