-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using KustomExport types from external JS back in Kotlin #33
Comments
Did more digging, I think the issue is because the type from the package thing
@KustomExport
class Item(
val id: Long,
val name: String
)
external class JsLibrary {
fun getItem(): Item = definedExternally // This is actually type: thing.js.Item and not thing.Item
}
@KustomExport
class KotlinLibrary(private val js: JsLibrary) {
fun getItem(): Item {
// When getItem() is called, it should not be wrapped in an exportIem(result)
// as it's already the expected JS Item
return js.getItem()
}
} So now I just need to figure out a solution for that. |
Hi! Sorry I didn't found time to investigate that yet. You're probably right as we never tested with From what I see the class I need to think a bit more about the implications (and not a lot of time atm), but please share your ideas on this topic (and if you've the js/ts code or a github repository with this example it'll help me).
Thanks, really appreciated. |
Correct. The wrapper The I wrote a failing test case here: shama@082173c and I'm trying to figure out a solution. I'm still pretty new to Kotlin though. One idea, we can try modifying Or another idea I was going to explore, write a Transformer for |
Did you try to define an interface with the annotation instead of the external declaration? I presume it works quite similarly, so we could generate from an (annotated) external declaration the same wrapper than for an interface, then use it when we receive the js implementation to unwrap the result. |
That is a good idea. I tried with converting the fun getItemAt(at: Int): Item {
return proxy.getItemAt(at)
}
// becomes
public fun getItemAt(at: Int): Item {
val result = common.getItemAt(
at = at,
)
return result.exportItem()
} But if we tried to do something with the list of items, it doesn't know to unwrap and tries to use the JS provided items: fun sumAmounts(): Long {
return proxy.items.asList().fold(0) { sum, item -> sum + item.amount }
}
// becomes
public fun sumAmounts(): Double {
// sumAmounts() will iterate over the List<Item> but they will be the JS provided Item
val result = common.sumAmounts()
return result.toDouble()
} I tried to trick it into unwrapping the items first before using them: fun getItems(): List<Item> {
return proxy.items.asList()
}
// and then
fun getItems(): List<Item> {
val res = mutableListOf<Item>()
for ((i, item) in proxy.items) {
res.add(getItemAt(i.toInt()))
}
return res
}
fun sumAmounts(): Long {
return getItems().fold(0) { sum, item -> sum + item.amount }
} But that didn't work as it's still calling Would you be open to adding some logic to the wrappers that detects if it's a |
Have you tried to pass the proxy via a setter? I can't investigate more right now, but you should have a generated class wrapping the typescript implementation in your kotlin code (if you can breakpoint and check that). I presume the issue is here because you're able to inject the proxy without its wrapper directly in 'common/kotlin' classes. Totally open to contribution!
|
Got a chance to try that. Passing the proxy via a setter/fun didn't work but if pass the list from the proxy via a setter, it works with interfaces. This works when class ExternalClass() {
var jsItems: List<Item> = emptyList()
fun provideJsItems(newItems: List<Item>) {
jsItems = items
}
} const thirdPartyClass = new ThirdPartyClass(
[
new Item(1, "One", 42),
new Item(2, "Two", 100),
]
)
const externalClass = new ExternalClass()
externalClass.jsItems = thirdPartyClass.items
// or
externalClass.provideJsItems(thirdPartyClass.items) So thanks again! I think that might be the solution we'll use until I figure out a good solution for Setting the proxy itself didn't work, has the same issue where class ExternalClass() {
lateinit var proxy: IThirdPartyClass
fun provideProxy(newProxy: IThirdPartyClass) {
proxy = newProxy
}
} const externalClass = new ExternalClass()
externalClass.proxy = thirdPartyClass
// or
externalClass.provideProxy(thirdPartyClass) I'll set aside some time in the next couple weeks to dig into more. I appreciate your help! |
I just released the 0.8.1 for you, please tell me if it's ok for your needs as I can't test it on my previous project anymore. |
Fantastic, thank you! It's working well for me. |
Hi! First, just wanted to say thanks for this library. It's made the TypeScript from Kotlin so much nicer to use. :)
I ran into an issue while using with
external
and have been looking into a fix but I'm fairly new to Kotlin so I could just be doing something wrong. I'm using the latest on themaster
branch as well as tried with0.8.0
of KustomExport.If I have a type exported from Kotlin with
@KustomExport
(e.g.class Item
), create that type in JS (e.g.new Item()
), and then send it back through Kotlin, accessing the properties from that object returnsundefined
.I have this example Kotlin:
and this JS:
I've been attempting to add support for this use case but thought I'd open an issue just in case you are already familiar with this use case or know a way to support it. I've written a test for the use case here. The first four assertions pass but the others that interface with KustomExport do not pass.
It appears that returning an instance from JS, when it's ran through
CommonItem.exportItem()
, the getters that proxy to the internalcommon
are no longer connected. I'm new to Kotlin though so that could be incorrect.Thank you for your time!
The text was updated successfully, but these errors were encountered: