Hey folks, Do you know where I can find an example...
# javascript
j
Hey folks, Do you know where I can find an example on how to map Javascript listeners to a Kotlin JS app ? I am specifically looking at https://firebase.google.com/docs/firestore/query-data/listen#web-version-9.
Copy code
import { doc, onSnapshot } from "firebase/firestore";

const unsub = onSnapshot(doc(db, "cities", "SF"), (doc) => {
    console.log("Current data: ", doc.data());
});
This snippet listens to realtime changes in firestore and triggers the callback function then. I literally have no clear idea how to bind this with my Kotlin. The main issue I see is that I would need to pass a function as input to the binding, with access to the kotlin side data 🧐
Well, answering myself, it's easy as one would expect
on the javascript side, define a function that takes a callback
Copy code
export function syncMessages(callback){

listen(() => {
    callback()
})
on the kotlin side, call and integrate an anonymous function
Copy code
@JsModule("@jlengrand/firebase-ports")
@JsNonModule
external object FirebasePorts{
    fun syncMessages(callback: () -> Unit)
}

                                FirebasePorts.syncMessages() { -> println("BOUM") }
Now, every time
listen
will be called, so will
callback
. Haven't tried yet but pretty sure that works with parameters and promises as well
d
Couldn't you just call
listen()
directly from Kotlin and pass it the lambda?
j
Hum, that's possible indeed as well
in my case, I'm trying to now have to use dynamic, nor have to implement too many bindings in Kotlin and listen contains a lot of specific objects
But in that specific snippet that'd most likely be much better indeed