handstandsam
09/06/2023, 5:34 PMjsonp
basically, or something like <http://window.my|window.my>_data="something"
to get the data currently.
Is there a similar workaroudn mechanism I can use in Compose WASM?mbonnin
09/06/2023, 5:36 PMmbonnin
09/06/2023, 5:37 PMI get around that in JavaScript withNot sure what you mean by that. You would still need a HTTP client to do the download?basically, or something likejsonp
to get the data currently.<http://window.my|window.my>_data="something"
Casey Brooks
09/06/2023, 6:02 PMCasey Brooks
09/06/2023, 6:12 PMhandstandsam
09/06/2023, 7:06 PMjsonp
means that you wrap the json in a function call my_function({"key": "value"})
in the response.
My current code just sets a global variable <http://window.my|window.my>_data = "something"
handstandsam
09/06/2023, 7:07 PMUse Browser APIs as exposed through the Kotlin stdlib to fetch an endpoint as a script, which sets a variable on the window scope.This is pretty interesting. It would only work using the methods I described above (jsonp and other javascript execution), but would not work for other CORS requests. think smart
Casey Brooks
09/06/2023, 7:22 PMfetch
API. The Fetch and XMLHttpRequest browser APIs both respect CORS, so any calls made through those HTTP clients (including Ktor) need proper CORS headers to be sent by the server.
JSONP uses the browser’s mechanism for downloading and running JS code as a proxy for fetching data. This API ignores CORS restrictions, but requires a painful and insecure method of getting usable data from a server. Like with CORS, it requires cooperation from the server you’re trying to access. If a server offers a JSONP endpoint, then they might also offer ways to call it cross-origin instead, and if so that would be a better option.
Anything Kotin/JS is doing is ultimately just compiled down to JS code, using the same browser APIs and fighting the same limitations you’d get if you wrote JS code by hand. There’s no fundamental difference to how browser APIs work when using Kotlin/JS, so the same kinds of solutions you’d find for a normal JS web app will also work for Kotlin apps. I’m not 100% certain about the difference with WASM, but from this docs page it looks like WASM interops with JS and browser APIs the same as with Kotlin/JS targetshandstandsam
09/06/2023, 8:19 PMhandstandsam
09/06/2023, 8:56 PMhandstandsam
09/07/2023, 4:29 PMorg.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1-wasm0
inside the WASM code to an object.
HTML:
<script src="<https://something/report-data.js>"></script>
Content of ☝️ is something like:
window.reportData = {"key": "value"}
Javascript:
function getJson() {
return JSON.stringify(window.reportData)
}
Kotlin WASM Code:
// Use external functions to call JS functions defined in global scope
external fun getJson(): String
...
// This is the parsed version of it in WASM that I can now iterate over
val jsonElement = Json { }.parseToJsonElement(getJson())
handstandsam
09/07/2023, 4:42 PM@Serializable
data class MyReportData(val key: String)
val reportData = json.decodeFromString(MyReportData.serializer(), getJson())