KMP-NativeCoroutines icon indicating copy to clipboard operation
KMP-NativeCoroutines copied to clipboard

[WIP] JS Support

Open ankushg opened this issue 3 years ago • 2 comments

Work toward https://github.com/rickclephas/KMP-NativeCoroutines/issues/17

Let's get JS support working in here!

To Extract into Separate PRs

  • [x] migrate to atomicfu: https://github.com/rickclephas/KMP-NativeCoroutines/pull/44
  • [x] intermediate sourceset: https://github.com/rickclephas/KMP-NativeCoroutines/pull/45
  • [x] make nativeCoroutines JS-friendly
    • [x] migrate tests to kotlinx-coroutines-test: https://github.com/rickclephas/KMP-NativeCoroutines/pull/46
    • [x] Move Apple-specific tests into appleTest https://github.com/rickclephas/KMP-NativeCoroutines/commit/213f541f0abc18f3acdb9d1b76f21ce0da49afea
    • [x] Rename test functions to support all possible targets https://github.com/rickclephas/KMP-NativeCoroutines/commit/53fa62fa37c5e05d9988f5ce907c11b1c0cdb8ed)
  • [x] include JS target in nativeCoroutines sourceset https://github.com/rickclephas/KMP-NativeCoroutines/pull/48
  • [ ] define default CoroutineScope for JS target
  • [ ] add sample app with regular JS/TS consumer
  • [ ] update compiler plugin to generate correct native functions/properties
    • [ ] split config options into apple and js blocks
    • [ ] allow different suffix for Apple and JS

Open Questions

  • [ ] do we need to add @JsExport? or are the typealiases fine if we just @JsExport the entire class in commonMain? Though we get warnings for this if it has a public suspend fun…
  • [ ] should the defaultCoroutineScope be different on JS than Native?

Done in this PR (will be separated into separate PRs)

Main Code

  • [x] create supportedTarget as a sourceset between common and apple/js
  • [x] created a cross-platform expect fun for freeze()
  • [x] created PlatformError as an expect class
    • exposed as NSError on Apple and Throwable on JS
    • create an internal kotlinCause extension that we that we can use instead of digging into userInfo so the tests work on non-Apple platforms

Tests

  • [x] switch from Kotlin/Native AtomicInt to atomicfu AtomicInt (extracted to this PR: )
  • [x] copied tests from appleTest into supportedTargetTest
    • [x] convert from fun `test method with spaces()` to fun test_method_with_underscores() because JS doesn't support spaces (even with backticks)
    • [x] removed freezing-related tests from supportedTargetTest

Remaining

  • [ ] figure out how to properly migrate from runBlocking to runTest (from kotlinx-coroutines-test)
    • [x] Tests in supportedTargetTest are passing in JS
    • [ ] Tests in supportedTargetTest are currently getting InvalidMutabilityExceptions on Native
    • [ ] delete overlapping tests from AppleNative____ tests and only leave freezing ones in there
  • [ ] update KSP to generate the additional methods on the JS platform

ankushg avatar Feb 17 '22 06:02 ankushg

An InvalidMutabilityException seems to prevent the conversion to runTest. Opened an issue: https://github.com/Kotlin/kotlinx.coroutines/issues/3195.

rickclephas avatar Feb 17 '22 19:02 rickclephas

some sort of test case of consuming this code?

We should probably add a react client to the sample as discussed in https://github.com/rickclephas/KMP-NativeCoroutines/issues/17#issuecomment-1034879671.

do we need to add @JsExport? or are the typealiases fine if we just @JsExport the class?

Not 100% sure, but I think we won't need the @JsExport in the library itself. We'll definitely need to add those to the generated functions though.

rickclephas avatar Feb 17 '22 19:02 rickclephas