I saw that support for exporting `Flow`s to JS got...
# javascript
m
I saw that support for exporting `Flow`s to JS got added to kotlinx.coroutinues recently. Is support for
SharedFlow
and
StateFlow
also planned?
SharedFlow
I could usually just cast to a flow, but for
StateFlow
having access to
.value
would be very useful.
a
With the merged changes only
Flow
is supported. But it makes sense to support
StateFlow
as well. CC: @Dmitry Khalanskiy [JB]
👍🏾 1
👍 1
d
What special support do you envision? If you have a
Flow
object, in the upcoming release, you'll be able to use the
for await
syntax in JS with that object. Since
StateFlow
and
SharedFlow
are both inheriting from
Flow
, that means them as well. Accessing the
.value
of a
StateFlow
should be no different than accessing any
val
of any interface from JavaScript. Am I missing something?
m
Maybe I misunderstood how
@JsExport
works but my understanding was that if I update to the new release and I export a
StateFlow
-typed field from Kotlin, JS/TS code will then see it an exported
Flow
. If I now want to access
.value
, it will not be listed in the generated TS types and directly accessing it from JS will also not work because only the mangled getter will be generated (since
StateFlow
itself is not exported and only exporting it generates the JS-friendly direct accessors, as far as I know). I checked with a small repro of the same hierarchy (exported
Flow
, non-exported
StateFlow
) and the field is typed as
Flow<...>/* StateFlow<...> */
in the
.d.ts
, and the impl class only gets
get_value_<hash>_k$()
, no
get value()
.
a
@Dmitry Khalanskiy [JB] I believe the case is not only to react on the
value
changed (what
for await
will do) but also to read it later. It could be done with something like this with just exported
Flow
, but I mean just accessing
StateFlow
with
value
instead of such code would be more DX-friendly:
Copy code
const state = { value: null };

(async () => {
  for await (const newValue of provideStateFlow()) {
    state.value = newValue;
  }
})();
m
Right, exactly.
value
is also available in non-async contexts, which is very useful for things like UI, where you need live updates but also a starting value (and you need it immediately).
d
I understand that the desire is to access
.value
, that much is clear.
Maybe I misunderstood how
@JsExport
works
I for one certainly do misunderstand that as well, so please bear with me!
if I update to the new release and I export a
StateFlow
-typed field from Kotlin, JS/TS code will then see it an exported
Flow
.
Why? If the field is
StateFlow
, why would the compiler upcast it to
Flow
on its own?
since
StateFlow
itself is not exported
What I don't understand here is why it wouldn't be exported in the first place. If the compiler forgets some of the type information about the exported fields, this sounds like a bug worth filing.
m
Why? If the field is
StateFlow
, why would the compiler upcast it to
Flow
on its own?
My understanding is that the compiler automatically falls back to the closest exported supertype, and only
Flow
has been marked with
@JsExport
in the initial implementation from what I've seen, right? And in that case, the compiler will only expose fields/functions defined directly on that supertype. I don't know whether there were any reasons for not including the annotation on
StateFlow
at the time. Would it be possible to add it? I sadly lack the technical knowledge in the relevant areas to contribute an implementation, especially since I don't know if the JS semantics would need to change when going from a cold to a hot flow.
➕ 1
d
My understanding is that the compiler automatically falls back to the closest exported supertype
Did I get it right that this code
Copy code
@JsExport
class MyClass(
    val dateField: kotlinx.datetime.LocalDate
)
would export
dateField
as some opaque object with no fields, since
kotlinx.datetime.LocalDate
isn't marked with
@JsExport
? If so, this looks like a glaring usability issue that should be fixed on the side of Kotlin/JS. Otherwise, we'd be accepting that Kotlin library authors would need to annotate everything with
@JsExport
, which doesn't look like an even remotely viable approach to me. Neither Kotlin/Native nor Kotlin/JVM require annotating every single public API.
m
From what I've seen,
dateField
would get exported as
any
(or
NonNullable<unknown>
if
-Xdts-use-unknown-instead-any
is used), unless
LocalDate
also gets a
@JsExport
annotation on it. You get a partial export if a (hypothetical) supertype of
LocalDate
had the annotation, where the field gets exported as that supertype instead. In this second case, JS code sees
dateField
as an instance of the supertype, and all the fields and methods defined on
LocalDate
itself are invisible to JS consumers (technically still present so Kotlin/JS code can use them, but with mangled names). So with the current Kotlin/JS approach, my understanding is that the only way to fix it would be to add
@JsExport
to all types that might appear in public signatures and that should be directly exportable.
😨 1
d
I see, thanks. We could of course annotate everything with
@JsExport
, that's not at all difficult. What's stopping me from proceeding with this is my strong opinion that this approach needs to be reworked. I don't think we can realistically require third-party libraries to spam
@JsExport
everywhere, and as a first-party library, I'd prefer
kotlinx.coroutines
not to set a precedent. @Filipp Zhinkin, could you please share your view of this issue (the last two messages should provide enough context)?
m
Also please note that this is just my understanding, I may be wrong on this. Maybe there is a better way of handling exporting at scale. For now, I am basically just asking for one more use of the annotation on
StateFlow
, but I understand if this is something you want to solve at a fundamental level instead.
thank you color 1
d
No problem at all, thanks for raising awareness of this! Ultimately, we're all interested in providing the best UX (even if it's not always obvious what the best method is), and feedback like yours helps us shape our understanding.
a
@Dmitry Khalanskiy [JB] thats true and how the transitive export works right now. We have plans to introduce a DSL on Gradle level to configure export of public declarations without @JsExport annotation: KT-47200
f
@Filipp Zhinkin, could you please share your view of this issue (the last two messages should provide enough context)?
@Dmitry Khalanskiy [JB], here's a quick answer: I agree that the export should be improved and it seems inconvenient to explicitly export every single declaration someone needs to expose to Js/Ts. With that said, if we can solve a particular problem right now by simply adding an annotation, then it's worth doing it. If/when the export will be improved, we can always remove the annotation.
➕ 3
d
If/when the export will be improved, we can always remove the annotation.
If that's actually the case, sure, there's no harm in annotating a couple of APIs with
JsExport
. The linked issue youtrack.jetbrains.com/issue/KT-47200/KJS-IR-Define-exports-without-JsExport does not convince me that we should expect that. Let's continue the discussion there!
a
As I said, the plan is to add the DSL in Gradle to mark public declarations as exported from build scripts (without the @JsExport annotation). The current estimation is 2.5.20-2.6.0, so this is the case. Anyway, the PR is created and waiting for review kodee greetings
❤️ 1
d
Yes, I did read what you said, and I still have questions. I asked them under KT-47200. In general, opening a PR without agreeing on all the details first is discouraged. A better call is to file an issue and gather feedback. The proposed change itself isn't complicated, and we would be able to handle it ourselves once all the details are clear.
e
We could of course annotate everything with
@JsExport
, that's not at all difficult
DO NOT do this btw. This will result in unwanted transitive exports in all projects that import your library. It looks like a straightforward solution but the impact is huge and problematic, also in terms of unnecessarily retained code.
a
@Edoardo Luppi we use a special internal annotation there which which export the declaration only in case its type is reachable through a directly exported declaration. So there will be no unconditional export of `Flow`/`StateFlow` in the production bundle
✔️ 1
e
Yes yes, I recall. Was referring to the user-available
@JsExport
specifically.