Do we really need to use two separate JS files for...
# javascript
t
Do we really need to use two separate JS files for web workers? Wouldn't it be possible to pass the main js, check if it runs as a worker or not and act accordingly? This would reduce the Gradle magic and the loading time (only one script is downloaded). It increases the amount of memory used by the web worker (I guess).
Well, it seems to be working. Why is this discarded, is there something I don't see/know?
Copy code
fun main() {
    if(js("typeof self.importScripts === 'function'") as? Boolean == true) {
        workerMain()
    } else {
        uiMain()
    }
}

fun uiMain() {
    println("UIMain")
    CoroutineScope(Dispatchers.Default).launch {
        Worker("/z2-rpc-test-multiplatform.js")
    }
}

fun workerMain() {
    println("WorkerMain")
}
b
Performance. You load entire app just to execute a small portion of the code relevant to your worker
That's why all js bundlers now chunk your js apps too so you could load to first page asap while the rest of the app is being loaded as needed
👆 2
t
Well, it seems to be working. Why is this discarded, is there something I don’t see/know?
Also some static imports or extension calls can produce variable declaration at start of JS file. As result - worker will crash.
Copy code
// static import for `ELEMENT_NODE`
var ELEMENT_NODE = Node.ELEMENT_NODE
t
I'm not really worried about performance as the main JS is loaded anyway. Using it as the web worker won't add much delay. I'm more worried about the worker pulling in kotlin.js and resulting a much bigger overall download. This is not a problem with pure Javascript but it is for Kotlin/JS. Do you have any good guide about how to chunk Kotlin / JS projects properly? I've been looking for that for a while. The worker crashing is a problem. Does this happen when the worker tries to access something that is not available for workers? I will probably opt for a pure JS worker as I really don't want to have a big JS file for a minimal functionality. This worker would perform a websocket communication and the sole reason for it is to handle tab throttling properly.
Ah, and thanks for the answers.
t
The worker crashing is a problem. Does this happen when the worker tries to access something that is not available for workers?
Yes
This worker would perform a websocket communication
If you will use Browser API only big chunk isn’t expected
I have special plugin for workers. • Valid
output.granularity
- first step. Otherwise app DCE will be applied workers. • Second step - task dependencies. To find workers in app dependencies I use separate Gradle configuration
jsMainModule
.
runtimeOnly
couldn’t be used for this goal.
thank you color 1