Does anyone have an example of using web workers w...
# webassembly
f
Does anyone have an example of using web workers with Kotlin/WASM? Not necessarily with the worker in Kotlin, but at least integrating the web worker with Kotlin code and the KMP gradle plugin.
t
What is your use case for web worker?
w
Maybe look at SQLDelight's
WebWorkerDriver
? Not sure exactly what you're looking for.
f
@turansky Run LLM on a different thread
@Winson Chiu I'll take a look, thanks
Looks like they publish a raw JS library that is just a web worker and then reference it from their wasm app https://www.npmjs.com/package/@cashapp/sqldelight-sqljs-worker
internal fun jsWorker(): Worker = js("""new Worker(new URL("@cashapp/sqldelight-sqljs-worker/sqljs.worker.js", import.meta.url))""")
w
Yes, although you don't have to actually publish it publicly. You can set up the node library as a folder.
f
@Winson Chiu Can you elaborate on how that is done?
f
Nice, having a simpler project helps a lot Is this a thing in web, where if you want to consume an API designed for a web worker, you need to set up the web worker yourself?
w
Not sure, I'm not that familiar with how web works. But it does seem like you can only access the APIs from inside a .js file. You can try to compile the Kotlin to JS, which is what I do for my service worker.
f
Yeah, that makes sense. I only need a few lines of code so I can endure the JS for that 😁 Thanks a lot!
t
We support
WorkerFactory
in Seskar - it allows to create
Worker
without separate subproject creation.
f
@turansky it says kotlin/js, is it for wasm?
t
Not for now But If you need own workers - you will need something similar
If WASM generarate single JS for subproject - you can share implementation with separate subproject for worker