I'm porting a pile of C++ code to Javascript using the Emscripten system. The C++ code has many calls to fopen
which is a synchronous IO call. Within Emscripten, we simulate this using an XHR request to a local resource however, within Firefox synchronous XHR calls (with a responseType
of blob
or arraybuffer
) are only supported within a Web-Worker. Converting all that c++ code to adapt to asynchronous IO code seems very complicated so for my first try, I'd like to see if I can fake a synchronous XHR request.
My initial thought was that the main loop could share some state with a web-worker which could make the synchronous io call and update the shared state while the main loop paused and waited for the web-worker finished. DISCLAIMER: I know this is not the typical Javascript way but I am porting synchronous code, not writing new code from scratch (in which I would definitely have used asynchronous IO).
Given the restrictions on sharing state between a web-worker and the main-loop, this idea looks untenable.
Are there other ways to do this?