How are people structuring wasm apps?
Is it:
-
rust code only runs in response to a user event (key press, mouse click, ...)
-
rust code runs in response to user event + requestAnimationFrame
-
[2] + some ticker that gets called at a frequency
===
I ask the above as it seems in wasm, Rust needs to 'give up the main loop' and register callbacks.
This is all going to depend on what type of app you are making.
For example, if you are writing some sort of CRUD app that runs in the browser, then you just need to handle user events.
However, if you are implementing a game then that'll almost entirely be based around requestAnimationFrame()
to update the game state and UI.
Either way, your WebAssembly is bound by the constraints of the environment it runs in... When you say "wasm apps", it sounds like you are referring to WebAssembly that runs inside the browser, and therefore you need to fit into JavaScript's eventing system. On the other hand, I do a lot of WebAssembly on the edge (mobile, devices, etc.) and desktop, and it's perfectly fine to call a host function which will fetch a page from the internet and block until the request completes.
1 Like
This is my situation, but WebGL contexts don't have .swapbuffers
, so I am wondering if I should minimize the amount of time in a requestAnimationFrame
.
If the answer to the above is true, then I need some type of 'tikcker' callback that handles work (network, updates), while the requestionAnimationFrame
only handles OpenGL related calls.
Is this just reinventing JavaScript promises or event handlers which call your WebAssembly code to let it know when something happens?
I don't know enough about JS development to answer your question.
On Rust/x86_64, the way I do OpenGL is:
-
there is some dedicated OpenGL thread
-
this thread does
global_state: Mutex<Arc<...>>;
loop {
get state from global_state;
render();
sleep(16.67ms); // I understand this has drift, callback is better, etc ...
}
Then, there are separate threads that do all types of work and swaps on global_state.
====
Now, in wasm32 / WebGL2, I have a requestAnimationFrame
, which not only does all the rendering work, but also does all the updating calculations
as a result, time_in_request_animation_frame = time_update + time_render_calls
now, I'm wondering whether there is benefit in refactoring the time_update
out into a separate event handler
All this is very wishy-washy as I don't really understand browser/JS/wasm model.