b

    Brian G

    10 months ago
    I can't figure out why I'm getting this ClassCastException on await():
    external interface BwipJs {}
    external fun <Module : Any> import(path: String): Promise<Module>
    
    @Composable
    fun Barcode() {
      val bwip = remember { mutableStateOf<BwipJs?>(null) }
      LaunchedEffect(Unit) {
        bwip.value = import<BwipJs>("bwip-js").await() // ClassCastException
      }
    }
    Outside of Compose, in plain Kotlin/JS, the import works fine, and I'm successfully calling functions (rendering barcodes) using the BwipJs handle, e.g.
    val canvas = document.createElement("canvas") as HTMLCanvasElement
    import<BwipJs>("bwip-js").then { bwipjs ->
        bwipjs.toCanvas(canvas)
    }
    a

    Akif Abasov [JB]

    10 months ago
    This isn’t compose web issue, this is Kotlin/JS issue.
    b

    Brian G

    10 months ago
    Do you know what the issue is? I was also able to reproduce it via just coroutines, so I moved my bug report over there: https://github.com/Kotlin/kotlinx.coroutines/issues/3009
    a

    Akif Abasov [JB]

    10 months ago
    It most liekly there’s issue in Kotlin wrapper of
    Promise
    , you can easily write your own version of
    await()
    using then/catch methods of
    Promise
    and coroutines API which will work corretcly.
    Actually i had done same workaround in one place and forgot to report it to Kotlin team. Thanks for report!
    b

    Brian G

    10 months ago
    Hmm, I think I tried every combination of await/then/catch and they all had the same issue. I suspect it's a bug deeper in the coroutines machinery.