Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

Commit

Permalink
Remove Messenger actor and use withContext instead
Browse files Browse the repository at this point in the history
Inspired by [comment] by elizarov (on Jun 15) in
Kotlin/kotlinx.coroutines#87:

> when you ask and actor and want a result back the proper design would
> be to have a `suspend fun` with a normal (non-deferred) `Result`.
> However, please note that this whole ask & wait pattern is an
> anti-pattern in actor-based systems, since it limits scalability.

[comment]: Kotlin/kotlinx.coroutines#87 (comment)
  • Loading branch information
twyatt committed Apr 7, 2020
1 parent 790dbca commit 7b04eea
Show file tree
Hide file tree
Showing 59 changed files with 1,969 additions and 1,375 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
key: grade-${{ hashFiles('**/*.gradle*') }}

- name: Check
run: ./gradlew $GRADLE_ARGS check jacocoTestDebugUnitTestReport
run: ./gradlew $GRADLE_ARGS check jacocoTestReport

- name: Codecov
uses: codecov/codecov-action@v1
Expand Down
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ traditionally rely on [`BluetoothGattCallback`] calls with [suspension functions
callback: BluetoothGattCallback
): BluetoothGatt</code></pre></td>
<td><pre><code>suspend fun connectGatt(
context: Context,
autoConnect: Boolean = false
context: Context
): ConnectGattResult</code><sup>1</sup></pre></td>
</tr>
</table>
Expand All @@ -37,7 +36,7 @@ traditionally rely on [`BluetoothGattCallback`] calls with [suspension functions
sealed class ConnectGattResult {
data class Success(val gatt: Gatt) : ConnectGattResult()
data class Canceled(val cause: CancellationException) : ConnectGattResult()
data class Failure(val cause: Throwable) : ConnectGattResult()
data class Failure(val cause: Exception) : ConnectGattResult()
}
```

Expand Down Expand Up @@ -112,8 +111,7 @@ sealed class ConnectGattResult {
<sup>1</sup> _Suspends until `STATE_CONNECTED` or non-`GATT_SUCCESS` is received._<br/>
<sup>2</sup> _Suspends until `STATE_DISCONNECTED` or non-`GATT_SUCCESS` is received._<br/>
<sup>3</sup> _Throws [`RemoteException`] if underlying [`BluetoothGatt`] call returns `false`._<br/>
<sup>3</sup> _Throws `GattClosed` if `Gatt` is closed while method is executing._<br/>
<sup>3</sup> _Throws `GattConnectionLost` if `Gatt` is disconnects while method is executing._
<sup>3</sup> _Throws `ConnectionLost` if `Gatt` is closed while method is executing._<br/>

### Details

Expand Down Expand Up @@ -142,11 +140,11 @@ corresponding [`BluetoothGatt`] will be closed:
```kotlin
fun connect(context: Context, device: BluetoothDevice) {
val deferred = async {
device.connectGatt(context, autoConnect = false)
device.connectGatt(context)
}

launch {
delay(1000L) // Assume, for this example, that BLE connection takes more than 1 second.
delay(1_000L) // Assume, for this example, that BLE connection takes more than 1 second.

// Cancels the `async` Coroutine and automatically closes the underlying `BluetoothGatt`.
deferred.cancel()
Expand All @@ -157,8 +155,8 @@ fun connect(context: Context, device: BluetoothDevice) {
```

Note that in the above example, if the BLE connection takes less than 1 second, then the
**established** connection will **not** be cancelled (and `Gatt` will not be closed), and `result`
will be `ConnectGattResult.Success`.
**established** connection will **not** be cancelled and `result` will be
`ConnectGattResult.Success`.

### `Gatt` Coroutine Scope

Expand Down Expand Up @@ -204,7 +202,7 @@ dependencies {
# License

```
Copyright 2018 JUUL Labs
Copyright 2020 JUUL Labs
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
6 changes: 2 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
}

plugins {
id 'org.jetbrains.kotlin.android' version '1.3.60' apply false
id 'org.jetbrains.kotlin.android' version '1.3.70' apply false
id 'org.jmailen.kotlinter' version '2.2.0' apply false
id 'com.android.library' version '3.6.2' apply false
id 'com.vanniktech.maven.publish' version '0.11.1' apply false
Expand All @@ -28,8 +28,7 @@ subprojects {
subprojects {
tasks.withType(Test) {
testLogging {
// For more verbosity add: "standardOut", "standardError"
events "passed", "skipped", "failed"
events "passed", "skipped", "failed", "standardOut", "standardError"
exceptionFormat "full"
showExceptions true
showStackTraces true
Expand All @@ -44,7 +43,6 @@ gradle.taskGraph.whenReady { taskGraph ->
taskGraph.getAllTasks().findAll { task ->
task.name.startsWith('installArchives') || task.name.startsWith('publishArchives')
}.forEach { task ->
logger.error("VERSION_NAME='" + project.findProperty("VERSION_NAME") + "'")
task.doFirst {
if (!project.hasProperty("VERSION_NAME") || project.findProperty("VERSION_NAME").startsWith("unspecified")) {
logger.error("VERSION_NAME=" + project.findProperty("VERSION_NAME"))
Expand Down
7 changes: 3 additions & 4 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
fixes:
- "com/juul/able/experimental::"
- "com/juul/able/experimental/processor::"
- "com/juul/able/experimental/throwable::"
- "com/juul/able/experimental/logger/timber::"
- "com/juul/able/logger/timber::"
- "com/juul/able/processor::"
- "com/juul/able/throwable::"
3 changes: 2 additions & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
id 'org.jmailen.kotlinter'
id 'com.hiya.jacoco-android'
id 'com.vanniktech.maven.publish'
}
Expand All @@ -21,7 +22,7 @@ android {

dependencies {
api deps.kotlin.coroutines
api deps.kotlin.junit
testImplementation deps.kotlin.junit
testImplementation deps.mockk
testImplementation deps.equalsverifier
}
4 changes: 2 additions & 2 deletions core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
~ Copyright 2018 JUUL Labs, Inc.
~ Copyright 2020 JUUL Labs, Inc.
-->

<manifest package="com.juul.able.experimental" />
<manifest package="com.juul.able" />
32 changes: 5 additions & 27 deletions core/src/main/java/Able.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
/*
* Copyright 2018 JUUL Labs, Inc.
* Copyright 2020 JUUL Labs, Inc.
*/

package com.juul.able.experimental
package com.juul.able

import android.util.Log

interface Logger {
fun isLoggable(priority: Int): Boolean
fun log(priority: Int, throwable: Throwable? = null, message: String)
}
import com.juul.able.logger.AndroidLogger

object Able {

const val VERBOSE = 2
const val DEBUG = 3
const val INFO = 4
const val WARN = 5
const val ERROR = 6
const val ASSERT = 7

@Volatile
var logger: Logger = AndroidLogger()

inline fun assert(throwable: Throwable? = null, message: () -> String) {
Expand Down Expand Up @@ -48,18 +37,7 @@ object Able {

inline fun log(priority: Int, throwable: Throwable? = null, message: () -> String) {
if (logger.isLoggable(priority)) {
logger.log(priority, throwable, message())
logger.log(priority, throwable, message.invoke())
}
}
}

class AndroidLogger : Logger {

private val tag = "Able"

override fun isLoggable(priority: Int): Boolean = Log.isLoggable(tag, priority)

override fun log(priority: Int, throwable: Throwable?, message: String) {
Log.println(priority, tag, message)
}
}
67 changes: 0 additions & 67 deletions core/src/main/java/ConnectionStateMonitor.kt

This file was deleted.

90 changes: 0 additions & 90 deletions core/src/main/java/CoroutinesDevice.kt

This file was deleted.

Loading

0 comments on commit 7b04eea

Please sign in to comment.