David Herman
01/13/2024, 8:28 PMcom.varabyte.kobweb.worker
plugin):
private fun calculatePi(numDigits: Int): List<Int> { /* ... */ }
@Serializable
class CalculatePiInput(val numDigits: Int)
@Serializable
class CalculatePiOutput(val digits: List<Int>)
// Input/Output types can be anything you want, as long as you can serialize it
internal class CalculatePiWorkerFactory
: WorkerFactory<CalculatePiInput, CalculatePiOutput> {
override fun createStrategy(postOutput: (CalculatePiOutput) -> Unit) =
WorkerStrategy<CalculatePiInput> { input ->
postOutput(calculatePi(input.numDigits))
}
override fun createIOSerializer() = Json.createIOSerializer()
}
With the above, Kobweb will create a Worker
for you, which you can use in your site to talk to the worker process. When your Kobweb application discovers a worker dependency, it will automatically extract and package the worker script for you too.
At this point, I think this may be the easiest way to create a web worker in a Kotlin/JS project.
Of course, we accumulated a bunch of additional changes and fixes during the long release cycle. Users @Ahmed Riyadh and @Dieter Vaesen helped out with some of them. thank you color
This comment is long enough. If you're interested to find out more, I recommend digging into the release notes and new README sections about the feature.David Herman
01/13/2024, 8:29 PMDavid Herman
01/13/2024, 8:32 PMDavid Herman
01/13/2024, 8:33 PMDavid Herman
01/13/2024, 8:34 PMDavid Herman
01/13/2024, 8:34 PMAlexander Black
01/22/2024, 11:15 PMAlexander Black
01/22/2024, 11:16 PM