Search code examples
kotlinkotlin-multiplatformkotlin-js

kotlin multiplaform using HashMap in javascript


I'm using kotlin multiplatform to export a library to ios, android and possibly js.

the kotlin class is like this:

@JsExport
class CoreClient (private val httpClient: HTTPClient, private val socket: SocketClient, eventDelegate:EventEngineDelegate? = null): SocketClientDelegate {
  ...
   fun sessionLogin(data: Map<String, Any>, callback: ((Exception?, String?) -> Unit)) {
        Logger.i { "CoreClient - sessionLogin"}

        val body = data["body"] as Map<String, Any?>
        // create an event for connecting the socket, store the token for the login to happen after connected
        val event = SocketConnectEvent(
            body,
            null,
            null,
            null,
            null,
            "SocketConnect"
        )
        invokeEngine(event, eventCallback(callback))
    }
    ...

}

that's how I'm trying to use it:

const lib = require("../build/js/packages/clientsdk-core-js-legacy");

const kotlin = require("../build/js/packages_imported/kotlin/1.6.10/kotlin");
const HashMap = kotlin.kotlin.collections.HashMap
HashMap.prototype.get = HashMap.prototype.get_11rb$
HashMap.prototype.put = HashMap.prototype.put_xwzc9p$

const {core, middleware, api } = lib.com.nexmo.clientsdk
const {CoreClient} = core
const {VoiceClient, MediaClient} = api
const coreClient = new CoreClient(HTTPClient(), SocketClient())

const mediaClient = new MediaClient()
const client = new VoiceClient(coreClient, mediaClient)    

loginEventBody = new HashMap()
loginEventBody.put('token', 'TOKEN')
loginEventBody.put('device_id', 'js1')
loginEventBody.put('device_type', 'js')
loginEventBody.put('SDK_version', 'foo')

const loginEvent = new HashMap()
loginEvent.put('body', loginEventBody)

// console.log('sessionLogin_wt4221$')
coreClient.sessionLogin(loginEvent)

if I do this, I got the following error:

/Users/jboemo/WorkspaceNexmo/nexmoclient-sdk-core-kmp/build/js/packages_imported/kotlin/1.6.10/kotlin.js:41851
      return this.internalMap_uxhen5$_0.put_xwzc9p$(key, value);
                                        ^

TypeError: Cannot read property 'put_xwzc9p$' of null

my gradle configuration for js is the following:

js(BOTH){
        browser {
            commonWebpackConfig{
                cssSupport.enabled = false
            }
        }
    }

so my questions are:

  • why the kotlin hashmap got his method called in that wired name?
  • how am I supposed to use the kotlin hashmap in js?
  • is there a better way to expose this without using the expect and actual mechanism?

this was to do things is working great in both android and ios


Solution

  • See an issue in Kotlin Youtrack - https://youtrack.jetbrains.com/issue/KT-34995. enter image description here