:spider_web: Latest Kobweb v0.15.4 :spider_web: <h...
# kobweb
d
🕸️ Latest Kobweb v0.15.4 🕸️ https://github.com/varabyte/kobweb/releases/tag/v0.15.4 This is a big release, which is why things took longer than usual! We introduce web worker support in Kotlin. (If you're not familiar with web workers, it's a web standard which allows you to run some isolated code on a background thread that your main site can communicate with) Essentially, you define a web worker strategy (inside its own module with a build script that applies the
com.varabyte.kobweb.worker
plugin):
Copy code
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.
K 8
🔥 5
K 2
@Alexander Black I had been thinking about web workers for a while, but our conversation a month and a half or so back where I slipped up mentioning "web workers" when I meant to say "websockets" is what got me on this road of actually implementing it at last. So thanks for that 😄
And if anyone is curious about some behind the scenes work, before writing any code I put together this document: https://docs.google.com/document/d/1L3yE_2xcqajBMa2cbfc5Ys4g1CEsSPGjNE79d3GER_o/
The final result is a little bit different from that -- some things weren't apparent until after the code was working and I was playing with it -- but it's mostly there! I actually didn't think it would be possible when I was drafting up my ideal version, but we got there actually. Google KSP is pretty amazing.
👍 1
There's no example template for workers yet, I'm hoping to add one in the next few days. But there's a lot of information in the README so hopefully if you wanted to try workers out, you don't have to wait for me. Of course you can ask questions in this channel if you get stuck
We did our best to test this thing out from different angles, but no promises it's perfect by any means. Please let us know if any of you try it out and have any feedback.
a
@David Herman For some reason never saw this notification thus the late response. Hey that’s awesome though! Happy I could inspire that thought. 😄
🙌 1
Super cool stuff… excited to check this all out. 🙂