Full Async Actions
Now that we have introduced Event
s and Action
s, we are going introduce a new mpsc::channel
for
Action
s. The advantage of this is that we can programmatically trigger updates to the state of the
app by sending Action
s on the channel.
Here’s the run
function refactored from before to introduce an Action
channel. In addition to
refactoring, we store the action_tx
half of the channel in the App
.
Running the code with this change should give the exact same behavior as before.
Now that we have stored the action_tx
half of the channel in the App
, we can use this to
schedule tasks. For example, let’s say we wanted to press J
and K
to perform some network
request and then increment the counter.
First, we have to update my Action
enum:
Next, we can update my event handler:
Finally, we can handle the action in my update
function by spawning a tokio task:
Here is the full code for reference:
With that, we have a fully async application that is tokio ready to spawn tasks to do work concurrently.