在 javascript 中使用 HashMap 的 kotlin 多平台
kotlin multiplaform using HashMap in javascript
我正在使用 kotlin multiplatform 将库导出到 ios、android 和可能的 js。
kotlin class 是这样的:
@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))
}
...
}
这就是我尝试使用它的方式:
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)
如果我这样做,我会收到以下错误:
/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
我的 gradle js 配置如下:
js(BOTH){
browser {
commonWebpackConfig{
cssSupport.enabled = false
}
}
}
所以我的问题是:
- 为什么 kotlin hashmap 以那个有线名称调用他的方法?
- 我应该如何在 js 中使用 kotlin hashmap?
- 有没有更好的方法在不使用期望和实际机制的情况下公开它?
这是为了做事在 android 和 ios
中都很有效
在 Kotlin Youtrack 中查看问题 - https://youtrack.jetbrains.com/issue/KT-34995。
我正在使用 kotlin multiplatform 将库导出到 ios、android 和可能的 js。
kotlin class 是这样的:
@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))
}
...
}
这就是我尝试使用它的方式:
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)
如果我这样做,我会收到以下错误:
/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
我的 gradle js 配置如下:
js(BOTH){
browser {
commonWebpackConfig{
cssSupport.enabled = false
}
}
}
所以我的问题是:
- 为什么 kotlin hashmap 以那个有线名称调用他的方法?
- 我应该如何在 js 中使用 kotlin hashmap?
- 有没有更好的方法在不使用期望和实际机制的情况下公开它?
这是为了做事在 android 和 ios
中都很有效在 Kotlin Youtrack 中查看问题 - https://youtrack.jetbrains.com/issue/KT-34995。