This shows how Tokio concurrency can be used inside Vizia.
Usually, Vizia expects you to create a new system thread every time you want to spawn a concurrent task (see
vizia::context::Context::spawn
).
System threads are usually heavier than async concurrency, especially with a high performance library like
Tokio, so it would be desirable to be able to use both.
Fortunately, by making main
into an async routine and running vizia's main event loop inside of it, it is
possible to spawn Tokio tasks outside of it. Whenever a Tokio task isn't running (e.g. during a sleep or when
waiting for a resource), execution is passed back to vizia.
I'm not sure whether work-stealing (rt-multi-thread
instead of just rt
) is necessary for this to work
seamlessly; I believe not, but I only tested with work-stealing, so experiment at your own peril.
In this demo, the async aspect is demonstrated by having every counter increment request be delayed by 1
second, by spawning a new task that awaits tokio::sleep
before emitting the actual increment event
AppEvent::DelayElapse
back into vizia.
While I don't plan on using vizia on the long term, due to it being retained-mode and more well-suited for developers familiar with reactive frameworks like React, I thought this was an interesting experiment to share with y'all Rustaceans. :)