Posting this here too, my implementation on thread
# serialization
p
Posting this here too, my implementation on thread
Copy code
object ImageBitmapSerializer2 : KSerializer<Image> {
    private val base64Serializer = String.serializer()

    override val descriptor: SerialDescriptor = base64Serializer.descriptor

    override fun serialize(encoder: Encoder, value: Image) {
        val base64 = value.toBase64()
        encoder.encodeSerializableValue(base64Serializer, base64)
    }

    override fun deserialize(decoder: Decoder): Image {
        val base64 = decoder.decodeSerializableValue(base64Serializer)
        return base64.fromBase64()
    }

    @OptIn(ExperimentalEncodingApi::class)
    fun Image.toBase64(): String {
        // Directly encode to PNG with minimal overhead
        val encodedBytes = this.encodeToData(EncodedImageFormat.PNG, 80)?.bytes
        return Base64.encode(encodedBytes ?: byteArrayOf())
    }

    @OptIn(ExperimentalEncodingApi::class)
    fun String.fromBase64(): Image {
        return Image.makeFromEncoded(Base64.decode(this))
    }
}                                                 @Serializable(with = ImageBitmapSerializer::class)
var bitmap: Image? = null, (there can be multiple)
Copy code
viewModelScope.launch(defaultAsyncDispatcher) {
string = json.encodeToString(currentRootNode)
//currentRootNode can have a lot of images or only one (it doesnt matter cause it still lags) 
} in wasm actual val defaultAsyncDispatcher: CoroutineDispatcher
    get() = Dispatchers.Default
read the forwarded issue for context
a
@sandwwraith ^^
s
We do not have any special multi-threading mechanisms on Kotlin/Wasm, kotlinx.serialization there consists mainly from common code. I'm not sure if i can be of any help here, but if you have any ideas what causes the problem, feel free to ask me
p
If im not wrong the Wasm team is working on a poc for multithreading in wasm. Is there an eta of this experiment or is it too far ahead?