Skip to content

Commit 97069df

Browse files
authored
Fix requestPeripheral when using Kotlin/JS IR compiler (#146)
1 parent bcc679b commit 97069df

3 files changed

Lines changed: 47 additions & 7 deletions

File tree

buildSrc/src/main/kotlin/Dependencies.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ fun stately(
1818
version: String = "1.1.7-a1"
1919
): String = "co.touchlab:stately-$module:$version"
2020

21+
fun wrappers(
22+
version: String = "1.0.1-pre.213-kotlin-1.5.10"
23+
) = "org.jetbrains.kotlin-wrappers:kotlin-extensions:$version"
24+
2125
object androidx {
2226
fun startup(
2327
version: String = "1.0.0"

core/build.gradle.kts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ plugins {
1111
kotlin {
1212
explicitApi()
1313

14-
js().browser()
1514
android {
1615
publishAllLibraryVariants()
1716
}
17+
js().browser()
1818
iosX64()
1919
iosArm32()
2020
iosArm64()
@@ -28,6 +28,12 @@ kotlin {
2828
}
2929
}
3030

31+
val jsMain by getting {
32+
dependencies {
33+
implementation(wrappers())
34+
}
35+
}
36+
3137
val androidMain by getting {
3238
dependencies {
3339
api(coroutines("android"))
Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.juul.kable
22

3+
import com.juul.kable.Options.Filter.Name
4+
import com.juul.kable.Options.Filter.NamePrefix
5+
import com.juul.kable.Options.Filter.Services
36
import com.juul.kable.external.Bluetooth
7+
import kotlinext.js.jsObject
48
import kotlinx.coroutines.CoroutineScope
59
import kotlin.js.Promise
610

@@ -14,14 +18,40 @@ public fun CoroutineScope.requestPeripheral(
1418
.requestDevice(options.toDynamic())
1519
.then { device -> peripheral(device, builderAction) }
1620

21+
/**
22+
* Converts [Options] to JavaScript friendly object.
23+
*
24+
* According to the `requestDevice`
25+
* [example](https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/requestDevice#example), the form of the
26+
* JavaScript object should be similar to:
27+
* ```
28+
* let options = {
29+
* filters: [
30+
* {services: ['heart_rate']},
31+
* {services: [0x1802, 0x1803]},
32+
* {services: ['c48e6067-5295-48d3-8d5c-0395f61792b1']},
33+
* {name: 'ExampleName'},
34+
* {namePrefix: 'Prefix'}
35+
* ],
36+
* optionalServices: ['battery_service']
37+
* }
38+
* ```
39+
*/
1740
private fun Options.toDynamic(): dynamic = if (filters == null) {
18-
object {
19-
val acceptAllDevices = true
20-
val optionalServices = this@toDynamic.optionalServices
41+
jsObject {
42+
this.acceptAllDevices = true
43+
this.optionalServices = optionalServices
2144
}
2245
} else {
23-
object {
24-
val optionalServices = this@toDynamic.optionalServices
25-
val filters = this@toDynamic.filters
46+
jsObject {
47+
this.optionalServices = optionalServices
48+
this.filters = filters.map { it.toDynamic() }.toTypedArray()
2649
}
2750
}
51+
52+
private fun Options.Filter.toDynamic(): dynamic =
53+
when (this) {
54+
is Name -> jsObject { this.name = name }
55+
is NamePrefix -> jsObject { this.namePrefix = namePrefix }
56+
is Services -> jsObject { this.services = services }
57+
}

0 commit comments

Comments
 (0)