Skip to content

Commit

Permalink
Improve Waiter to not block when calling wake
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jkasten2 committed Mar 28, 2024
1 parent bf2cfda commit fd60e70
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.onesignal.common.threading

import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.runBlocking

/**
* An abstraction which allows for a suspending function to coordinate
Expand All @@ -18,7 +17,13 @@ class Waiter {
/**
* Wake the suspending function that has called [waitForWake].
*/
fun wake() = runBlocking { channel.send(null) }
fun wake() {
val result = channel.trySend(null)
if (result.isFailure) {
// Most likely only happens when the chanel is misconfigured or misused
throw Exception("Waiter.wait failed", result.exceptionOrNull())
}
}
}

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

0 comments on commit fd60e70

Please sign in to comment.