Send message from independent function

I have a function which is independent, no return type. Id like to be able to send a message but am unsure how to do it.

ive tried:

let direction = ...
let _ = Task::perform(async { Message::ActionGesture(direction) }, |result | result);

I assume you’ve fixed this at this point, but the idea is you return Tasks to the runtime and let it execute them.

Task::perform(my_async_fn(), Message::ActionGesture) is the way to use it, assuming my_async_fn() returns type T and Message looks like:

pub enum Message {
    ActionGesture(T)
}

That T can be whatever you want, so long as they match. If T is (), then just make it

pub enum Message {
    ActionGesture
}

I think that ought to work

You could also Task::future(my_async_fn()).map(Message::ActionGesture)

A couple more examples here: How to use "and_then" with Task - #2 by airstrike

Its not working yet.

What i’m currently doing is:
update() → call my functions (eg helper.update) without using tasks or returns, and without async.

What i should be doing is:
update() → Setup a task with my async fn, and a resulting message. And return that, allowing iced to run the task for me.

Is that second way the right way to be setting up my application?
If thats the case, why is the async stuff all behind features instead of default?

How can i return none from a perform?

Message::Start => Task::perform(
                    async {self.start()}, 
                    |_| ()
                ),

If you have no code that does no do long blocking, you should not do that async. Why would you?

If the update would be async and called multiple times at once, that would create some issues I think. For example, which update has priority? How do you deal with the mutable state on multiple threads. The UI needs one single source of truth to display.
So even if you would make it async it would still need to be run in sync after each other to work.

If you have something that blocks or waits some time, just put it in a task. That creates some stuff that can be executed in the background. Once that is finished you change your state (single source of truth) based on that.

If you want to return none from a task, you can use Task.dischard.

You don’t need to wrap your fn in async {}. Is self.start() an async fn? If so, just Task::future(self.start()).discard() should work (I think)

But if self.start() is not async, just call it directly. You don’t always have to do your work within a Task

I suggest reading the examples directory

Hi guys.
How can i set a timer/delay before sending a message?

I have the following, but i dont think its right as this is just the duration, not a timer:

Task::perform(async {
  Duration::from_millis(FADE_DURATION as u64)
  }, |_| main_app::Message::GestureHandler(Message::UpdateHistory));

you can use sleep in tokio::time - Rust or sleep in async_std::task - Rust in the task

Thanks! that does the trick