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
Async Module Loading / Progressive App Support #4201
Comments
An idea that is floating in my head is to introduce an IR node Nodes:
|
Coming from the source language side, I had the following in mind. Add a primitive method defined as: package object js {
def dynamicImport[A](body: => A): js.Promise[A] = <stub>
} which can be used like class Foo(val x: Int) {
def square: Int = x * x
}
class Bar {
def work(x: Int): Unit = {
val result = js.dynamicImport {
new Foo(x).square
}
for (r <- result.toFuture)
println(s"square is $r")
}
} The semantics would be that the // dyn-module-1.js
class $c_Foo { ... }
export default function(x) {
return new $c_Foo().init___I(x).square__I()
} // main.js
class $c_Bar {
...
work__I__V(x) {
const result = import("./dyn-module-1.js")
.then(mod => mod.default(x));
result.then(r => console.log(`square is ${r}`));
}
} How does it work? In practice, the body of a class Bar$Async$1 {
static def entryPoint__int__java.lang.Object(x: int): any = {
new $c_Foo().init___I(x).square__I();
}
} At the "call site", we leave an indication that there is a dynamic import block, mentioning the static method's full name + actual capture values. In terms of IR, we would only need one node of the form: case class AsyncStaticApply(cls: ClassName, methodName: MethodName, args: List[Tree]) extends Tree {
val tpe = AnyType
} which could print as async Bar$Async$1::entryPoint__int__java.lang.Object(x) and which semantically simply means: asynchronously call that static method with the given arguments, returning a That semantics allows the linker to implement it as:
The |
Hah, that's a neat solution. Unsure though why it should be a default export and not just simply a plain old boring static method. Also, I reckon |
Oh yes, sure, it can be exported under any name. But it needs to be exported, in order to be accessible in the
Ah yes, indeed. |
TODO: - Rename to loadDynamic? - Ensure that we can optimize dynamic native imports
Follow-up after #2681 / #4198 to support async module loading for true progressive app support
The text was updated successfully, but these errors were encountered: