I am building a Compose wasm webapp and i am analy...
# compose-web
t
I am building a Compose wasm webapp and i am analyzing files. I am using coroutine Dispatchers.Default for this. As far as i understand in Wasm multithreading is not possible. So my work stops the UI from working. Currently i fixed the issue with putting a lot of yield() commands in my code that analyzes the files. Just want to know if there is maybe a better solution.
w
You can defer this work to a Web Worker instead. That's sometimes difficult because you need to serialize the data sent across the wire between the worker and your main application. But this seems like a good use case for it because you can just send the initial file, do the analysis entirely in the worker, and only respond with a single final result. You should be able to write this worker entirely in Kotlin that compiles to JS.
t
Hi thank you for this advice. Is there maybe a sample project anywhere? But i will try to findout myself. My code is in kotlin common so it should compile to JS without any problems (I think otherwise it would also not compile to wasm right?)
k
w
Ooo, nice, I saw that but didn't realize it had been merged. Time to test it out on my image heavy site. 🖼️
k
@Winson Chiu I'd be happy to hear your feedback about it
t
So i got my Web Worker implementation now working. But there are some problems. Of course i want to run kotlin code in my worker so i create a module dedicated to the worker code. In the build.gradle.kts file currently i think it is not really possible to generate a wasm app not using the browser:
Copy code
wasmJs {
        outputModuleName = "worker-example"
        browser {
            binaries.executable()
        }
    }
But one problem is that inside a Web Worker it is not allowed to access document and other dom elements. So i needed to use a hack to prevent the compiler from generating this. Not sure if there is already an issue filed for this. Or if there are plans to maybe support WebWorker context out of the box for wasmJs