Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactiveCommand not respecting canExecute

Tags:

c#

reactiveui

Perhaps I'm missing something, however I can't get ReactiveCommand to prevent execution based on the canExecute observable.

Below is the simplest example I can conjure. I would expect the command to never fire, however it is.

What am I missing?

void Main()
{
    var canExecute = Observable.Return(false);
    var myCommand = ReactiveCommand.CreateAsyncTask(canExecute, m => functions.doAllThings(m));

    myCommand.Subscribe(x=>"executing".Dump());

    myCommand.Execute("Tom"); // This fires the command. I would have expected it to block
}

static class functions
{
    public static Task doAllThings(object message)
    {
        var result = Task.Run(() =>{
                    "running task...".Dump();
                    return "hello " + (string)message;});

        return result;
    }
}

Note - This is question is kind of a 'fork' from Executing a command from another command. I believe that this is more the core issue.

like image 349
Damien Sawyer Avatar asked Aug 04 '26 03:08

Damien Sawyer


1 Answers

This is by-design. ReactiveUI doesn't stop you from explicitly calling Execute / ExecuteAsync, and trusts that You Know What You're Doing™

like image 103
Ana Betts Avatar answered Aug 05 '26 18:08

Ana Betts