Skip to content

Commit fd60e70

Browse files
committed
Improve Waiter to not block when calling wake
We don't want to do any waiting when we call wake, so use trySend instead. Also added result checking, to ensure we throw instead of silently handling the error so we know if there is an issue at any time.
1 parent bf2cfda commit fd60e70

File tree

1 file changed

+14
-3
lines changed
  • OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/threading

1 file changed

+14
-3
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/threading/Waiter.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.onesignal.common.threading
22

33
import kotlinx.coroutines.channels.Channel
4-
import kotlinx.coroutines.runBlocking
54

65
/**
76
* An abstraction which allows for a suspending function to coordinate
@@ -18,7 +17,13 @@ class Waiter {
1817
/**
1918
* Wake the suspending function that has called [waitForWake].
2019
*/
21-
fun wake() = runBlocking { channel.send(null) }
20+
fun wake() {
21+
val result = channel.trySend(null)
22+
if (result.isFailure) {
23+
// Most likely only happens when the chanel is misconfigured or misused
24+
throw Exception("Waiter.wait failed", result.exceptionOrNull())
25+
}
26+
}
2227
}
2328

2429
/**
@@ -40,5 +45,11 @@ open class WaiterWithValue<TType> {
4045
*
4146
* @param value The data to be returned by the [waitForWake].
4247
*/
43-
fun wake(value: TType) = runBlocking { channel.send(value) }
48+
fun wake(value: TType) {
49+
val result = channel.trySend(value)
50+
if (result.isFailure) {
51+
// Most likely only happens when the chanel is misconfigured or misused
52+
throw Exception("WaiterWithValue.wait failed", result.exceptionOrNull())
53+
}
54+
}
4455
}

0 commit comments

Comments
 (0)